Compare children of sections.

This commit is contained in:
Tom Alexander 2023-04-12 11:46:49 -04:00
parent c4e6549feb
commit 2a601475fd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 68 additions and 0 deletions

View File

@ -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<DiffResult, Box<dyn std::error::Error>> {
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<DiffResult, Box<dyn std::error::Error>> {
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,
})
}

View File

@ -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);

View File

@ -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>;