From 3da52a0826fe3e67df2112e739e01146a75197d1 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 6 Oct 2023 14:07:25 -0400 Subject: [PATCH] Make a more generic version of compare_property_quoted_string. This allows for the rust value to be determined by a function rather than hard-coded. --- src/compare/compare_field.rs | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/compare/compare_field.rs b/src/compare/compare_field.rs index 2f96d637..e7361700 100644 --- a/src/compare/compare_field.rs +++ b/src/compare/compare_field.rs @@ -1,3 +1,5 @@ +use std::fmt::Debug; + use super::diff::DiffStatus; use super::sexp::Token; use super::util::get_property_quoted_string; @@ -5,9 +7,22 @@ use super::util::get_property_quoted_string; #[derive(Debug)] pub(crate) enum EmacsField<'s> { Required(&'s str), + #[allow(dead_code)] Optional(&'s str), } +/// Do no comparison. +/// +/// This is for when you want to acknowledge that a field exists in the emacs token, but you do not have any validation for it when using the compare_properties!() macro. Ideally, this should be kept to a minimum since this represents untested values. +#[allow(dead_code)] +pub(crate) fn compare_noop<'b, 's, 'x>( + _emacs: &'b Token<'s>, + _emacs_field: &'x str, + _rust_value: (), +) -> Result)>, Box> { + Ok(None) +} + pub(crate) fn compare_property_quoted_string_required_value<'b, 's, 'x>( emacs: &'b Token<'s>, emacs_field: &'x str, @@ -25,3 +40,30 @@ pub(crate) fn compare_property_quoted_string_required_value<'b, 's, 'x>( 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, + rust_value_getter: RG, +) -> Result)>, Box> { + let value = get_property_quoted_string(emacs, emacs_field)?; + let rust_value = rust_value_getter(rust_node); + if !rust_value.eq(&value.as_ref().map(String::as_str)) { + 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) + } +}