Compare commits

..

No commits in common. "fb412bc2cd8794175b5b1bf5f3eaa5f486ab893d" and "360b2d963d448eb738b7d6e2ac9a09efd041ccf3" have entirely different histories.

3 changed files with 85 additions and 79 deletions

View File

@ -23,7 +23,6 @@ use super::sexp::unquote;
use super::sexp::Token; use super::sexp::Token;
use super::util::assert_no_children; use super::util::assert_no_children;
use super::util::compare_children; use super::util::compare_children;
use super::util::compare_children_iter;
use super::util::compare_standard_properties; use super::util::compare_standard_properties;
use super::util::get_property; use super::util::get_property;
use super::util::get_property_boolean; use super::util::get_property_boolean;
@ -439,45 +438,99 @@ pub fn compare_document<'b, 's>(
} }
fn _compare_document<'b, 's>( fn _compare_document<'b, 's>(
source: &'s str, _source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
rust: &'b Document<'s>, rust: &'b Document<'s>,
) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<DiffEntry<'b, 's>, Box<dyn std::error::Error>> {
let mut this_status = DiffStatus::Good; let children = emacs.as_list()?;
let mut child_status = Vec::new(); let mut child_status = Vec::new();
let mut this_status = DiffStatus::Good;
let mut message = None; let mut message = None;
compare_children_iter( // Compare :path
source, // :path is a quoted string to the absolute path of the document.
emacs, let document_path = get_property_quoted_string(emacs, ":path")?;
rust.into_iter(), let rust_document_path = rust.path.as_ref().map(|p| p.to_str()).flatten();
&mut child_status, match (
&mut this_status, document_path.as_ref().map(|s| s.as_str()),
&mut message, rust_document_path,
)?;
for diff in compare_properties!(
source,
emacs,
rust,
(
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 { (None, None) => {}
ComparePropertiesResult::NoChange => {} (None, Some(_)) | (Some(_), None) => {
ComparePropertiesResult::SelfChange(new_status, new_message) => { this_status = DiffStatus::Bad;
this_status = new_status; message = Some(format!(
message = new_message "Path mismatch (emacs != rust) {:?} != {:?}",
document_path, rust_document_path
));
} }
ComparePropertiesResult::DiffEntry(diff_entry) => child_status.push(diff_entry), (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());
} }
} }

View File

@ -284,33 +284,6 @@ where
Ok(()) Ok(())
} }
pub(crate) fn compare_children_iter<'b, 's, RC, RI: Iterator<Item = RC> + ExactSizeIterator>(
source: &'s str,
emacs: &'b Token<'s>,
rust_children: RI,
child_status: &mut Vec<DiffEntry<'b, 's>>,
this_status: &mut DiffStatus,
message: &mut Option<String>,
) -> Result<(), Box<dyn std::error::Error>>
where
AstNode<'b, 's>: From<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>( pub(crate) fn assert_no_children<'b, 's>(
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
this_status: &mut DiffStatus, this_status: &mut DiffStatus,

View File

@ -11,14 +11,7 @@ macro_rules! children_iter {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.next.next().map(Into::<AstNode>::into) self.next.next().map(Into::<AstNode>::into)
} }
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.next.len();
(size, Some(size))
} }
}
impl<'r, 's> ExactSizeIterator for $itertype<'r, 's> {}
impl<'r, 's> IntoIterator for &'r $astnodetype { impl<'r, 's> IntoIterator for &'r $astnodetype {
type Item = AstNode<'r, 's>; type Item = AstNode<'r, 's>;
@ -49,13 +42,7 @@ macro_rules! empty_iter {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
None None
} }
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(0))
} }
}
impl<'r, 's> ExactSizeIterator for $itertype<'r, 's> {}
impl<'r, 's> IntoIterator for &'r $astnodetype { impl<'r, 's> IntoIterator for &'r $astnodetype {
type Item = AstNode<'r, 's>; type Item = AstNode<'r, 's>;
@ -92,14 +79,7 @@ $fieldname: $innertype,
.or_else(|| self.$fieldname.next().map(Into::<AstNode>::into)) .or_else(|| self.$fieldname.next().map(Into::<AstNode>::into))
),* ),*
} }
fn size_hint(&self) -> (usize, Option<usize>) {
let size = self.$firstfieldname.len()$( + self.$fieldname.len() ),*;
(size, Some(size))
} }
}
impl<'r, 's> ExactSizeIterator for $itertype<'r, 's> {}
impl<'r, 's> IntoIterator for &'r $astnodetype { impl<'r, 's> IntoIterator for &'r $astnodetype {
type Item = AstNode<'r, 's>; type Item = AstNode<'r, 's>;