Skip to content

feat!: use target_arch instead of feature flags for wasm vs uniffi #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/api_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
cat /tmp/post_generation_status.txt
echo ""
echo "🔍 Detailed diff:"
git diff --no-pager ${{ env.OUTPUT_DIR }}
GIT_PAGER="" git diff ${{ env.OUTPUT_DIR }}
echo ""
echo "💡 This indicates that the generated code is not in sync with the current specification."
echo " Please run 'cargo api generate-algod' and commit the changes."
Expand Down
14 changes: 4 additions & 10 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
ref: ${{ github.ref }}
- uses: dtolnay/rust-toolchain@master
with:
targets: wasm32-unknown-unknown
toolchain: 1.85.0
components: clippy, rustfmt

Expand All @@ -34,19 +35,12 @@ jobs:
- name: Install algokit CLI
run: uv tool install algokit

- name: Check formatting
run: cargo fmt --check
- name: Clippy
# Run clippy and treat warnings as errors
run: cargo clippy -- -D warnings
- name: Check
run: cargo check
- name: Check WASM
run: cargo check --no-default-features --features ffi_wasm

- name: Start localnet
run: algokit localnet start

- name: Run sanity script
run: bash scripts/sanity.sh

- name: Build all crates
run: cargo build --workspace

Expand Down
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde_repr = "^0.1"
serde_bytes = "^0.11"

# HTTP client
algokit_http_client = { path = "../algokit_http_client", features = ["ffi_uniffi"] }
algokit_http_client = { path = "../algokit_http_client" }
url = "^2.5"

{% if spec.has_msgpack_operations %}
Expand Down
6 changes: 5 additions & 1 deletion crates/algod_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ serde_repr = "^0.1"
serde_bytes = "^0.11"

# HTTP client
algokit_http_client = { path = "../algokit_http_client", features = ["ffi_uniffi"] }
algokit_http_client = { path = "../algokit_http_client" }
url = "^2.5"

# AlgoKit dependencies for msgpack and signed transactions
Expand All @@ -41,3 +41,7 @@ uuid = { version = "^1.0", features = ["v4"] }
[dev-dependencies]
tokio = { version = "1.0", features = ["full"] }
tokio-test = "^0.4"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.15", features = ["js"] }

22 changes: 9 additions & 13 deletions crates/algokit_http_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ version = "0.1.0"
edition = "2024"

[features]
default = ["default_client"]
ffi_uniffi = ["dep:uniffi"]
ffi_wasm = [
"dep:wasm-bindgen",
"dep:js-sys",
"dep:tsify-next",
"dep:serde-wasm-bindgen",
]
default_client = ["dep:reqwest"]

[dependencies]
async-trait = "0.1.88"
js-sys = { workspace = true, optional = true }
reqwest = { version = "0.12.19", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = { version = "0.6", optional = true }
thiserror.workspace = true
tsify-next = { workspace = true, optional = true }
uniffi = { workspace = true, optional = true }
wasm-bindgen = { workspace = true, optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = { workspace = true }
tsify-next = { workspace = true }
wasm-bindgen = { workspace = true }
wasm-bindgen-futures = "0.4.50"
serde-wasm-bindgen = { version = "0.6" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
uniffi = { workspace = true }
43 changes: 20 additions & 23 deletions crates/algokit_http_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use async_trait::async_trait;
use std::collections::HashMap;

#[cfg(feature = "ffi_uniffi")]
#[cfg(not(target_arch = "wasm32"))]
uniffi::setup_scaffolding!();

#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "ffi_uniffi", derive(uniffi::Error))]
#[cfg_attr(not(target_arch = "wasm32"), derive(uniffi::Error))]
pub enum HttpError {
#[error("HttpError: {0}")]
RequestError(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "ffi_uniffi", derive(uniffi::Enum))]
#[cfg_attr(feature = "ffi_wasm", derive(tsify_next::Tsify))]
#[cfg_attr(feature = "ffi_wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "ffi_wasm", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(not(target_arch = "wasm32"), derive(uniffi::Enum))]
#[cfg_attr(target_arch = "wasm32", derive(tsify_next::Tsify))]
#[cfg_attr(target_arch = "wasm32", tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(target_arch = "wasm32", derive(serde::Serialize, serde::Deserialize))]
pub enum HttpMethod {
Get,
Post,
Expand All @@ -41,23 +41,20 @@ impl HttpMethod {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "ffi_uniffi", derive(uniffi::Record))]
#[cfg_attr(feature = "ffi_wasm", derive(tsify_next::Tsify))]
#[cfg_attr(feature = "ffi_wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(feature = "ffi_wasm", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(not(target_arch = "wasm32"), derive(uniffi::Record))]
#[cfg_attr(target_arch = "wasm32", derive(tsify_next::Tsify))]
#[cfg_attr(target_arch = "wasm32", tsify(into_wasm_abi, from_wasm_abi))]
#[cfg_attr(target_arch = "wasm32", derive(serde::Serialize, serde::Deserialize))]
pub struct HttpResponse {
pub body: Vec<u8>,
pub headers: HashMap<String, String>,
}

#[cfg(not(feature = "ffi_wasm"))]
#[cfg_attr(feature = "ffi_uniffi", uniffi::export(with_foreign))]
#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(not(target_arch = "wasm32"), uniffi::export(with_foreign))]
#[async_trait]
/// This trait must be implemented by any HTTP client that is used by our Rust crates.
/// It is assumed the implementing type will provide the hostname, port, headers, etc. as needed for each request.
///
/// By default, this trait requires the implementing type to be `Send + Sync`.
/// For WASM targets, enable the `ffi_wasm` feature to use a different implementation that is compatible with WASM.
pub trait HttpClient: Send + Sync {
async fn request(
&self,
Expand Down Expand Up @@ -110,8 +107,8 @@ impl DefaultHttpClient {
}

#[cfg(feature = "default_client")]
#[cfg_attr(feature = "ffi_wasm", async_trait(?Send))]
#[cfg_attr(not(feature = "ffi_wasm"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl HttpClient for DefaultHttpClient {
async fn request(
&self,
Expand Down Expand Up @@ -178,16 +175,16 @@ impl HttpClient for DefaultHttpClient {
}

// WASM-specific implementations
#[cfg(feature = "ffi_wasm")]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "ffi_wasm")]
#[cfg(target_arch = "wasm32")]
use js_sys::Uint8Array;

#[cfg(feature = "ffi_wasm")]
#[cfg(target_arch = "wasm32")]
use tsify_next::Tsify;

#[cfg(feature = "ffi_wasm")]
#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
pub trait HttpClient {
async fn request(
Expand All @@ -201,7 +198,7 @@ pub trait HttpClient {
}

#[wasm_bindgen]
#[cfg(feature = "ffi_wasm")]
#[cfg(target_arch = "wasm32")]
extern "C" {
/// The interface for the JavaScript-based HTTP client that will be used in WASM environments.
///
Expand All @@ -219,7 +216,7 @@ extern "C" {
) -> Result<JsValue, JsValue>;
}

#[cfg(feature = "ffi_wasm")]
#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
impl HttpClient for WasmHttpClient {
async fn request(
Expand Down
20 changes: 9 additions & 11 deletions crates/algokit_transact_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ edition = "2021"
[lib]
crate-type = ["lib", "cdylib", "staticlib"]

[features]
default = ["ffi_uniffi"]
ffi_wasm = ["dep:wasm-bindgen", "dep:tsify-next", "dep:js-sys"]
ffi_uniffi = ["dep:uniffi"]

[dependencies]
algokit_transact = { path = "../algokit_transact", features = ['test_utils'] }
ffi_macros = { path = "../ffi_macros" }
Expand All @@ -22,14 +17,17 @@ serde_bytes = "0.11.15"
serde_json = "1.0.133"
base64 = "0.22.1"

tsify-next = { workspace = true, optional = true }
uniffi = { workspace = true, features = [
"scaffolding-ffi-buffer-fns",
], optional = true }
wasm-bindgen = { workspace = true, optional = true }
js-sys = { workspace = true, optional = true }
pretty_assertions = "1.4.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { workspace = true }
tsify-next = { workspace = true }
js-sys = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
uniffi = { workspace = true, features = [
"scaffolding-ffi-buffer-fns",
]}

[dev-dependencies]
wasm-pack = "0.13.1"
Expand Down
Loading
Loading