diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 76e00eba..54057747 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,27 +151,40 @@ jobs: - name: Build for Android (arm) run: cargo ndk --target armv7-linux-androideabi build --verbose - # # WebAssembly System Interface (WASI) - # wasi: - # name: WASI - # runs-on: ubuntu-latest - # needs: quality - # steps: - # - uses: actions/checkout@v4 - - # - name: Remove rust-toolchain.toml - # run: rm -f rust-toolchain.toml - - # - name: Install Rust - # uses: dtolnay/rust-toolchain@nightly - # with: - # targets: wasm32-wasip2 - - # - name: Cache dependencies - # uses: Swatinem/rust-cache@v2 - - # - name: Build for WASI - # run: cargo build --target wasm32-wasip2 --verbose - - # - name: Build examples for WASI - # run: cargo build --examples --target wasm32-wasip2 --verbose + # WebAssembly System Interface (WASI) + wasi: + name: WASI + runs-on: ubuntu-latest + needs: quality + steps: + - uses: actions/checkout@v4 + + - name: Remove rust-toolchain.toml + run: rm -f rust-toolchain.toml + + - name: Install Rust + uses: dtolnay/rust-toolchain@nightly + with: + targets: wasm32-wasip2 + + - name: Cache dependencies + uses: Swatinem/rust-cache@v2 + + - name: Install wasmtime + uses: taiki-e/install-action@v2 + with: + tool: wasmtime + + - name: Build for WASI + run: cargo build --target wasm32-wasip2 --verbose + + - name: Build examples for WASI + run: cargo build --examples --target wasm32-wasip2 --verbose + + - name: Run tests in WASI environment + run: | + cargo test --no-run --target wasm32-wasip2 --verbose + find target/wasm32-wasip2/debug/deps -type f -name "*.wasm" | while read -r test_file; do + echo "Running test: $test_file" + wasmtime run "$test_file" --dir=. + done diff --git a/README.md b/README.md index abbb3a8a..8ccfcf96 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ _Cross-platform filesystem notification library for Rust._ - [Notify Types Documentation][notify-types-docs] - [Mini Debouncer Documentation][debouncer-mini-docs] - [Full Debouncer Documentation][debouncer-full-docs] -- [File ID][file-id-docs] +- [File ID Documentation][file-id-docs] - [Examples][examples] - [Changelog][changelog] - [Upgrading notify from v4](UPGRADING_V4_TO_V5.md) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index d6e08ce5..e7f21180 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -10,10 +10,15 @@ notify = { workspace = true } notify-debouncer-mini = { workspace = true } notify-debouncer-full = { workspace = true } futures = { workspace = true } -tempfile = { workspace = true } log = { workspace = true } env_logger = { workspace = true } +[target.'cfg(not(target_family = "wasm"))'.dev-dependencies] +tempfile = { workspace = true } + +[target.'cfg(target_family = "wasm")'.dev-dependencies] +tempfile = { workspace = true, features = ["nightly"] } + [[example]] name = "async_monitor" path = "async_monitor.rs" diff --git a/file-id/Cargo.toml b/file-id/Cargo.toml index fa615f63..ff1d1ed1 100644 --- a/file-id/Cargo.toml +++ b/file-id/Cargo.toml @@ -23,5 +23,8 @@ serde = { workspace = true, optional = true } [target.'cfg(windows)'.dependencies] windows-sys = { workspace = true, features = ["Win32_Storage_FileSystem", "Win32_Foundation"] } -[dev-dependencies] -tempfile.workspace = true +[target.'cfg(not(target_family = "wasm"))'.dev-dependencies] +tempfile = { workspace = true } + +[target.'cfg(target_family = "wasm")'.dev-dependencies] +tempfile = { workspace = true, features = ["nightly"] } diff --git a/file-id/bin/file_id.rs b/file-id/bin/file_id.rs index 9f9c900d..c8be6941 100644 --- a/file-id/bin/file_id.rs +++ b/file-id/bin/file_id.rs @@ -8,7 +8,7 @@ fn main() { print_file_id(&path); } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", target_family = "wasm"))] fn print_file_id(path: &str) { print_result(file_id::get_file_id(path)); } diff --git a/file-id/src/lib.rs b/file-id/src/lib.rs index cd92136f..acd76ffa 100644 --- a/file-id/src/lib.rs +++ b/file-id/src/lib.rs @@ -27,6 +27,9 @@ //! let file_id = file_id::get_high_res_file_id(file.path()).unwrap(); //! println!("{file_id:?}"); //! ``` + +#![cfg_attr(target_family = "wasm", feature(wasip2, wasi_ext))] + use std::{fs, io, path::Path}; #[cfg(feature = "serde")] @@ -122,6 +125,16 @@ pub fn get_file_id(path: impl AsRef) -> io::Result { Ok(FileId::new_inode(metadata.dev(), metadata.ino())) } +/// Get the `FileId` for the file or directory at `path` +#[cfg(target_family = "wasm")] +pub fn get_file_id(path: impl AsRef) -> io::Result { + use std::os::wasi::fs::MetadataExt; + + let metadata = fs::metadata(path.as_ref())?; + + Ok(FileId::new_inode(metadata.dev(), metadata.ino())) +} + /// Get the `FileId` for the file or directory at `path` #[cfg(target_family = "windows")] pub fn get_file_id(path: impl AsRef) -> io::Result { diff --git a/notify-debouncer-full/Cargo.toml b/notify-debouncer-full/Cargo.toml index 07c7510e..ece7a461 100644 --- a/notify-debouncer-full/Cargo.toml +++ b/notify-debouncer-full/Cargo.toml @@ -35,4 +35,9 @@ rstest.workspace = true serde.workspace = true deser-hjson.workspace = true rand.workspace = true -tempfile.workspace = true + +[target.'cfg(not(target_family = "wasm"))'.dev-dependencies] +tempfile = { workspace = true } + +[target.'cfg(target_family = "wasm")'.dev-dependencies] +tempfile = { workspace = true, features = ["nightly"] } diff --git a/notify-debouncer-mini/Cargo.toml b/notify-debouncer-mini/Cargo.toml index 3a5c5a71..177920f5 100644 --- a/notify-debouncer-mini/Cargo.toml +++ b/notify-debouncer-mini/Cargo.toml @@ -25,4 +25,9 @@ notify.workspace = true notify-types.workspace = true crossbeam-channel = { workspace = true, optional = true } log.workspace = true -tempfile.workspace = true + +[target.'cfg(not(target_family = "wasm"))'.dev-dependencies] +tempfile = { workspace = true } + +[target.'cfg(target_family = "wasm")'.dev-dependencies] +tempfile = { workspace = true, features = ["nightly"] } diff --git a/notify/Cargo.toml b/notify/Cargo.toml index 0bf6de82..d8eea551 100644 --- a/notify/Cargo.toml +++ b/notify/Cargo.toml @@ -51,6 +51,11 @@ mio.workspace = true [dev-dependencies] serde_json.workspace = true -tempfile.workspace = true nix.workspace = true insta.workspace = true + +[target.'cfg(not(target_family = "wasm"))'.dev-dependencies] +tempfile = { workspace = true } + +[target.'cfg(target_family = "wasm")'.dev-dependencies] +tempfile = { workspace = true, features = ["nightly"] }