diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 422a604..cf6be5b 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -117,24 +117,36 @@ jobs: include: - triple: x86_64-unknown-linux-gnu os: ubuntu-latest + features: --all-features - triple: aarch64-unknown-linux-gnu os: ubuntu-latest + features: --all-features - triple: armv7-unknown-linux-gnueabihf os: ubuntu-latest + features: --all-features - triple: arm-unknown-linux-gnueabihf os: ubuntu-latest + features: --all-features - triple: riscv64gc-unknown-linux-gnu os: ubuntu-latest + features: --all-features + - triple: wasm32-unknown-unknown + os: ubuntu-latest + features: --no-default-features --features=async-http-client - triple: x86_64-apple-darwin os: macos-latest + features: --all-features - triple: aarch64-apple-darwin os: macos-latest + features: --all-features - triple: x86_64-pc-windows-msvc os: windows-latest + features: --all-features - triple: aarch64-pc-windows-msvc os: windows-latest + features: --all-features env: RUSTFLAGS: --deny warnings steps: @@ -156,4 +168,4 @@ jobs: key: release-${{ matrix.triple }}-${{ steps.rust.outputs.cachekey }}-${{ hashFiles('**/Cargo.*') }} path: target/ - - run: ${{ runner.os == 'Linux' && 'cross' || 'cargo' }} build --release --offline --all-features --target ${{ matrix.triple }} + - run: ${{ runner.os == 'Linux' && 'cross' || 'cargo' }} build --release --offline ${{ matrix.features }} --target ${{ matrix.triple }} diff --git a/Cargo.toml b/Cargo.toml index e88e5b7..aae2ec8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,7 +101,7 @@ default-features = false features = ["multipart", "stream", "rustls-tls"] optional = true -[dependencies.tokio] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] version = "1" features = ["fs"] optional = true diff --git a/src/client_reqwest.rs b/src/client_reqwest.rs index 375359f..98d9f66 100644 --- a/src/client_reqwest.rs +++ b/src/client_reqwest.rs @@ -1,11 +1,9 @@ use std::path::PathBuf; -use std::time::Duration; use async_trait::async_trait; use bon::Builder; use reqwest::multipart; use serde_json::Value; -use tokio::fs::File; use crate::trait_async::AsyncTelegramApi; use crate::Error; @@ -17,16 +15,21 @@ pub struct AsyncApi { #[builder(into)] pub api_url: String, - #[builder( - default = reqwest::ClientBuilder::new() - .connect_timeout(Duration::from_secs(10)) - .timeout(Duration::from_secs(500)) - .build() - .unwrap() - )] + #[builder(default = default_client())] pub client: reqwest::Client, } +fn default_client() -> reqwest::Client { + let client_builder = reqwest::ClientBuilder::new(); + + #[cfg(not(target_arch = "wasm32"))] + let client_builder = client_builder + .connect_timeout(std::time::Duration::from_secs(10)) + .timeout(std::time::Duration::from_secs(500)); + + client_builder.build().unwrap() +} + impl AsyncApi { /// Create a new `AsyncApi`. You can use [`AsyncApi::new_url`] or [`AsyncApi::builder`] for more options. pub fn new(api_key: &str) -> Self { @@ -66,7 +69,9 @@ impl From for Error { } } -#[async_trait] +// Wasm target need not be `Send` because it is single-threaded +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl AsyncTelegramApi for AsyncApi { type Error = Error; @@ -128,8 +133,9 @@ impl AsyncTelegramApi for AsyncApi { } } + #[cfg(not(target_arch = "wasm32"))] // TODO: just for demonstration for (parameter_name, file_path, file_name) in files_with_paths { - let file = File::open(file_path) + let file = tokio::fs::File::open(file_path) .await .map_err(|error| Error::Encode(error.to_string()))?; let part = multipart::Part::stream(file).file_name(file_name); diff --git a/src/trait_async.rs b/src/trait_async.rs index cf550fa..c1a0fa4 100644 --- a/src/trait_async.rs +++ b/src/trait_async.rs @@ -48,7 +48,9 @@ macro_rules! request_nb { } } -#[async_trait::async_trait] +// Wasm target need not be `Send` because it is single-threaded +#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] +#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] pub trait AsyncTelegramApi where Self: Sync,