comparing begin and end offsets for top-level sections and headlines.

This commit is contained in:
Tom Alexander
2023-04-11 19:16:04 -04:00
parent 276e8abb13
commit 52b401d548
6 changed files with 109 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
use super::sexp::Token;
use crate::compare::util::get_offsets;
use crate::parser::Document;
use crate::parser::Section;
use crate::parser::Heading;
#[derive(Debug)]
pub struct DiffResult {
@@ -46,7 +48,7 @@ pub fn compare_document<'s>(
return Err("Document should correspond to an org-data cell.".into());
}
let mut child_status = Vec::new();
// TODO: compare the children
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() {
@@ -72,14 +74,14 @@ pub fn compare_document<'s>(
.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, rust)?);
child_status.push(compare_heading(rust.source, token, corresponding_heading)?);
} else {
return Err("Document should only contain sections and headlines.".into());
}
}
Ok(DiffResult {
status: DiffStatus::Good,
status: this_status,
name: "document".to_owned(),
children: child_status,
})
@@ -97,9 +99,28 @@ pub fn compare_section<'s>(
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;
}
Ok(DiffResult {
status: DiffStatus::Good,
status: this_status,
name: "section".to_owned(),
children: child_status,
})
@@ -108,7 +129,7 @@ pub fn compare_section<'s>(
pub fn compare_heading<'s>(
source: &'s str,
emacs: &'s Token<'s>,
rust: &'s Document<'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.")?;
@@ -117,9 +138,28 @@ pub fn compare_heading<'s>(
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;
}
Ok(DiffResult {
status: DiffStatus::Good,
status: this_status,
name: "heading".to_owned(),
children: child_status,
})