Switch to more generic implementation.

This commit is contained in:
Tom Alexander 2023-10-06 16:03:41 -04:00
parent 3da52a0826
commit c7dbe596b3
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 11 additions and 33 deletions

View File

@ -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<Option<(DiffStatus, Option<String>)>, Box<dyn std::error::Error>> {
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<Option<&'a str>>,
RG: Fn(R) -> RV,
>(
emacs: &'b Token<'s>,
emacs_field: &'x str,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<Option<(DiffStatus, Option<String>)>, Box<dyn std::error::Error>> {
let value = get_property_quoted_string(emacs, emacs_field)?;

View File

@ -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;

View File

@ -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<String> = 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)) => {