From a9d45810ba022a01dae6c2830e70063dc28659ff Mon Sep 17 00:00:00 2001 From: Alex Wine Date: Fri, 16 Aug 2024 14:56:17 -0600 Subject: [PATCH] feat: added utilities library --- .github/workflows/ci.yml | 16 ++++++++++++++++ .moon/tasks/tag-rust.yml | 2 +- Cargo.lock | 9 +++++++++ crates/sample-wasm/Cargo.toml | 1 + crates/sample-wasm/src/lib.rs | 7 +------ crates/utilities/Cargo.toml | 12 ++++++++++++ crates/utilities/moon.yml | 3 +++ crates/utilities/src/lib.rs | 2 ++ crates/utilities/src/web.rs | 26 ++++++++++++++++++++++++++ crates/utilities/tests/lib.rs | 0 templates/rust-package/template.yml | 5 +++++ 11 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 crates/utilities/Cargo.toml create mode 100644 crates/utilities/moon.yml create mode 100644 crates/utilities/src/lib.rs create mode 100644 crates/utilities/src/web.rs create mode 100644 crates/utilities/tests/lib.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8907ad9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,16 @@ +name: "Pipeline" +on: + push: + branches: + - "main" + pull_request: +jobs: + ci: + name: "CI" + runs-on: "ubuntu-latest" + steps: + - uses: "actions/checkout@v4" + with: + fetch-depth: 0 + - uses: "moonrepo/setup-toolchain@v0" + - run: "moon ci" diff --git a/.moon/tasks/tag-rust.yml b/.moon/tasks/tag-rust.yml index 4211091..a051948 100644 --- a/.moon/tasks/tag-rust.yml +++ b/.moon/tasks/tag-rust.yml @@ -78,7 +78,7 @@ tasks: local: true test: - command: 'cargo nextest run --workspace' + command: 'cargo test --workspace' inputs: - '@group(cargo)' - '@group(sources)' diff --git a/Cargo.lock b/Cargo.lock index cd38249..4672c9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -97,6 +97,7 @@ version = "0.1.0" dependencies = [ "console_error_panic_hook", "js-sys", + "utilities", "wasm-bindgen", "wasm-bindgen-test", ] @@ -130,6 +131,14 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "utilities" +version = "0.1.0" +dependencies = [ + "wasm-bindgen", + "wasm-bindgen-test", +] + [[package]] name = "walkdir" version = "2.5.0" diff --git a/crates/sample-wasm/Cargo.toml b/crates/sample-wasm/Cargo.toml index 0f130f6..d400a45 100644 --- a/crates/sample-wasm/Cargo.toml +++ b/crates/sample-wasm/Cargo.toml @@ -19,6 +19,7 @@ wasm-bindgen = {workspace = true} # code size when deploying. console_error_panic_hook = { version = "0.1.7", optional = true } js-sys = {workspace = true} +utilities = { path = "../utilities" } [dev-dependencies] wasm-bindgen-test = {workspace = true} diff --git a/crates/sample-wasm/src/lib.rs b/crates/sample-wasm/src/lib.rs index 1fc116a..e8cca1f 100644 --- a/crates/sample-wasm/src/lib.rs +++ b/crates/sample-wasm/src/lib.rs @@ -1,5 +1,6 @@ mod utils; +use utilities::console_log; use wasm_bindgen::prelude::*; pub mod functions; // use functions::game_of_life::*; @@ -23,12 +24,6 @@ extern "C" { fn log_many(a: &str, b: &str); } -macro_rules! console_log { - // Note that this is using the `log` function imported above during - // `bare_bones` - ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) -} - #[wasm_bindgen] pub fn greet() { console_log!("Hello fibbers"); diff --git a/crates/utilities/Cargo.toml b/crates/utilities/Cargo.toml new file mode 100644 index 0000000..792ea19 --- /dev/null +++ b/crates/utilities/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "utilities" +version = "0.1.0" +authors = ["Alex Wine "] +edition = "2021" + + +[dependencies] +wasm-bindgen = {workspace = true} + +[dev-dependencies] +wasm-bindgen-test = {workspace = true} diff --git a/crates/utilities/moon.yml b/crates/utilities/moon.yml new file mode 100644 index 0000000..7d5ca13 --- /dev/null +++ b/crates/utilities/moon.yml @@ -0,0 +1,3 @@ +type: "library" + +tags: ['rust'] \ No newline at end of file diff --git a/crates/utilities/src/lib.rs b/crates/utilities/src/lib.rs new file mode 100644 index 0000000..a788614 --- /dev/null +++ b/crates/utilities/src/lib.rs @@ -0,0 +1,2 @@ +// #[cfg(target_arch = "wasm32")] +pub mod web; diff --git a/crates/utilities/src/web.rs b/crates/utilities/src/web.rs new file mode 100644 index 0000000..c63bc66 --- /dev/null +++ b/crates/utilities/src/web.rs @@ -0,0 +1,26 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern "C" { + fn alert(s: &str); + // Use `js_namespace` here to bind `console.log(..)` instead of just + // `log(..)` + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); + + // The `console.log` is quite polymorphic, so we can bind it with multiple + // signatures. Note that we need to use `js_name` to ensure we always call + // `log` in JS. + #[wasm_bindgen(js_namespace = console, js_name = log)] + fn log_u32(a: u32); + + // Multiple arguments too! + #[wasm_bindgen(js_namespace = console, js_name = log)] + fn log_many(a: &str, b: &str); +} + +#[macro_export] +macro_rules! console_log { + + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) +} diff --git a/crates/utilities/tests/lib.rs b/crates/utilities/tests/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/templates/rust-package/template.yml b/templates/rust-package/template.yml index 07d37ce..a8aa5f0 100644 --- a/templates/rust-package/template.yml +++ b/templates/rust-package/template.yml @@ -1,3 +1,8 @@ +# https://moonrepo.dev/docs/config/template +$schema: 'https://moonrepo.dev/schemas/template.json' + +destination: 'crates/[name | kebab_case]' + title: 'rust-package' description: | A simple rust package