From b3e182d7fefe1ba0cc6dfb3e385c0d1125e34969 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 22 Apr 2023 17:58:16 -0400 Subject: [PATCH] Plain text compare not yet working because the text is quoted from the sexp. --- src/compare/diff.rs | 140 +++++++++++++++++++++++++++++++++----------- src/parser/mod.rs | 4 ++ src/parser/sexp.rs | 11 +++- 3 files changed, 120 insertions(+), 35 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index b68ec67..2343942 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -19,16 +19,20 @@ use crate::parser::Heading; use crate::parser::HorizontalRule; use crate::parser::Keyword; use crate::parser::LatexEnvironment; +use crate::parser::Object; use crate::parser::Paragraph; use crate::parser::PlainList; use crate::parser::PlainListItem; +use crate::parser::PlainText; use crate::parser::Planning; use crate::parser::PropertyDrawer; +use crate::parser::RegularLink; use crate::parser::Section; use crate::parser::SrcBlock; use crate::parser::Table; use crate::parser::TableCell; use crate::parser::TableRow; +use crate::parser::TextMarkup; use crate::parser::VerseBlock; #[derive(Debug)] @@ -64,7 +68,13 @@ impl DiffResult { DiffStatus::Bad => "BAD", } }; - println!("{}{} {}", " ".repeat(indentation), status_text, self.name); + println!( + "{}{} {} {}", + " ".repeat(indentation), + status_text, + self.name, + self.message.as_ref().map(|m| m.as_str()).unwrap_or("") + ); for child in self.children.iter() { child.print_indented(indentation + 1)?; } @@ -85,6 +95,48 @@ impl DiffResult { } } +fn compare_element<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s Element<'s>, +) -> Result> { + 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::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), + Element::PropertyDrawer(obj) => compare_property_drawer(source, emacs, obj), + Element::Table(obj) => compare_table(source, emacs, obj), + Element::VerseBlock(obj) => compare_verse_block(source, emacs, obj), + Element::CommentBlock(obj) => compare_comment_block(source, emacs, obj), + Element::ExampleBlock(obj) => compare_example_block(source, emacs, obj), + Element::ExportBlock(obj) => compare_export_block(source, emacs, obj), + Element::SrcBlock(obj) => compare_src_block(source, emacs, obj), + Element::Clock(obj) => compare_clock(source, emacs, obj), + Element::DiarySexp(obj) => compare_diary_sexp(source, emacs, obj), + Element::Planning(obj) => compare_planning(source, emacs, obj), + Element::FixedWidthArea(obj) => compare_fixed_width_area(source, emacs, obj), + Element::HorizontalRule(obj) => compare_horizontal_rule(source, emacs, obj), + Element::Keyword(obj) => compare_keyword(source, emacs, obj), + Element::LatexEnvironment(obj) => compare_latex_environment(source, emacs, obj), + } +} + +fn compare_object<'s>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s Object<'s>, +) -> Result> { + match rust { + Object::TextMarkup(obj) => compare_text_markup(source, emacs, obj), + Object::PlainText(obj) => compare_plain_text(source, emacs, obj), + Object::RegularLink(obj) => compare_regular_link(source, emacs, obj), + } +} + pub fn compare_document<'s>( emacs: &'s Token<'s>, rust: &'s Document<'s>, @@ -200,43 +252,13 @@ fn compare_heading<'s>( }) } -fn compare_element<'s>( - source: &'s str, - emacs: &'s Token<'s>, - rust: &'s Element<'s>, -) -> Result> { - 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::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), - Element::PropertyDrawer(obj) => compare_property_drawer(source, emacs, obj), - Element::Table(obj) => compare_table(source, emacs, obj), - Element::VerseBlock(obj) => compare_verse_block(source, emacs, obj), - Element::CommentBlock(obj) => compare_comment_block(source, emacs, obj), - Element::ExampleBlock(obj) => compare_example_block(source, emacs, obj), - Element::ExportBlock(obj) => compare_export_block(source, emacs, obj), - Element::SrcBlock(obj) => compare_src_block(source, emacs, obj), - Element::Clock(obj) => compare_clock(source, emacs, obj), - Element::DiarySexp(obj) => compare_diary_sexp(source, emacs, obj), - Element::Planning(obj) => compare_planning(source, emacs, obj), - Element::FixedWidthArea(obj) => compare_fixed_width_area(source, emacs, obj), - Element::HorizontalRule(obj) => compare_horizontal_rule(source, emacs, obj), - Element::Keyword(obj) => compare_keyword(source, emacs, obj), - Element::LatexEnvironment(obj) => compare_latex_environment(source, emacs, obj), - } -} - fn compare_paragraph<'s>( source: &'s str, emacs: &'s Token<'s>, rust: &'s Paragraph<'s>, ) -> Result> { let children = emacs.as_list()?; - let child_status = Vec::new(); + let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; let emacs_name = "paragraph"; if assert_name(emacs, emacs_name).is_err() { @@ -247,7 +269,9 @@ fn compare_paragraph<'s>( this_status = DiffStatus::Bad; } - for (_emacs_child, _rust_child) in children.iter().skip(2).zip(rust.children.iter()) {} + for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) { + child_status.push(compare_object(source, emacs_child, rust_child)?); + } Ok(DiffResult { status: this_status, @@ -856,3 +880,53 @@ fn compare_latex_environment<'s>( children: child_status, }) } + +fn compare_plain_text<'s>( + _source: &'s str, + emacs: &'s Token<'s>, + rust: &'s PlainText<'s>, +) -> Result> { + let mut this_status = DiffStatus::Good; + let mut message = None; + let text = emacs.as_text()?; + if text.text != rust.source { + this_status = DiffStatus::Bad; + message = Some(format!( + "(emacs != rust) {:?} != {:?}", + text.text, rust.source + )); + } + + Ok(DiffResult { + status: this_status, + name: "plain-text".to_owned(), + message, + children: Vec::new(), + }) +} + +fn compare_text_markup<'s>( + _source: &'s str, + emacs: &'s Token<'s>, + rust: &'s TextMarkup<'s>, +) -> Result> { + Ok(DiffResult { + status: DiffStatus::Good, + name: "text-markup".to_owned(), + message: None, + children: Vec::new(), + }) +} + +fn compare_regular_link<'s>( + _source: &'s str, + emacs: &'s Token<'s>, + rust: &'s RegularLink<'s>, +) -> Result> { + Ok(DiffResult { + status: DiffStatus::Good, + name: "regular-link".to_owned(), + message: None, + children: Vec::new(), + }) +} diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 48c4cb6..3bd8761 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -60,5 +60,9 @@ pub use lesser_element::Planning; pub use lesser_element::SrcBlock; pub use lesser_element::TableCell; pub use lesser_element::VerseBlock; +pub use object::Object; +pub use object::PlainText; +pub use object::RegularLink; +pub use object::TextMarkup; pub use source::Source; type Context<'r, 's> = &'r parser_context::ContextTree<'r, 's>; diff --git a/src/parser/sexp.rs b/src/parser/sexp.rs index ea4a5a6..8543548 100644 --- a/src/parser/sexp.rs +++ b/src/parser/sexp.rs @@ -27,9 +27,9 @@ pub enum Token<'s> { #[derive(Debug)] pub struct TextWithProperties<'s> { #[allow(dead_code)] - text: &'s str, + pub text: &'s str, #[allow(dead_code)] - properties: Vec>, + pub properties: Vec>, } impl<'s> Token<'s> { @@ -47,6 +47,13 @@ impl<'s> Token<'s> { }?) } + pub fn as_text<'p>(&'p self) -> Result<&'p TextWithProperties<'s>, Box> { + Ok(match self { + Token::TextWithProperties(body) => Ok(body), + _ => Err(format!("wrong token type {:?}", self)), + }?) + } + pub fn as_map<'p>( &'p self, ) -> Result>, Box> {