Compare table formulas.

This commit is contained in:
Tom Alexander 2023-09-08 23:05:04 -04:00
parent 84d2babda9
commit 80f7098f9b
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 42 additions and 4 deletions

View File

@ -1,3 +1,4 @@
use std::collections::BTreeSet;
use std::collections::HashSet;
use super::util::assert_bounds;
@ -491,7 +492,7 @@ fn compare_heading<'s>(
if rust.stars.to_string() != level {
this_status = DiffStatus::Bad;
message = Some(format!(
"Headline level do not much (emacs != rust): {} != {}",
"Headline level do not match (emacs != rust): {} != {}",
level, rust.stars
))
}
@ -1075,9 +1076,43 @@ fn compare_table<'s>(
Ok(_) => {}
};
// TODO: Compare :type :tblfm :value
// Compare formulas
//
// :tblfm is a list () filled with quoted strings containing the value for any tblfm keywords at the end of the table.
// :tblfm is either nil or a list () filled with quoted strings containing the value for any tblfm keywords at the end of the table.
let emacs_formulas = get_property(emacs, ":tblfm")?;
if let Some(emacs_formulas) = emacs_formulas {
let emacs_formulas = emacs_formulas.as_list()?;
if emacs_formulas.len() != rust.formulas.len() {
this_status = DiffStatus::Bad;
message = Some(format!(
"Formulas do not match (emacs != rust): {:?} != {:?}",
emacs_formulas, rust.formulas
))
} else {
let atoms = emacs_formulas
.into_iter()
.map(Token::as_atom)
.collect::<Result<Vec<_>, _>>()?;
let unquoted = atoms
.into_iter()
.map(unquote)
.collect::<Result<BTreeSet<_>, _>>()?;
for kw in &rust.formulas {
if !unquoted.contains(kw.value) {
this_status = DiffStatus::Bad;
message = Some(format!("Could not find formula in emacs: {}", kw.value))
}
}
}
} else {
if !rust.formulas.is_empty() {
this_status = DiffStatus::Bad;
message = Some(format!(
"Formulas do not match (emacs != rust): {:?} != {:?}",
emacs_formulas, rust.formulas
))
}
}
for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) {
child_status.push(compare_table_row(source, emacs_child, rust_child)?);

View File

@ -58,7 +58,7 @@ pub fn org_mode_table<'b, 'g, 'r, 's>(
let (remaining, (children, _exit_contents)) =
many_till(org_mode_table_row_matcher, exit_matcher)(input)?;
let (remaining, _formulas) =
let (remaining, formulas) =
many0(parser_with_context!(table_formula_keyword)(context))(remaining)?;
// TODO: Consume trailing formulas
@ -68,6 +68,7 @@ pub fn org_mode_table<'b, 'g, 'r, 's>(
remaining,
Table {
source: source.into(),
formulas,
children,
},
))

View File

@ -1,5 +1,6 @@
use super::element::Element;
use super::lesser_element::TableCell;
use super::Keyword;
use super::Object;
use super::Source;
@ -63,6 +64,7 @@ pub struct NodeProperty<'s> {
#[derive(Debug)]
pub struct Table<'s> {
pub source: &'s str,
pub formulas: Vec<Keyword<'s>>,
pub children: Vec<TableRow<'s>>,
}