Compare the additional properties on headlines.

This commit is contained in:
Tom Alexander 2023-10-10 17:40:27 -04:00
parent 6b90a9bfa8
commit 7cb71a5a0a
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 63 additions and 1 deletions

View File

@ -32,6 +32,26 @@ pub(crate) enum ComparePropertiesResult<'b, 's> {
DiffEntry(DiffEntry<'b, 's>),
}
impl<'b, 's> ComparePropertiesResult<'b, 's> {
pub(crate) fn apply(
self,
child_status: &mut Vec<DiffEntry<'b, 's>>,
this_status: &mut DiffStatus,
message: &mut Option<String>,
) {
match self {
ComparePropertiesResult::NoChange => {}
ComparePropertiesResult::SelfChange(new_status, new_message) => {
*this_status = new_status;
*message = new_message
}
ComparePropertiesResult::DiffEntry(diff_entry) => child_status.push(diff_entry),
}
// foo
}
}
/// 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.

View File

@ -21,6 +21,7 @@ use super::elisp_fact::GetElispFact;
use super::sexp::unquote;
use super::sexp::Token;
use super::util::assert_no_children;
use super::util::compare_additional_properties;
use super::util::compare_children;
use super::util::compare_children_iter;
use super::util::compare_standard_properties;
@ -537,12 +538,27 @@ fn compare_heading<'b, 's>(
let mut child_status = Vec::new();
let mut message = None;
// TODO: Add checks to compare values for additional properties.
let additional_property_names: Vec<String> = rust
.get_additional_properties()
.map(|node_property| format!(":{}", node_property.property_name.to_uppercase()))
.collect();
let additional_properties: Vec<(String, &str)> = rust
.get_additional_properties()
.map(|node_property| {
(
format!(":{}", node_property.property_name.to_uppercase()),
node_property.value.unwrap_or(""),
)
})
.collect();
compare_additional_properties(emacs, additional_properties.into_iter())?.apply(
&mut child_status,
&mut this_status,
&mut message,
);
compare_children(
source,
emacs,

View File

@ -1,5 +1,6 @@
use std::str::FromStr;
use super::compare_field::ComparePropertiesResult;
use super::diff::DiffEntry;
use super::diff::DiffStatus;
use super::elisp_fact::GetElispFact;
@ -311,3 +312,28 @@ pub(crate) fn assert_no_children<'b, 's>(
}
Ok(())
}
pub(crate) fn compare_additional_properties<'b, 's, RK, RV, RI>(
emacs: &'b Token<'s>,
rust_children: RI,
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>>
where
RK: AsRef<str>,
RV: AsRef<str>,
RI: Iterator<Item = (RK, RV)> + ExactSizeIterator,
{
for (rust_key, rust_value) in rust_children {
let rust_key = rust_key.as_ref();
let rust_value = rust_value.as_ref();
let emacs_value = get_property_quoted_string(emacs, rust_key)?;
if Some(rust_value) != emacs_value.as_ref().map(String::as_str) {
let this_status = DiffStatus::Bad;
let message = Some(format!(
"{} mismatch (emacs != rust) {:?} != {:?}",
rust_key, emacs_value, rust_value
));
return Ok(ComparePropertiesResult::SelfChange(this_status, message));
}
}
Ok(ComparePropertiesResult::NoChange)
}