Add diff code for dynamic blocks.

This commit is contained in:
Tom Alexander 2023-04-19 13:59:17 -04:00
parent cab5ba70e5
commit ed8cdae1cd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -12,6 +12,7 @@ use crate::parser::Paragraph;
use crate::parser::PlainList;
use crate::parser::PlainListItem;
use crate::parser::Section;
use crate::DynamicBlock;
#[derive(Debug)]
pub struct DiffResult {
@ -217,7 +218,7 @@ fn compare_element<'s>(
Element::Paragraph(obj) => compare_paragraph(source, emacs, obj),
Element::PlainList(obj) => compare_plain_list(source, emacs, obj),
Element::GreaterBlock(obj) => compare_greater_block(source, emacs, obj),
Element::DynamicBlock(obj) => todo!(),
Element::DynamicBlock(obj) => compare_dynamic_block(source, emacs, obj),
Element::FootnoteDefinition(obj) => compare_footnote_definition(source, emacs, obj),
Element::Comment(obj) => compare_comment(source, emacs, obj),
Element::Drawer(obj) => compare_drawer(source, emacs, obj),
@ -414,6 +415,51 @@ fn compare_greater_block<'s>(
})
}
fn compare_dynamic_block<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s DynamicBlock<'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 != "dynamic-block" {
return Err("Dynamic block should correspond to a dynamic-block 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()) {
child_status.push(compare_element(source, emacs_child, rust_child)?);
}
Ok(DiffResult {
status: this_status,
name: "dynamic-block".to_owned(),
children: child_status,
})
}
fn compare_footnote_definition<'s>(
source: &'s str,
emacs: &'s Token<'s>,