Starting a new macro for comparing fields.
This will help us assert that all fields are tested and that only expected fields are present.
This commit is contained in:
59
src/compare/macros.rs
Normal file
59
src/compare/macros.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
/// Create iterators for ast nodes where it only has to iterate over children
|
||||
macro_rules! compare_properties {
|
||||
($emacs:expr, $($emacs_field:expr),+) => {
|
||||
{
|
||||
let mut this_status = DiffStatus::Good;
|
||||
let mut message: Option<String> = None;
|
||||
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();
|
||||
$(
|
||||
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) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = 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(", ");
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Emacs token had extra field(s): {}",
|
||||
unexpected_keys
|
||||
));
|
||||
}
|
||||
|
||||
$(
|
||||
println!("{:?}", $emacs_field);
|
||||
),+
|
||||
|
||||
match this_status {
|
||||
DiffStatus::Good => {
|
||||
Ok(None)
|
||||
},
|
||||
_ => {
|
||||
Ok(Some((this_status, message)))
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use compare_properties;
|
||||
Reference in New Issue
Block a user