Skip to content

Commit 3acb111

Browse files
committed
Add wasm flag
1 parent f6d03d9 commit 3acb111

File tree

11 files changed

+66
-20
lines changed

11 files changed

+66
-20
lines changed

.github/workflows/rust-tests.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,18 @@ jobs:
8888

8989
- name: test
9090
run: cargo test
91+
92+
build-wasm:
93+
name: Build for WASM
94+
runs-on: ubuntu-latest
95+
steps:
96+
- name: Checkout repository
97+
uses: actions/checkout@v5
98+
99+
- name: Install Rust toolchain
100+
uses: dtolnay/rust-toolchain@stable
101+
with:
102+
targets: wasm32-unknown-unknown
103+
104+
- name: Build for wasm32
105+
run: cargo build --target wasm32-unknown-unknown --no-default-features --features "storage-inmemory"

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,24 @@ resolver = "2"
2020
[features]
2121
default = ["sync", "bundled", "storage"]
2222

23+
# (private) Support for the sync API
24+
sync-api = []
2325
# Support for all sync solutions
24-
sync = ["server-sync", "server-gcp", "server-aws", "server-local"]
26+
sync = ["sync-api", "server-sync", "server-gcp", "server-aws", "server-local"]
2527
# Support for sync to a server
26-
server-sync = ["encryption", "dep:ureq", "dep:url"]
28+
server-sync = ["sync-api", "encryption", "dep:ureq", "dep:url"]
2729
# Support for sync to GCP
28-
server-gcp = ["cloud", "encryption", "dep:google-cloud-storage"]
30+
server-gcp = ["sync-api", "cloud", "encryption", "dep:google-cloud-storage"]
2931
# Support for sync to AWS
30-
server-aws = ["cloud", "encryption", "dep:aws-sdk-s3", "dep:aws-config", "dep:aws-credential-types"]
32+
server-aws = ["sync-api", "cloud", "encryption", "dep:aws-sdk-s3", "dep:aws-config", "dep:aws-credential-types"]
3133
# Suppport for sync to another SQLite database on the same machine
32-
server-local = ["storage-sqlite"]
34+
server-local = ["sync-api", "storage-sqlite"]
3335
# Support for all task storage backends
3436
storage = ["storage-sqlite"]
3537
# Support for SQLite task storage
3638
storage-sqlite = ["dep:rusqlite", "dep:tokio-rusqlite"]
39+
# Support for the in-memory storage backend (WASM-compatible)
40+
storage-inmemory = []
3741
# (private) Support for sync protocol encryption
3842
encryption = ["dep:ring"]
3943
# (private) Generic support for cloud sync
@@ -62,16 +66,26 @@ serde_json = "^1.0"
6266
serde = { version = "^1.0.147", features = ["derive"] }
6367
strum = "0.27"
6468
strum_macros = "0.27"
65-
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
6669
thiserror = "2.0"
6770
ureq = { version = "^2.12.1", features = ["tls"], optional = true }
6871
uuid = { version = "^1.16.0", features = ["serde", "v4"] }
6972
url = { version = "2", optional = true }
7073
async-trait = "0.1.89"
7174
tokio-rusqlite = { version = "0.6.0", optional = true }
75+
# Use only WASM-compatible features by default
76+
tokio = { version = "1", features = ["macros"] }
77+
78+
# For non-WASM targets, enable the multi-threaded runtime
79+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
80+
tokio = { version = "1", features = ["rt-multi-thread"] }
81+
82+
# For WASM targets, enable the single-threaded runtime and WASM-friendly uuid features
83+
[target.'cfg(target_arch = "wasm32")'.dependencies]
84+
tokio = { version = "1", features = ["rt"] }
85+
uuid = { version = "^1.16.0", features = ["js"] }
7286

7387
[dev-dependencies]
7488
proptest = "^1.7.0"
7589
tempfile = "3"
7690
rstest = "0.26"
77-
pretty_assertions = "1"
91+
pretty_assertions = "1"

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@ The Rust API, as defined in [the docs](https://docs.rs/taskchampion/latest/taskc
2020

2121
The Rust API follows semantic versioning.
2222
As this is still in the `0.x` phase, so breaking changes may occur but will be indicated with a change to the minor version.
23+
24+
## Building for WebAssembly (WASM)
25+
When compiling this crate for a WebAssembly target (e.g., wasm32-unknown-unknown), you must disable the default features, as they include dependencies not compatible with WASM. Instead, select only the features you need that are WASM-compatible.
26+
27+
For example, to build with in-memory storage, use the following command:
28+
29+
```bash
30+
cargo build --target wasm32-unknown-unknown --no-default-features --features "storage-inmemory"
31+
```

src/errors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,10 @@ impl From<ureq::Error> for Error {
7474

7575
pub(crate) type Result<T> = std::result::Result<T, Error>;
7676

77-
#[cfg(test)]
77+
#[cfg(all(test, feature = "server-sync"))]
7878
mod test {
7979
use super::*;
8080

81-
#[cfg(feature = "server-sync")]
8281
#[test]
8382
fn ureq_error_status() {
8483
let err = ureq::Error::Status(

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ pub use depmap::DependencyMap;
1919
pub use errors::Error;
2020
pub use operation::{Operation, Operations};
2121
pub use replica::Replica;
22-
pub use server::{Server, ServerConfig};
22+
pub use server::Server;
23+
#[cfg(feature = "sync-api")]
24+
pub use server::ServerConfig;
2325
pub use task::{utc_timestamp, Annotation, Status, Tag, Task, TaskData};
2426
pub use workingset::WorkingSet;
2527

src/server/config.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#[cfg(feature = "sync-api")]
12
use super::types::Server;
3+
#[cfg(feature = "sync-api")]
24
use crate::errors::Result;
35
#[cfg(feature = "server-aws")]
46
pub use crate::server::cloud::aws::AwsCredentials;
@@ -12,17 +14,18 @@ use crate::server::cloud::CloudServer;
1214
use crate::server::local::LocalServer;
1315
#[cfg(feature = "server-sync")]
1416
use crate::server::sync::SyncServer;
15-
use std::{
16-
path::PathBuf,
17-
sync::{Arc, Mutex},
18-
};
17+
#[cfg(feature = "server-local")]
18+
use std::path::PathBuf;
19+
#[cfg(feature = "sync-api")]
20+
use std::sync::{Arc, Mutex};
1921
#[cfg(feature = "server-sync")]
2022
use uuid::Uuid;
2123

2224
/// The configuration for a replica's access to a sync server.
2325
///
2426
/// This enum is non-exhaustive, as users should only be constructing required
2527
/// variants, not matching on it.
28+
#[cfg(feature = "sync-api")]
2629
#[non_exhaustive]
2730
pub enum ServerConfig {
2831
/// A local task database, for situations with a single replica.
@@ -96,6 +99,7 @@ pub enum ServerConfig {
9699
},
97100
}
98101

102+
#[cfg(feature = "sync-api")]
99103
impl ServerConfig {
100104
/// Get a server based on this configuration
101105
pub fn into_server(self) -> Result<Arc<Mutex<dyn Server + Send>>> {

src/server/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod sync;
2828
#[cfg(feature = "cloud")]
2929
mod cloud;
3030

31+
#[cfg(feature = "sync-api")]
3132
pub use config::*;
3233
pub use types::*;
3334

tests/cross-sync.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#![cfg(feature = "server-local")]
2+
13
use chrono::Utc;
24
use pretty_assertions::assert_eq;
35
use taskchampion::{storage::InMemoryStorage, Operations, Replica, ServerConfig, Status, Uuid};
46
use tempfile::TempDir;
57

68
#[tokio::test]
7-
#[cfg(feature = "server-local")]
89
async fn cross_sync() -> anyhow::Result<()> {
910
// set up two replicas, and demonstrate replication between them
1011
let mut rep1 = Replica::new(InMemoryStorage::new());

tests/syncing-proptest.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
#![cfg(feature = "storage-sqlite")]
2+
13
use pretty_assertions::assert_eq;
24
use proptest::prelude::*;
3-
#[cfg(feature = "storage-sqlite")]
45
use taskchampion::{storage::InMemoryStorage, Operations, Replica, ServerConfig, TaskData, Uuid};
56
use tempfile::TempDir;
67

@@ -27,7 +28,6 @@ fn actions() -> impl Strategy<Value = Vec<(Action, u8)>> {
2728

2829
proptest! {
2930
#[test]
30-
#[cfg(feature = "storage-sqlite")]
3131
/// Check that various sequences of operations on mulitple db's do not get the db's into an
3232
/// incompatible state. The main concern here is that there might be a sequence of operations
3333
/// that results in a task being in different states in different replicas. Different tasks

0 commit comments

Comments
 (0)