From f050e9b6a83e9b2c2499cd0c6f8e8c7201a344c1 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 27 Dec 2023 18:01:56 -0500 Subject: [PATCH] Taking into account additional property names but not comparing their values. --- src/wasm/additional_property.rs | 27 +++++++++++++++++++++++++++ src/wasm/document.rs | 22 ++++++++++++---------- src/wasm/mod.rs | 1 + src/wasm_test/macros.rs | 28 +++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 11 deletions(-) create mode 100644 src/wasm/additional_property.rs diff --git a/src/wasm/additional_property.rs b/src/wasm/additional_property.rs new file mode 100644 index 0000000..0fdc833 --- /dev/null +++ b/src/wasm/additional_property.rs @@ -0,0 +1,27 @@ +use std::collections::HashMap; + +use serde::Serialize; + +use super::WasmAstNode; + +#[derive(Debug, Serialize)] +pub enum AdditionalPropertyValue<'s, 'p> { + SingleString(&'s str), + ListOfStrings(Vec<&'s str>), + OptionalPair { + optval: Option<&'s str>, + val: &'s str, + }, + ObjectTree(Vec<(Option>>, Vec>)>), +} + +#[derive(Debug, Serialize, Default)] +pub struct AdditionalProperties<'s, 'p> { + pub(crate) properties: HashMap>, +} + +impl<'s, 'p> AdditionalProperties<'s, 'p> { + pub(crate) fn get_elisp_names<'c>(&'c self) -> impl Iterator + 'c { + self.properties.keys().map(move |key| format!(":{}", key)) + } +} diff --git a/src/wasm/document.rs b/src/wasm/document.rs index a4b4f55..e0ad38a 100644 --- a/src/wasm/document.rs +++ b/src/wasm/document.rs @@ -2,6 +2,8 @@ use std::path::PathBuf; use serde::Serialize; +use super::additional_property::AdditionalProperties; +use super::additional_property::AdditionalPropertyValue; use super::ast_node::WasmAstNode; use super::macros::to_wasm; use super::standard_properties::WasmStandardProperties; @@ -16,7 +18,7 @@ use crate::wasm::to_wasm::ToWasmStandardProperties; #[serde(rename = "org-data")] pub struct WasmDocument<'s, 'p> { pub(crate) standard_properties: WasmStandardProperties, - additional_properties: Vec<(String, &'s str)>, + pub(crate) additional_properties: AdditionalProperties<'s, 'p>, pub(crate) children: Vec>, pub(crate) category: Option<&'p str>, pub(crate) path: Option, @@ -32,15 +34,15 @@ to_wasm!( let category = original.category.as_ref().map(String::as_str); let path = original.path.clone(); - let additional_properties: Vec<(String, &str)> = original - .get_additional_properties() - .map(|node_property| { - ( - format!(":{}", node_property.property_name.to_uppercase()), - node_property.value.unwrap_or(""), - ) - }) - .collect(); + let mut additional_properties = AdditionalProperties::default(); + for (name, val) in original.get_additional_properties().map(|node_property| { + ( + node_property.property_name.to_uppercase(), + AdditionalPropertyValue::SingleString(node_property.value.unwrap_or("")), + ) + }) { + additional_properties.properties.insert(name, val); + } let children = original .zeroth_section diff --git a/src/wasm/mod.rs b/src/wasm/mod.rs index 97df599..c790977 100644 --- a/src/wasm/mod.rs +++ b/src/wasm/mod.rs @@ -1,3 +1,4 @@ +mod additional_property; mod angle_link; mod ast_node; mod babel_call; diff --git a/src/wasm_test/macros.rs b/src/wasm_test/macros.rs index 6972a47..0712196 100644 --- a/src/wasm_test/macros.rs +++ b/src/wasm_test/macros.rs @@ -40,10 +40,36 @@ macro_rules! wasm_compare { } { - // Compare standard properties + // Compare standard properties. result.extend(wasm_compare_standard_properties($source, $emacs, &$wasm.standard_properties)?)?; } + { + // Compare additional properties. + let additional_property_names: Vec = $wasm.additional_properties.get_elisp_names().collect(); + for additional_property in additional_property_names.iter().map(String::as_str).map(EmacsField::Required) { + match additional_property { + EmacsField::Required(name) if emacs_keys.contains(name) => { + emacs_keys.remove(name); + } + EmacsField::Optional(name) if emacs_keys.contains(name) => { + emacs_keys.remove(name); + } + EmacsField::Required(name) => { + result.status.push(WasmDiffStatus::Bad( + format!( + "Emacs node lacked required field ({name}).", + name = name, + ) + .into(), + )); + } + EmacsField::Optional(_name) => {} + + } + } + } + { // Compare children. result.extend(wasm_compare_list(