From c7dbe596b37d28bb0ef2ed83c9edd65738f8cb7c Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 6 Oct 2023 16:03:41 -0400 Subject: [PATCH] Switch to more generic implementation. --- src/compare/compare_field.rs | 29 ++--------------------------- src/compare/diff.rs | 11 +++++++---- src/compare/macros.rs | 4 ++-- 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/src/compare/compare_field.rs b/src/compare/compare_field.rs index e736170..c922b63 100644 --- a/src/compare/compare_field.rs +++ b/src/compare/compare_field.rs @@ -23,35 +23,10 @@ pub(crate) fn compare_noop<'b, 's, 'x>( Ok(None) } -pub(crate) fn compare_property_quoted_string_required_value<'b, 's, 'x>( +pub(crate) fn compare_property_quoted_string<'b, 's, 'x, R, RG: Fn(R) -> Option<&'s str>>( 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(crate) fn compare_property_quoted_string< - 'b, - 's, - 'x, - R, - RV: Debug + for<'a> PartialEq>, - RG: Fn(R) -> RV, ->( - emacs: &'b Token<'s>, - emacs_field: &'x str, rust_node: R, + emacs_field: &'x str, rust_value_getter: RG, ) -> Result)>, Box> { let value = get_property_quoted_string(emacs, emacs_field)?; diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 4b3a670..af12011 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use std::collections::BTreeSet; use std::collections::HashSet; +use super::compare_field::compare_property_quoted_string; use super::compare_field::compare_property_quoted_string_required_value; use super::elisp_fact::ElispFact; use super::elisp_fact::GetElispFact; @@ -2659,9 +2660,10 @@ fn compare_verbatim<'b, 's>( if let Some((new_status, new_message)) = compare_properties!( emacs, + rust, EmacsField::Required(":value"), - rust.contents, - compare_property_quoted_string_required_value + |r| Some(r.contents), + compare_property_quoted_string )? { this_status = new_status; message = new_message; @@ -2688,9 +2690,10 @@ fn compare_code<'b, 's>( if let Some((new_status, new_message)) = compare_properties!( emacs, + rust, EmacsField::Required(":value"), - rust.contents, - compare_property_quoted_string_required_value + |r| Some(r.contents), + compare_property_quoted_string )? { this_status = new_status; message = new_message; diff --git a/src/compare/macros.rs b/src/compare/macros.rs index 0e14012..c40c719 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, $rust_value:expr, $compare_fn: ident),+) => { + ($emacs:expr, $rust:expr, $($emacs_field:expr, $rust_value_getter:expr, $compare_fn: ident),+) => { { let mut this_status = DiffStatus::Good; let mut message: Option = None; @@ -57,7 +57,7 @@ macro_rules! compare_properties { name }, }; - let result = $compare_fn($emacs, emacs_name, $rust_value)?; + let result = $compare_fn($emacs, $rust, emacs_name, $rust_value_getter)?; match result { Some((DiffStatus::Good, _)) => unreachable!("No comparison functions should return Some() when DiffStatus is good."), Some((status, msg)) => {