diff --git a/src/compare/diff.rs b/src/compare/diff.rs index f8eada8..154bf31 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -23,6 +23,7 @@ use super::sexp::unquote; use super::sexp::Token; use super::util::assert_no_children; use super::util::compare_children; +use super::util::compare_children_iter; use super::util::compare_standard_properties; use super::util::get_property; use super::util::get_property_boolean; @@ -438,6 +439,63 @@ pub fn compare_document<'b, 's>( } fn _compare_document<'b, 's>( + source: &'s str, + emacs: &'b Token<'s>, + rust: &'b Document<'s>, +) -> Result, Box> { + let mut this_status = DiffStatus::Good; + let mut child_status = Vec::new(); + let mut message = None; + + // compare_children_iter( + // source, + // emacs, + // rust.zeroth_section + // .iter() + // .map(Into::::into) + // .chain(rust.children.iter().map(Into::::into)), + // &mut child_status, + // &mut this_status, + // &mut message, + // )?; + + for diff in compare_properties!( + source, + emacs, + rust, + // ( + // EmacsField::Optional(":name"), + // |r| r.name, + // compare_property_quoted_string + // ), + ( + EmacsField::Optional(":caption"), + compare_identity, + compare_noop + ) + ) { + 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 _old_compare_document<'b, 's>( _source: &'s str, emacs: &'b Token<'s>, rust: &'b Document<'s>, diff --git a/src/compare/util.rs b/src/compare/util.rs index 28f7bad..e3e02d5 100644 --- a/src/compare/util.rs +++ b/src/compare/util.rs @@ -284,6 +284,39 @@ where Ok(()) } +pub(crate) fn compare_children_iter< + 'b, + 's, + 'x, + RC: 'x, + RI: Iterator + ExactSizeIterator, +>( + source: &'s str, + emacs: &'b Token<'s>, + rust_children: RI, + child_status: &mut Vec>, + this_status: &mut DiffStatus, + message: &mut Option, +) -> Result<(), Box> +where + AstNode<'b, 's>: From<&'x RC>, +{ + let emacs_children = emacs.as_list()?; + let emacs_children_length = emacs_children.len() - 2; + if emacs_children_length != rust_children.len() { + *this_status = DiffStatus::Bad; + *message = Some(format!( + "Child length mismatch (emacs != rust) {:?} != {:?}", + emacs_children_length, + rust_children.len() + )); + } + for (emacs_child, rust_child) in emacs_children.iter().skip(2).zip(rust_children) { + child_status.push(compare_ast_node(source, emacs_child, rust_child.into())?); + } + Ok(()) +} + pub(crate) fn assert_no_children<'b, 's>( emacs: &'b Token<'s>, this_status: &mut DiffStatus,