546 lines
17 KiB
Rust
546 lines
17 KiB
Rust
use super::sexp::Token;
|
|
use crate::compare::util::get_offsets;
|
|
use crate::parser::Comment;
|
|
use crate::parser::Document;
|
|
use crate::parser::DocumentElement;
|
|
use crate::parser::Element;
|
|
use crate::parser::FootnoteDefinition;
|
|
use crate::parser::GreaterBlock;
|
|
use crate::parser::Heading;
|
|
use crate::parser::Paragraph;
|
|
use crate::parser::PlainList;
|
|
use crate::parser::PlainListItem;
|
|
use crate::parser::Section;
|
|
use crate::parser::Drawer;
|
|
|
|
#[derive(Debug)]
|
|
pub struct DiffResult {
|
|
status: DiffStatus,
|
|
name: String,
|
|
children: Vec<DiffResult>,
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum DiffStatus {
|
|
Good,
|
|
Bad,
|
|
}
|
|
|
|
impl DiffResult {
|
|
pub fn print(&self) -> Result<(), Box<dyn std::error::Error>> {
|
|
self.print_indented(0)
|
|
}
|
|
|
|
fn print_indented(&self, indentation: usize) -> Result<(), Box<dyn std::error::Error>> {
|
|
let status_text = {
|
|
match self.status {
|
|
DiffStatus::Good => {
|
|
if self.has_bad_children() {
|
|
"BADCHILD"
|
|
} else {
|
|
"GOOD"
|
|
}
|
|
}
|
|
DiffStatus::Bad => "BAD",
|
|
}
|
|
};
|
|
println!("{}{} {}", " ".repeat(indentation), status_text, self.name);
|
|
for child in self.children.iter() {
|
|
child.print_indented(indentation + 1)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn has_bad_children(&self) -> bool {
|
|
self.children
|
|
.iter()
|
|
.any(|child| child.status == DiffStatus::Bad || child.has_bad_children())
|
|
}
|
|
|
|
pub fn is_bad(&self) -> bool {
|
|
match self.status {
|
|
DiffStatus::Good => self.has_bad_children(),
|
|
DiffStatus::Bad => true,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn compare_document<'s>(
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s Document<'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.")?;
|
|
let first_child_text = first_child.as_atom()?;
|
|
if first_child_text != "org-data" {
|
|
return Err("Document should correspond to an org-data cell.".into());
|
|
}
|
|
let mut child_status = Vec::new();
|
|
let mut this_status = DiffStatus::Good;
|
|
|
|
// Skipping "org-data" and the first parameter which is often nil
|
|
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_section(
|
|
rust.source,
|
|
token,
|
|
rust.zeroth_section
|
|
.as_ref()
|
|
.ok_or("No corresponding zeroth-section")?,
|
|
)?);
|
|
} 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_heading(rust.source, token, corresponding_heading)?);
|
|
} else {
|
|
return Err("Document should only contain sections and headlines.".into());
|
|
}
|
|
}
|
|
|
|
Ok(DiffResult {
|
|
status: this_status,
|
|
name: "document".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_section<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s Section<'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.")?;
|
|
let first_child_text = first_child.as_atom()?;
|
|
if first_child_text != "section" {
|
|
return Err("Section should correspond to a section 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: "section".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_heading<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s Heading<'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.")?;
|
|
let first_child_text = first_child.as_atom()?;
|
|
if first_child_text != "headline" {
|
|
return Err("Heading should correspond to a headline 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()) {
|
|
match rust_child {
|
|
DocumentElement::Heading(rust_heading) => {
|
|
child_status.push(compare_heading(source, emacs_child, rust_heading)?);
|
|
}
|
|
DocumentElement::Section(rust_section) => {
|
|
child_status.push(compare_section(source, emacs_child, rust_section)?);
|
|
}
|
|
};
|
|
}
|
|
|
|
Ok(DiffResult {
|
|
status: this_status,
|
|
name: "heading".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
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(obj) => compare_plain_list(source, emacs, obj),
|
|
Element::GreaterBlock(obj) => compare_greater_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),
|
|
}
|
|
}
|
|
|
|
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,
|
|
})
|
|
}
|
|
|
|
fn compare_plain_list<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s PlainList<'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 != "plain-list" {
|
|
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()) {
|
|
child_status.push(compare_plain_list_item(source, emacs_child, rust_child)?);
|
|
}
|
|
|
|
Ok(DiffResult {
|
|
status: this_status,
|
|
name: "plain-list".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_plain_list_item<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s PlainListItem<'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 != "item" {
|
|
return Err("PlainListItem should correspond to an item 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: "plain-list-item".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_greater_block<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s GreaterBlock<'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()?;
|
|
match rust.name.to_lowercase().as_str() {
|
|
"center" => {
|
|
if first_child != "center-block" {
|
|
return Err(
|
|
"Center greater blocks should correspond to a center-block cell.".into(),
|
|
);
|
|
}
|
|
}
|
|
"quote" => {
|
|
if first_child != "quote-block" {
|
|
return Err("Quote greater blocks should correspond to a quote-block cell.".into());
|
|
}
|
|
}
|
|
_ => todo!(),
|
|
}
|
|
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: "greater-block".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_footnote_definition<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s FootnoteDefinition<'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 != "footnote-definition" {
|
|
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()) {
|
|
child_status.push(compare_element(source, emacs_child, rust_child)?);
|
|
}
|
|
|
|
Ok(DiffResult {
|
|
status: this_status,
|
|
name: "footnote-definition".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_comment<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s Comment<'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 != "comment" {
|
|
return Err("Comment should correspond to a comment 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;
|
|
}
|
|
|
|
Ok(DiffResult {
|
|
status: this_status,
|
|
name: "comment".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|
|
|
|
fn compare_drawer<'s>(
|
|
source: &'s str,
|
|
emacs: &'s Token<'s>,
|
|
rust: &'s Drawer<'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 != "drawer" {
|
|
return Err("Drawer should correspond to a drawer 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: "drawer".to_owned(),
|
|
children: child_status,
|
|
})
|
|
}
|