compare_properties plain list item.

This commit is contained in:
Tom Alexander 2023-10-09 21:54:29 -04:00
parent 409a92333e
commit f543caee00
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 62 additions and 99 deletions

View File

@ -0,0 +1,3 @@
- [ ] Foo
- [-] Bar
- [X] Baz

View File

@ -83,8 +83,6 @@ use crate::types::Paragraph;
use crate::types::PlainLink; use crate::types::PlainLink;
use crate::types::PlainList; use crate::types::PlainList;
use crate::types::PlainListItem; use crate::types::PlainListItem;
use crate::types::PlainListItemCounter;
use crate::types::PlainListItemPreBlank;
use crate::types::PlainListType; use crate::types::PlainListType;
use crate::types::PlainText; use crate::types::PlainText;
use crate::types::Planning; use crate::types::Planning;
@ -1121,108 +1119,70 @@ fn compare_plain_list_item<'b, 's>(
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
rust: &'b PlainListItem<'s>, rust: &'b PlainListItem<'s>,
) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error + 's>> {
let children = emacs.as_list()?;
let mut child_status = Vec::new();
let mut this_status = DiffStatus::Good; let mut this_status = DiffStatus::Good;
let mut child_status = Vec::new();
let mut message = None; let mut message = None;
// Compare tag compare_children(
let tag = get_property(emacs, ":tag")?; source,
match (tag, rust.tag.is_empty()) { emacs,
(None, true) => {} &rust.children,
(None, false) | (Some(_), true) => { &mut child_status,
this_status = DiffStatus::Bad; &mut this_status,
message = Some("Mismatched tags".to_owned()); &mut message,
} )?;
(Some(tag), false) => {
let tag_status = tag
.as_list()?
.iter()
.zip(rust.tag.iter())
.map(|(emacs_child, rust_child)| {
compare_ast_node(source, emacs_child, rust_child.into())
})
.collect::<Result<Vec<_>, _>>()?;
child_status.push(artificial_diff_scope("tag", tag_status)?);
}
};
// Compare contents for diff in compare_properties!(
let contents_status = children source,
.iter() emacs,
.skip(2) rust,
.zip(rust.children.iter()) (
.map(|(emacs_child, rust_child)| compare_ast_node(source, emacs_child, rust_child.into())) EmacsField::Required(":tag"),
.collect::<Result<Vec<_>, _>>()?; |r| if r.tag.is_empty() {
child_status.push(artificial_diff_scope("contents", contents_status)?); None
} else {
// Compare bullet Some(r.tag.iter())
let bullet = get_property_quoted_string(emacs, ":bullet")? },
.ok_or("Plain list items must have a :bullet.")?; compare_property_list_of_ast_nodes
if bullet != rust.bullet { ),
this_status = DiffStatus::Bad; (
message = Some(format!( EmacsField::Required(":bullet"),
"Bullet mismatch (emacs != rust) {:?} != {:?}", |r| Some(r.bullet),
bullet, rust.bullet compare_property_quoted_string
)); ),
} (
EmacsField::Required(":counter"),
// Compare counter |r| r.counter,
let counter = get_property_unquoted_atom(emacs, ":counter")?; compare_property_numeric
let counter: Option<PlainListItemCounter> = counter ),
.map(|val| val.parse()) (
.map_or(Ok(None), |r| r.map(Some))?; EmacsField::Required(":checkbox"),
match (counter, rust.counter) { |r| r
(None, None) => {} .checkbox
(None, Some(_)) | (Some(_), None) => { .as_ref()
this_status = DiffStatus::Bad; .map(|(checkbox_type, _)| checkbox_type)
message = Some(format!( .map(|checkbox_type| match checkbox_type {
"Counter mismatch (emacs != rust) {:?} != {:?}", CheckboxType::On => "on",
counter, rust.counter CheckboxType::Trans => "trans",
)); CheckboxType::Off => "off",
}),
compare_property_unquoted_atom
),
(
EmacsField::Required(":pre-blank"),
|r| Some(r.pre_blank),
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),
} }
(Some(e), Some(r)) if e != r => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Counter mismatch (emacs != rust) {:?} != {:?}",
counter, rust.counter
));
}
(Some(_), Some(_)) => {}
};
// Compare checkbox
let checkbox = get_property(emacs, ":checkbox")?
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.unwrap_or("nil");
match (checkbox, &rust.checkbox) {
("nil", None) => {}
("off", Some((CheckboxType::Off, _))) => {}
("trans", Some((CheckboxType::Trans, _))) => {}
("on", Some((CheckboxType::On, _))) => {}
_ => {
this_status = DiffStatus::Bad;
message = Some(format!(
"Checkbox mismatch (emacs != rust) {:?} != {:?}",
checkbox, rust.checkbox
));
}
};
// Compare pre-blank
// :pre-blank appears to count the line breaks between "::" and the contents in a descriptive list. Oddly enough it does not count the spaces so I'm not quite sure what the value is.
let pre_blank = get_property_unquoted_atom(emacs, ":pre-blank")?;
let pre_blank: Option<PlainListItemPreBlank> = pre_blank
.map(|val| val.parse())
.map_or(Ok(None), |r| r.map(Some))?;
if pre_blank.unwrap_or(0) != rust.pre_blank {
this_status = DiffStatus::Bad;
message = Some(format!(
"Pre-blank mismatch (emacs != rust) {:?} != {:?}",
pre_blank, rust.pre_blank
));
} }
Ok(DiffResult { Ok(DiffResult {