Implement a compare_property_numeric.

This commit is contained in:
Tom Alexander 2023-10-09 21:04:41 -04:00
parent 8e357ed3b6
commit 166e59b922
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 84 additions and 0 deletions

View File

@ -1,4 +1,5 @@
use std::fmt::Debug;
use std::str::FromStr;
use super::diff::artificial_diff_scope;
use super::diff::compare_ast_node;
@ -7,6 +8,7 @@ use super::diff::DiffStatus;
use super::sexp::unquote;
use super::sexp::Token;
use super::util::get_property;
use super::util::get_property_numeric;
use super::util::get_property_quoted_string;
use super::util::get_property_unquoted_atom;
use crate::types::AstNode;
@ -117,6 +119,38 @@ pub(crate) fn compare_property_unquoted_atom<'b, 's, 'x, R, RG: Fn(R) -> Option<
}
}
pub(crate) fn compare_property_numeric<
'b,
's,
'x,
R,
RV: FromStr + PartialEq + Debug,
RG: Fn(R) -> Option<RV>,
>(
_source: &'s str,
emacs: &'b Token<'s>,
rust_node: R,
emacs_field: &'x str,
rust_value_getter: RG,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error + 's>>
where
<RV as FromStr>::Err: std::error::Error,
<RV as FromStr>::Err: 's,
{
let value = get_property_numeric::<RV>(emacs, emacs_field)?;
let rust_value = rust_value_getter(rust_node);
if rust_value != value {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
emacs_field, value, rust_value
));
Ok(ComparePropertiesResult::SelfChange(this_status, message))
} else {
Ok(ComparePropertiesResult::NoChange)
}
}
pub(crate) fn compare_property_list_of_quoted_string<
'b,
's,

View File

@ -10,6 +10,7 @@ use super::compare_field::compare_property_always_nil;
use super::compare_field::compare_property_boolean;
use super::compare_field::compare_property_list_of_ast_nodes;
use super::compare_field::compare_property_list_of_quoted_string;
use super::compare_field::compare_property_numeric;
use super::compare_field::compare_property_quoted_string;
use super::compare_field::compare_property_unquoted_atom;
use super::elisp_fact::ElispFact;
@ -594,6 +595,55 @@ fn compare_section<'b, 's>(
.into())
}
fn new_compare_heading<'b, 's>(
source: &'s str,
emacs: &'b Token<'s>,
rust: &'b Heading<'s>,
) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error + 's>> {
let mut this_status = DiffStatus::Good;
let mut child_status = Vec::new();
let mut message = None;
compare_children(
source,
emacs,
&rust.children,
&mut child_status,
&mut this_status,
&mut message,
)?;
for diff in compare_properties!(
source,
emacs,
rust,
(
EmacsField::Required(":level"),
|r| Some(r.level),
compare_property_numeric
)
) {
match diff {
ComparePropertiesResult::NoChange => {}
ComparePropertiesResult::SelfChange(new_status, new_message) => {
this_status = new_status;
message = new_message
}
ComparePropertiesResult::DiffEntry(diff_entry) => child_status.push(diff_entry),
}
}
Ok(DiffResult {
status: this_status,
name: rust.get_elisp_name(),
message,
children: child_status,
rust_source: rust.get_source(),
emacs_token: emacs,
}
.into())
}
fn compare_heading<'b, 's>(
source: &'s str,
emacs: &'b Token<'s>,