Update compare_properties to handle affiliated keywords.
This commit is contained in:
parent
78a9b93f22
commit
aa33fe42a8
@ -431,6 +431,7 @@ pub(crate) fn compare_property_list_of_list_of_list_of_ast_nodes<
|
|||||||
emacs_field: &'x str,
|
emacs_field: &'x str,
|
||||||
rust_value_getter: RG,
|
rust_value_getter: RG,
|
||||||
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
) -> Result<ComparePropertiesResult<'b, 's>, Box<dyn std::error::Error>> {
|
||||||
|
// TODO: Replace Object<'s> with generics. I hard-coded Object in to make lifetimes easier.
|
||||||
let rust_value = rust_value_getter(rust_node);
|
let rust_value = rust_value_getter(rust_node);
|
||||||
let value = get_property(emacs, emacs_field)?
|
let value = get_property(emacs, emacs_field)?
|
||||||
.map(Token::as_list)
|
.map(Token::as_list)
|
||||||
|
@ -768,19 +768,11 @@ fn compare_plain_list<'b, 's>(
|
|||||||
&mut message,
|
&mut message,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
for diff in compare_affiliated_keywords(source, emacs, rust)? {
|
|
||||||
diff.apply(&mut child_status, &mut this_status, &mut message);
|
|
||||||
}
|
|
||||||
let affiliated_keywords_names: Vec<String> = affiliated_keywords_names(rust).collect();
|
|
||||||
|
|
||||||
for diff in compare_properties!(
|
for diff in compare_properties!(
|
||||||
source,
|
source,
|
||||||
emacs,
|
emacs,
|
||||||
rust,
|
rust,
|
||||||
affiliated_keywords_names
|
[],
|
||||||
.iter()
|
|
||||||
.map(String::as_str)
|
|
||||||
.map(EmacsField::Required),
|
|
||||||
(
|
(
|
||||||
EmacsField::Optional(":name"),
|
EmacsField::Optional(":name"),
|
||||||
|r| r.name,
|
|r| r.name,
|
||||||
|
@ -96,6 +96,95 @@ macro_rules! compare_properties {
|
|||||||
new_status
|
new_status
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
// For elements with affiliated keywords
|
||||||
|
($source:expr, $emacs:expr, $rust:expr, [], $(($emacs_field:expr, $rust_value_getter:expr, $compare_fn: expr)),+) => {
|
||||||
|
{
|
||||||
|
let mut new_status = Vec::new();
|
||||||
|
let children = $emacs.as_list()?;
|
||||||
|
let attributes_child = children
|
||||||
|
.iter()
|
||||||
|
.nth(1)
|
||||||
|
.ok_or("Should have an attributes child.")?;
|
||||||
|
let attributes_map = attributes_child.as_map()?;
|
||||||
|
let mut emacs_keys: BTreeSet<&str> = attributes_map.keys().map(|s| *s).collect();
|
||||||
|
if emacs_keys.contains(":standard-properties") {
|
||||||
|
emacs_keys.remove(":standard-properties");
|
||||||
|
} else {
|
||||||
|
new_status.push(ComparePropertiesResult::SelfChange(DiffStatus::Bad, Some(format!(
|
||||||
|
"Emacs token lacks :standard-properties field.",
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
let affiliated_keywords_names: Vec<String> = affiliated_keywords_names($rust).collect();
|
||||||
|
for additional_field in affiliated_keywords_names.iter().map(String::as_str).map(EmacsField::Required) {
|
||||||
|
match additional_field {
|
||||||
|
EmacsField::Required(name) if emacs_keys.contains(name) => {
|
||||||
|
emacs_keys.remove(name);
|
||||||
|
},
|
||||||
|
EmacsField::Optional(name) if emacs_keys.contains(name) => {
|
||||||
|
emacs_keys.remove(name);
|
||||||
|
},
|
||||||
|
EmacsField::Required(name) => {
|
||||||
|
new_status.push(ComparePropertiesResult::SelfChange(DiffStatus::Bad, Some(format!(
|
||||||
|
"Emacs token lacks required field: {}",
|
||||||
|
name
|
||||||
|
))));
|
||||||
|
},
|
||||||
|
EmacsField::Optional(_name) => {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$(
|
||||||
|
match $emacs_field {
|
||||||
|
EmacsField::Required(name) if emacs_keys.contains(name) => {
|
||||||
|
emacs_keys.remove(name);
|
||||||
|
},
|
||||||
|
EmacsField::Optional(name) if emacs_keys.contains(name) => {
|
||||||
|
emacs_keys.remove(name);
|
||||||
|
},
|
||||||
|
EmacsField::Required(name) => {
|
||||||
|
new_status.push(ComparePropertiesResult::SelfChange(DiffStatus::Bad, Some(format!(
|
||||||
|
"Emacs token lacks required field: {}",
|
||||||
|
name
|
||||||
|
))));
|
||||||
|
},
|
||||||
|
EmacsField::Optional(_name) => {},
|
||||||
|
}
|
||||||
|
)+
|
||||||
|
|
||||||
|
if !emacs_keys.is_empty() {
|
||||||
|
let unexpected_keys: Vec<&str> = emacs_keys.into_iter().collect();
|
||||||
|
let unexpected_keys = unexpected_keys.join(", ");
|
||||||
|
new_status.push(ComparePropertiesResult::SelfChange(DiffStatus::Bad, Some(format!(
|
||||||
|
"Emacs token had extra field(s): {}",
|
||||||
|
unexpected_keys
|
||||||
|
))));
|
||||||
|
}
|
||||||
|
|
||||||
|
for diff in compare_affiliated_keywords($source, $emacs, $rust)? {
|
||||||
|
new_status.push(diff);
|
||||||
|
}
|
||||||
|
|
||||||
|
$(
|
||||||
|
let emacs_name = match $emacs_field {
|
||||||
|
EmacsField::Required(name) => {
|
||||||
|
name
|
||||||
|
},
|
||||||
|
EmacsField::Optional(name) => {
|
||||||
|
name
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let result = $compare_fn($source, $emacs, $rust, emacs_name, $rust_value_getter)?;
|
||||||
|
match result {
|
||||||
|
ComparePropertiesResult::SelfChange(DiffStatus::Good, _) => unreachable!("No comparison functions should return SelfChange() when DiffStatus is good."),
|
||||||
|
ComparePropertiesResult::NoChange => {},
|
||||||
|
result => {
|
||||||
|
new_status.push(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)+
|
||||||
|
|
||||||
|
new_status
|
||||||
|
}
|
||||||
|
};
|
||||||
// Specifies additional properties
|
// Specifies additional properties
|
||||||
($source:expr, $emacs:expr, $rust:expr, $additionalproperties: expr, $(($emacs_field:expr, $rust_value_getter:expr, $compare_fn: expr)),+) => {
|
($source:expr, $emacs:expr, $rust:expr, $additionalproperties: expr, $(($emacs_field:expr, $rust_value_getter:expr, $compare_fn: expr)),+) => {
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user