diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 3e69a6ad..415082e8 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -4,6 +4,8 @@ use crate::parser::Document; use crate::parser::DocumentElement; use crate::parser::Heading; use crate::parser::Section; +use crate::parser::Paragraph; +use crate::parser::Element; #[derive(Debug)] pub struct DiffResult { @@ -132,6 +134,10 @@ pub fn compare_section<'s>( this_status = DiffStatus::Bad; } + for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) { + child_status.push(compare_element(source, emacs_child, rust_child)?); + } + Ok(DiffResult { status: this_status, name: "section".to_owned(), @@ -188,3 +194,57 @@ pub fn compare_heading<'s>( children: child_status, }) } + +pub fn compare_element<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s Element<'s>, +) -> Result> { + match rust { + Element::Paragraph(obj) => compare_paragraph(source, emacs, obj), + Element::PlainList(_) => todo!(), + Element::GreaterBlock(_) => todo!(), + Element::FootnoteDefinition(_) => todo!(), + } +} + +pub fn compare_paragraph<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s Paragraph<'s>, +) -> Result> { + let children = emacs.as_list()?; + let first_child = children.first().ok_or("Should have at least one child.")?.as_atom()?; + if first_child != "paragraph" { + return Err("Paragraph should correspond to a paragraph cell.".into()); + } + let mut child_status = Vec::new(); + let mut this_status = DiffStatus::Good; + + let attributes_child = children + .iter() + .nth(1) + .ok_or("Should have an attributes child.")?; + let attributes_map = attributes_child.as_map()?; + let begin = attributes_map + .get(":begin") + .ok_or("Missing :begin attribute.")? + .as_atom()?; + let end = attributes_map + .get(":end") + .ok_or("Missing :end attribute.")? + .as_atom()?; + let (rust_begin, rust_end) = get_offsets(source, rust); + if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + this_status = DiffStatus::Bad; + } + + for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) { + } + + Ok(DiffResult { + status: this_status, + name: "paragraph".to_owned(), + children: child_status, + }) +} diff --git a/src/parser/element.rs b/src/parser/element.rs index f92a1069..9a2d189a 100644 --- a/src/parser/element.rs +++ b/src/parser/element.rs @@ -32,6 +32,12 @@ impl<'s> Source<'s> for Element<'s> { } } +impl<'s> Source<'s> for Paragraph<'s> { + fn get_source(&'s self) -> &'s str { + self.source + } +} + #[tracing::instrument(ret, level = "debug")] pub fn element<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Element<'s>> { let non_paragraph_matcher = parser_with_context!(non_paragraph_element)(context); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9c810ce9..c9663316 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -19,5 +19,7 @@ pub use document::Document; pub use document::DocumentElement; pub use document::Heading; pub use document::Section; +pub use element::Element; +pub use lesser_element::Paragraph; pub use source::Source; type Context<'r, 's> = &'r parser_context::ContextTree<'r, 's>;