diff --git a/.gitignore b/.gitignore index 4fffb2f..84877c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +/wasm diff --git a/Cargo.toml b/Cargo.toml index eaf8c8f..77560c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,15 @@ repository = "https://github.com/wezm/rsspls.git" readme = "README.md" license = "Apache-2.0" +[lib] +# cdylib is for WASM +crate-type = ["cdylib", "rlib"] + [dependencies] crc = "3.0.1" + +[target.'cfg(target_family = "wasm")'.dependencies] +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.5" +serde_bytes = "0.11.9" +wasm-bindgen = "0.2.84" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..030dfe8 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +all: wasm/macbinary.js + +wasm/macbinary.js: target/wasm32-unknown-unknown/release/macbinary.wasm + wasm-bindgen target/wasm32-unknown-unknown/release/macbinary.wasm --target web --out-dir wasm + +target/wasm32-unknown-unknown/release/macbinary.wasm: + cargo build --lib --target wasm32-unknown-unknown --release + +.PHONY: target/wasm32-unknown-unknown/release/macbinary.wasm diff --git a/src/lib.rs b/src/lib.rs index 3a48178..9da2f96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,8 @@ mod macroman; mod resource; #[cfg(test)] mod test; +#[cfg(target_family = "wasm")] +mod wasm; const MBIN_SIG: u32 = u32::from_be_bytes(*b"mBIN"); diff --git a/src/wasm.rs b/src/wasm.rs new file mode 100644 index 0000000..3de6e4b --- /dev/null +++ b/src/wasm.rs @@ -0,0 +1,31 @@ +use serde::{Deserialize, Serialize}; +use wasm_bindgen::prelude::*; + +use crate::ParseError; + +#[derive(Serialize, Deserialize)] +struct MacBinaryFile { + name: String, + #[serde(with = "serde_bytes")] + data_fork: Vec, + #[serde(with = "serde_bytes")] + rsrc_fork: Vec, +} + +#[wasm_bindgen] +pub fn parse_macbinary(data: &[u8]) -> Result { + let file = crate::parse(data)?; + let res = MacBinaryFile { + name: file.filename(), + data_fork: file.data_fork().to_vec(), + rsrc_fork: file.resource_fork_raw().to_vec(), + }; + let js = serde_wasm_bindgen::to_value(&res)?; + Ok(js) +} + +impl From for JsValue { + fn from(err: ParseError) -> JsValue { + JsValue::from(err.to_string()) + } +}