From 3d7f411cf90c03cf83829030f37c8dbc46c8d592 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 4 Oct 2023 10:31:01 -0400 Subject: [PATCH] Compare number lines for example blocks. --- src/compare/diff.rs | 44 ++++++++++++++++++++++++++++++++++++- src/parser/lesser_block.rs | 6 ++--- src/types/lesser_element.rs | 9 ++++++++ src/types/mod.rs | 2 ++ 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 533854f..025c922 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -52,6 +52,7 @@ use crate::types::Keyword; use crate::types::LatexEnvironment; use crate::types::LatexFragment; use crate::types::LineBreak; +use crate::types::LineNumber; use crate::types::Minute; use crate::types::MinuteInner; use crate::types::Month; @@ -83,6 +84,7 @@ use crate::types::StatisticsCookie; use crate::types::StrikeThrough; use crate::types::Subscript; use crate::types::Superscript; +use crate::types::SwitchNumberLines; use crate::types::Table; use crate::types::TableCell; use crate::types::TableRow; @@ -1545,7 +1547,7 @@ fn compare_example_block<'b, 's>( let mut this_status = DiffStatus::Good; let mut message = None; - // TODO: Compare :number-lines :preserve-indent :retain-labels :use-labels :label-fmt + // TODO: Compare :preserve-indent :retain-labels :use-labels :label-fmt // Compare value let contents = get_property_quoted_string(emacs, ":value")?.unwrap_or(String::new()); @@ -1567,6 +1569,46 @@ fn compare_example_block<'b, 's>( )); } + // Compare number-lines + let number_lines = get_property(emacs, ":number-lines")?; + match (number_lines, &rust.number_lines) { + (None, None) => {} + (Some(number_lines), Some(rust_number_lines)) => { + let token_list = number_lines.as_list()?; + let number_type = token_list + .get(0) + .map(Token::as_atom) + .map_or(Ok(None), |r| r.map(Some))? + .ok_or(":number-lines should have a type.")?; + let number_value = token_list + .get(2) + .map(Token::as_atom) + .map_or(Ok(None), |r| r.map(Some))? + .map(|val| val.parse::()) + .map_or(Ok(None), |r| r.map(Some))? + .ok_or(":number-lines should have a value.")?; + match (number_type, number_value, rust_number_lines) { + ("new", emacs_val, SwitchNumberLines::New(rust_val)) if emacs_val == *rust_val => {} + ("continued", emacs_val, SwitchNumberLines::Continued(rust_val)) + if emacs_val == *rust_val => {} + _ => { + this_status = DiffStatus::Bad; + message = Some(format!( + "Number lines mismatch (emacs != rust) {:?} != {:?}", + number_lines, rust.number_lines + )); + } + } + } + _ => { + this_status = DiffStatus::Bad; + message = Some(format!( + "Number lines mismatch (emacs != rust) {:?} != {:?}", + number_lines, rust.number_lines + )); + } + }; + Ok(DiffResult { status: this_status, name: rust.get_elisp_name(), diff --git a/src/parser/lesser_block.rs b/src/parser/lesser_block.rs index 3199781..5e97261 100644 --- a/src/parser/lesser_block.rs +++ b/src/parser/lesser_block.rs @@ -144,10 +144,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>( let parser_context = context.with_additional_node(&contexts[0]); let parser_context = parser_context.with_additional_node(&contexts[1]); let parser_context = parser_context.with_additional_node(&contexts[2]); - let parameters = match parameters { - Some((_ws, parameters)) => Some(parameters), - None => None, - }; + let parameters = parameters.map(|(_, parameters)| parameters); let (remaining, contents) = parser_with_context!(text_until_exit)(&parser_context)(remaining)?; let (remaining, _end) = lesser_block_end_specialized(&parser_context, remaining)?; @@ -159,6 +156,7 @@ pub(crate) fn example_block<'b, 'g, 'r, 's>( source: source.into(), name: source.into(), switches: parameters.map(|parameters| Into::<&str>::into(parameters)), + number_lines: None, // TODO contents: contents.into(), }, )) diff --git a/src/types/lesser_element.rs b/src/types/lesser_element.rs index 74bc728..7e5e6d4 100644 --- a/src/types/lesser_element.rs +++ b/src/types/lesser_element.rs @@ -41,6 +41,7 @@ pub struct ExampleBlock<'s> { pub source: &'s str, pub name: &'s str, pub switches: Option<&'s str>, + pub number_lines: Option, pub contents: &'s str, } @@ -107,6 +108,14 @@ pub struct LatexEnvironment<'s> { pub source: &'s str, } +pub type LineNumber = usize; + +#[derive(Debug)] +pub enum SwitchNumberLines { + New(LineNumber), + Continued(LineNumber), +} + impl<'s> Paragraph<'s> { pub(crate) fn of_text(input: &'s str) -> Self { let mut objects = Vec::with_capacity(1); diff --git a/src/types/mod.rs b/src/types/mod.rs index d0b9b97..e517795 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -47,9 +47,11 @@ pub use lesser_element::FixedWidthArea; pub use lesser_element::HorizontalRule; pub use lesser_element::Keyword; pub use lesser_element::LatexEnvironment; +pub use lesser_element::LineNumber; pub use lesser_element::Paragraph; pub use lesser_element::Planning; pub use lesser_element::SrcBlock; +pub use lesser_element::SwitchNumberLines; pub use lesser_element::TableCell; pub use lesser_element::VerseBlock; pub use object::AngleLink;