From ae11e390d16faf989bbcff050d0a5e3d0ff65caa Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 6 Oct 2023 13:45:19 -0400 Subject: [PATCH] Add a default case for tokens which do not have any expected properties except for :standard-properties. --- src/compare/diff.rs | 9 ++++++-- src/compare/macros.rs | 51 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index c824519..4b3a670 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -2575,9 +2575,14 @@ fn compare_bold<'b, 's>( rust: &'b Bold<'s>, ) -> Result, Box> { let children = emacs.as_list()?; - let this_status = DiffStatus::Good; + let mut this_status = DiffStatus::Good; let mut child_status = Vec::new(); - let message = None; + let mut message = None; + + if let Some((new_status, new_message)) = compare_properties!(emacs)? { + this_status = new_status; + message = new_message; + } for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) { child_status.push(compare_ast_node(source, emacs_child, rust_child.into())?); diff --git a/src/compare/macros.rs b/src/compare/macros.rs index 392ca07..0e14012 100644 --- a/src/compare/macros.rs +++ b/src/compare/macros.rs @@ -10,7 +10,15 @@ macro_rules! compare_properties { .nth(1) .ok_or("Should have an attributes child.")?; let attributes_map = attributes_child.as_map()?; - let mut emacs_keys: BTreeSet<&str> = attributes_map.keys().map(|s| *s).filter(|s| *s != ":standard-properties").collect(); + let mut emacs_keys: BTreeSet<&str> = attributes_map.keys().map(|s| *s).collect(); + if emacs_keys.contains(":standard-properties") { + emacs_keys.remove(":standard-properties"); + } else { + this_status = DiffStatus::Bad; + message = Some(format!( + "Emacs token lacks :standard-properties field.", + )); + } $( match $emacs_field { EmacsField::Required(name) if emacs_keys.contains(name) => { @@ -71,6 +79,47 @@ macro_rules! compare_properties { } } }; + // Default case for when there are no expected properties except for :standard-properties + ($emacs:expr) => { + { + let mut this_status = DiffStatus::Good; + let mut message: Option = None; + let children = $emacs.as_list()?; + let attributes_child = children + .iter() + .nth(1) + .ok_or("Should have an attributes child.")?; + let attributes_map = attributes_child.as_map()?; + let mut emacs_keys: BTreeSet<&str> = attributes_map.keys().map(|s| *s).collect(); + if emacs_keys.contains(":standard-properties") { + emacs_keys.remove(":standard-properties"); + } else { + this_status = DiffStatus::Bad; + message = Some(format!( + "Emacs token lacks :standard-properties field.", + )); + } + + if !emacs_keys.is_empty() { + let unexpected_keys: Vec<&str> = emacs_keys.into_iter().collect(); + let unexpected_keys = unexpected_keys.join(", "); + this_status = DiffStatus::Bad; + message = Some(format!( + "Emacs token had extra field(s): {}", + unexpected_keys + )); + } + match this_status { + DiffStatus::Good => { + let result: Result<_, Box> = Ok(None); + result + }, + _ => { + Ok(Some((this_status, message))) + } + } + } + }; } pub(crate) use compare_properties;