From 585b1d2b74d765d96ce5422f1b0b140cf6463c48 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Tue, 10 Oct 2023 15:30:27 -0400 Subject: [PATCH] compare_properties for document. --- src/compare/diff.rs | 124 +++----------------------------------------- 1 file changed, 8 insertions(+), 116 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 838b0e2..911d192 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -460,15 +460,15 @@ fn _compare_document<'b, 's>( source, emacs, rust, - // ( - // EmacsField::Optional(":name"), - // |r| r.name, - // compare_property_quoted_string - // ), ( - EmacsField::Optional(":caption"), - compare_identity, - compare_noop + EmacsField::Required(":path"), + |r| r.path.as_ref().map(|p| p.to_str()).flatten(), + compare_property_quoted_string + ), + ( + EmacsField::Required(":CATEGORY"), + |r| r.category.as_ref(), + compare_property_quoted_string ) ) { match diff { @@ -492,114 +492,6 @@ fn _compare_document<'b, 's>( .into()) } -fn _old_compare_document<'b, 's>( - _source: &'s str, - emacs: &'b Token<'s>, - rust: &'b Document<'s>, -) -> Result, Box> { - let children = emacs.as_list()?; - let mut child_status = Vec::new(); - let mut this_status = DiffStatus::Good; - let mut message = None; - - // Compare :path - // :path is a quoted string to the absolute path of the document. - let document_path = get_property_quoted_string(emacs, ":path")?; - let rust_document_path = rust.path.as_ref().map(|p| p.to_str()).flatten(); - match ( - document_path.as_ref().map(|s| s.as_str()), - rust_document_path, - ) { - (None, None) => {} - (None, Some(_)) | (Some(_), None) => { - this_status = DiffStatus::Bad; - message = Some(format!( - "Path mismatch (emacs != rust) {:?} != {:?}", - document_path, rust_document_path - )); - } - (Some(e), Some(r)) if e != r => { - this_status = DiffStatus::Bad; - message = Some(format!( - "Path mismatch (emacs != rust) {:?} != {:?}", - document_path, rust_document_path - )); - } - (Some(_), Some(_)) => {} - }; - - // Compare category - // :CATEGORY is specified either from "#+CATEGORY:" or it is the file name without the ".org" extension. - let category = get_property_quoted_string(emacs, ":CATEGORY")?; - match (category.as_ref(), rust.category.as_ref()) { - (None, None) => {} - (None, Some(_)) | (Some(_), None) => { - this_status = DiffStatus::Bad; - message = Some(format!( - "Category mismatch (emacs != rust) {:?} != {:?}", - category, rust.category - )); - } - (Some(e), Some(r)) if e != r => { - this_status = DiffStatus::Bad; - message = Some(format!( - "Category mismatch (emacs != rust) {:?} != {:?}", - category, rust.category - )); - } - (Some(_), Some(_)) => {} - }; - - // Skipping "org-data" and its properties - for (i, token) in children.iter().skip(2).enumerate() { - let section_or_headline = token.as_list()?; - let first_cell = section_or_headline - .first() - .ok_or("Should have at least one child.")? - .as_atom()?; - if first_cell == "section" { - if i != 0 { - return Err("Section cannot be after the first child of document.".into()); - } - child_status.push(compare_ast_node( - rust.source, - token, - rust.zeroth_section - .as_ref() - .ok_or("No corresponding zeroth-section")? - .into(), - )?); - } else if first_cell == "headline" { - let corresponding_heading = rust - .children - .iter() - .nth(i - rust.zeroth_section.as_ref().map(|_| 1).unwrap_or(0)) - .ok_or("Should have a corresponding heading.")?; - child_status.push(compare_ast_node( - rust.source, - token, - corresponding_heading.into(), - )?); - } else { - return Err(format!( - "Document should only contain sections and headlines, found: {}", - first_cell - ) - .into()); - } - } - - Ok(DiffResult { - status: this_status, - name: rust.get_elisp_name(), - message, - children: child_status, - rust_source: rust.get_source(), - emacs_token: emacs, - } - .into()) -} - fn compare_section<'b, 's>( source: &'s str, emacs: &'b Token<'s>,