diff --git a/src/wasm/additional_property.rs b/src/wasm/additional_property.rs index 0fdc8334..8011103d 100644 --- a/src/wasm/additional_property.rs +++ b/src/wasm/additional_property.rs @@ -2,7 +2,11 @@ use std::collections::HashMap; use serde::Serialize; +use super::macros::to_wasm; +use super::to_wasm::ToWasm; use super::WasmAstNode; +use crate::types::AffiliatedKeywordValue; +use crate::types::AffiliatedKeywords; #[derive(Debug, Serialize)] pub enum AdditionalPropertyValue<'s, 'p> { @@ -25,3 +29,65 @@ impl<'s, 'p> AdditionalProperties<'s, 'p> { self.properties.keys().map(move |key| format!(":{}", key)) } } + +to_wasm!( + AdditionalProperties<'s, 'p>, + AffiliatedKeywords<'s>, + original, + wasm_context, + { + let mut additional_properties = AdditionalProperties::default(); + for (name, val) in original.keywords.iter() { + let converted_val = match val { + AffiliatedKeywordValue::SingleString(val) => { + AdditionalPropertyValue::SingleString(val) + } + AffiliatedKeywordValue::ListOfStrings(val) => { + AdditionalPropertyValue::ListOfStrings(val.clone()) + } + AffiliatedKeywordValue::OptionalPair { optval, val } => { + AdditionalPropertyValue::OptionalPair { + optval: optval.clone(), + val: val, + } + } + AffiliatedKeywordValue::ObjectTree(val) => { + let mut ret = Vec::with_capacity(val.len()); + + for (optval, value) in val { + let converted_optval = if let Some(optval) = optval { + Some( + optval + .iter() + .map(|child| { + child + .to_wasm(wasm_context.clone()) + .map(Into::>::into) + }) + .collect::, _>>()?, + ) + } else { + None + }; + let converted_value = value + .iter() + .map(|child| { + child + .to_wasm(wasm_context.clone()) + .map(Into::>::into) + }) + .collect::, _>>()?; + + ret.push((converted_optval, converted_value)); + } + + AdditionalPropertyValue::ObjectTree(ret) + } + }; + additional_properties + .properties + .insert(name.clone(), converted_val); + } + Ok(additional_properties) + } +); diff --git a/src/wasm/macros.rs b/src/wasm/macros.rs index e813f6aa..89ce843a 100644 --- a/src/wasm/macros.rs +++ b/src/wasm/macros.rs @@ -2,6 +2,19 @@ /// /// This exists to make changing the type signature easier. macro_rules! to_wasm { + ($ostruct:ty, $istruct:ty, $original:ident, $wasm_context:ident, $fnbody:tt) => { + impl<'s, 'p> ToWasm<'p> for $istruct { + type Output = $ostruct; + + fn to_wasm( + &'p self, + $wasm_context: crate::wasm::to_wasm::ToWasmContext<'_>, + ) -> Result { + let $original = self; + $fnbody + } + } + }; ($ostruct:ty, $istruct:ty, $original:ident, $wasm_context:ident, $standard_properties:ident, $fnbody:tt) => { impl<'s, 'p> ToWasm<'p> for $istruct { type Output = $ostruct; diff --git a/src/wasm/paragraph.rs b/src/wasm/paragraph.rs index 2b75e21c..e056b0bd 100644 --- a/src/wasm/paragraph.rs +++ b/src/wasm/paragraph.rs @@ -3,6 +3,10 @@ use serde::Serialize; use super::macros::to_wasm; use super::standard_properties::WasmStandardProperties; use super::to_wasm::ToWasm; +use super::AdditionalProperties; +#[cfg(feature = "wasm_test")] +use crate::compare::ElispFact; +use crate::types::GetAffiliatedKeywords; use crate::types::Paragraph; use crate::wasm::to_wasm::ToWasmStandardProperties; use crate::wasm::WasmAstNode; @@ -11,8 +15,9 @@ use crate::wasm::WasmAstNode; #[serde(tag = "ast_node")] #[serde(rename = "paragraph")] pub struct WasmParagraph<'s, 'p> { - standard_properties: WasmStandardProperties, - children: Vec>, + pub(crate) standard_properties: WasmStandardProperties, + pub(crate) additional_properties: AdditionalProperties<'s, 'p>, + pub(crate) children: Vec>, } to_wasm!( @@ -22,6 +27,10 @@ to_wasm!( wasm_context, standard_properties, { + let additional_properties = original + .get_affiliated_keywords() + .to_wasm(wasm_context.clone())?; + let children = original .children .iter() @@ -34,6 +43,7 @@ to_wasm!( Ok(WasmParagraph { standard_properties, + additional_properties, children, }) } @@ -44,3 +54,10 @@ impl<'s, 'p> Into> for WasmParagraph<'s, 'p> { WasmAstNode::Paragraph(self) } } + +#[cfg(feature = "wasm_test")] +impl<'s, 'p> ElispFact<'s> for WasmParagraph<'s, 'p> { + fn get_elisp_name<'b>(&'b self) -> std::borrow::Cow<'s, str> { + "paragraph".into() + } +} diff --git a/src/wasm_test/compare.rs b/src/wasm_test/compare.rs index b301f6b2..dc5d67b1 100644 --- a/src/wasm_test/compare.rs +++ b/src/wasm_test/compare.rs @@ -227,23 +227,7 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmParagraph<'s, 'p> { source: &'s str, emacs: &'b Token<'s>, ) -> Result, Box> { - // TODO: Implement this. - let result = WasmDiffResult::default(); - // let result = wasm_compare!( - // source, - // emacs, - // self, - // ( - // EmacsField::Required(":path"), - // |w| w.path.as_ref().and_then(|p| p.to_str()), - // wasm_compare_property_quoted_string - // ), - // ( - // EmacsField::Required(":CATEGORY"), - // |w| w.category.as_ref(), - // wasm_compare_property_quoted_string - // ) - // ); + let result = wasm_compare!(source, emacs, self,); Ok(result) }