From 1b603f3a0546095eba57bf4e2e3d3519a969c2ee Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 6 Oct 2023 13:29:46 -0400 Subject: [PATCH] Implement the comparison. --- src/compare/diff.rs | 29 +++++++++++++++++++++++++++-- src/compare/macros.rs | 23 ++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 23d2575..739aa9c 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -425,6 +425,24 @@ fn compare_ast_node<'b, 's>( Ok(compare_result.into()) } +fn compare_property_quoted_string_required_value<'b, 's, 'x>( + emacs: &'b Token<'s>, + emacs_field: &'x str, + rust_value: &'s str, +) -> Result)>, Box> { + let value = get_property_quoted_string(emacs, emacs_field)?; + if value.as_ref().map(String::as_str) != Some(rust_value) { + let this_status = DiffStatus::Bad; + let message = Some(format!( + "{} mismatch (emacs != rust) {:?} != {:?}", + emacs_field, value, rust_value + )); + Ok(Some((this_status, message))) + } else { + Ok(None) + } +} + pub fn compare_document<'b, 's>( emacs: &'b Token<'s>, rust: &'b Document<'s>, @@ -2690,8 +2708,15 @@ fn compare_code<'b, 's>( )); } - let foo: Result)>, Box> = - compare_properties!(emacs, EmacsField::Required(":value")); + if let Some((new_status, new_message)) = compare_properties!( + emacs, + EmacsField::Required(":value"), + rust.contents, + compare_property_quoted_string_required_value + )? { + this_status = new_status; + message = new_message; + } Ok(DiffResult { status: this_status, diff --git a/src/compare/macros.rs b/src/compare/macros.rs index b71aa03..463dd9b 100644 --- a/src/compare/macros.rs +++ b/src/compare/macros.rs @@ -1,6 +1,6 @@ /// Create iterators for ast nodes where it only has to iterate over children macro_rules! compare_properties { - ($emacs:expr, $($emacs_field:expr),+) => { + ($emacs:expr, $($emacs_field:expr, $rust_value:expr, $compare_fn: ident),+) => { { let mut this_status = DiffStatus::Good; let mut message: Option = None; @@ -41,12 +41,29 @@ macro_rules! compare_properties { } $( - println!("{:?}", $emacs_field); + let emacs_name = match $emacs_field { + EmacsField::Required(name) => { + name + }, + EmacsField::Optional(name) => { + name + }, + }; + let result = $compare_fn($emacs, emacs_name, $rust_value)?; + match result { + Some((DiffStatus::Good, _)) => unreachable!("No comparison functions should return Some() when DiffStatus is good."), + Some((status, msg)) => { + this_status = status; + message = msg; + }, + _ => {} + } ),+ match this_status { DiffStatus::Good => { - Ok(None) + let result: Result<_, Box> = Ok(None); + result }, _ => { Ok(Some((this_status, message)))