Skip to content

Commit

Permalink
bodge: test wasm in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Oct 22, 2024
1 parent a593457 commit 6e219dc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 17 additions & 11 deletions src/client_reqwest.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -66,7 +69,9 @@ impl From<reqwest::Error> 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;

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/trait_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6e219dc

Please sign in to comment.