From 8ddd3d836e5e3488fbb4a8c1f9bc4230ea28e4eb Mon Sep 17 00:00:00 2001 From: Tyler Cook <10459406+cilki@users.noreply.github.com> Date: Thu, 25 Jul 2024 19:21:17 -0500 Subject: [PATCH] Compile for wasm32 --- couch_rs/Cargo.toml | 4 ++-- couch_rs/src/client.rs | 15 ++++++++++----- couch_rs/src/database.rs | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/couch_rs/Cargo.toml b/couch_rs/Cargo.toml index 8d44bb4..62b6e3e 100644 --- a/couch_rs/Cargo.toml +++ b/couch_rs/Cargo.toml @@ -20,7 +20,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" couch_rs_derive = { version = "0.10.1", optional = true, path = "../couch_rs_derive" } url = "2" -tokio = { version = "^1.32", features = ["rt-multi-thread"] } +tokio = { version = "^1.32", features = ["rt"] } base64 = "0.22" tokio-util = { version = "0.7", features = ["io"] } bytes = "1" @@ -39,7 +39,7 @@ default-features = false tokio = { version = "^1.32", features = ["rt-multi-thread", "macros"] } [features] -default = ["derive", "native-tls"] +default = ["derive", "native-tls", "tokio/rt-multi-thread"] integration-tests = [] # Provide derive(CouchDocument) macros. diff --git a/couch_rs/src/client.rs b/couch_rs/src/client.rs index f2eb053..3c9e180 100644 --- a/couch_rs/src/client.rs +++ b/couch_rs/src/client.rs @@ -120,14 +120,19 @@ impl Client { headers.insert(header::AUTHORIZATION, auth_header); } - let mut client_builder = reqwest::Client::builder().default_headers(headers).gzip(true); - if let Some(t) = timeout { - client_builder = client_builder.timeout(Duration::new(t, 0)); + let mut client_builder = reqwest::Client::builder().default_headers(headers); + + #[cfg(not(target_arch = "wasm32"))] + { + client_builder = client_builder.gzip(true); + + if let Some(t) = timeout { + client_builder = client_builder.timeout(Duration::new(t, 0)); + } } - let client = client_builder.build()?; Ok(Client { - _client: client, + _client: client_builder.build()?, uri: parse_server(uri)?, _gzip: true, _timeout: timeout, diff --git a/couch_rs/src/database.rs b/couch_rs/src/database.rs index 238768b..89af3f1 100644 --- a/couch_rs/src/database.rs +++ b/couch_rs/src/database.rs @@ -20,10 +20,32 @@ use std::{collections::HashMap, fmt::Debug, pin::Pin, sync::Arc}; use tokio::sync::mpsc::Sender; trait CouchJsonExt { + #[cfg(target_arch = "wasm32")] + fn couch_json(self) -> Pin>>>; + #[cfg(not(target_arch = "wasm32"))] fn couch_json(self) -> Pin> + Send>>; } impl CouchJsonExt for reqwest::Response { + #[cfg(target_arch = "wasm32")] + fn couch_json(self) -> Pin>>> { + let fut = async move { + let x = self.json(); + + match x.await { + Ok(x) => Ok(x), + Err(e) if e.is_decode() => Err(CouchError::InvalidJson(ErrorMessage { + message: e.to_string(), + upstream: Some(Arc::new(e)), + })), + Err(e) => Err(e.into()), + } + }; + + Box::pin(fut) + } + + #[cfg(not(target_arch = "wasm32"))] fn couch_json(self) -> Pin> + Send>> { let fut = async move { let x = self.json();