diff --git a/src/bin_wasm.rs b/src/bin_wasm.rs index 26675c8..242c235 100644 --- a/src/bin_wasm.rs +++ b/src/bin_wasm.rs @@ -11,7 +11,7 @@ mod wasm; pub fn parse_org(org_contents: &str) -> wasm_bindgen::JsValue { let global_settings = GlobalSettings::default(); let rust_parsed = parse_with_settings(org_contents, &global_settings) - .map(|document| ParseResult::Success(document.into())) + .map(|document| ParseResult::Success((org_contents, document).into())) .unwrap_or_else(|err| ParseResult::Error(format!("{:?}", err))); serde_wasm_bindgen::to_value(&rust_parsed).unwrap() } diff --git a/src/wasm/document.rs b/src/wasm/document.rs index 2bcd1d6..abd73e2 100644 --- a/src/wasm/document.rs +++ b/src/wasm/document.rs @@ -2,13 +2,17 @@ use organic::types::Document; use serde::Deserialize; use serde::Serialize; +use super::macros::to_wasm; + #[derive(Serialize, Deserialize)] #[serde(tag = "ast_node")] #[serde(rename = "org-data")] -pub(crate) struct WasmDocument {} - -impl From> for WasmDocument { - fn from(value: Document) -> Self { - todo!() - } +pub(crate) struct WasmDocument { + children: Vec<()>, } + +to_wasm!(WasmDocument, Document<'s>, original, document, { + WasmDocument { + children: Vec::new(), + } +}); diff --git a/src/wasm/macros.rs b/src/wasm/macros.rs new file mode 100644 index 0000000..8d02399 --- /dev/null +++ b/src/wasm/macros.rs @@ -0,0 +1,15 @@ +/// Write the implementation for the intermediate ast node. +/// +/// This exists to make changing the type signature easier. +macro_rules! to_wasm { + ($ostruct:ty, $istruct:ty, $original:ident, $document:ident, $fnbody:tt) => { + impl<'s> From<(&'s str, $istruct)> for $ostruct { + fn from(value: (&'s str, $istruct)) -> $ostruct { + let ($original, $document) = value; + $fnbody + } + } + }; +} + +pub(crate) use to_wasm; diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index 4ea7533..807f1ce 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -1,4 +1,5 @@ mod document; +mod macros; mod parse_result; pub(crate) use parse_result::ParseResult;