From 5a64db98fe1ecb7d5e8250750bc6bac84f8cae4b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 27 Dec 2023 19:15:39 -0500 Subject: [PATCH] Move wasm diff structs to their own module. --- src/wasm/paragraph.rs | 2 +- src/wasm_test/compare.rs | 108 +------------------------------- src/wasm_test/diff.rs | 110 +++++++++++++++++++++++++++++++++ src/wasm_test/elisp_compare.rs | 2 +- src/wasm_test/mod.rs | 1 + 5 files changed, 115 insertions(+), 108 deletions(-) create mode 100644 src/wasm_test/diff.rs diff --git a/src/wasm/paragraph.rs b/src/wasm/paragraph.rs index 809550bb..8c4c0c19 100644 --- a/src/wasm/paragraph.rs +++ b/src/wasm/paragraph.rs @@ -9,7 +9,7 @@ use crate::wasm::WasmAstNode; #[derive(Debug, Serialize)] #[serde(tag = "ast_node")] -#[serde(rename = "org-data")] +#[serde(rename = "paragraph")] pub struct WasmParagraph<'s, 'p> { standard_properties: WasmStandardProperties, children: Vec>, diff --git a/src/wasm_test/compare.rs b/src/wasm_test/compare.rs index fb5a7719..305daacf 100644 --- a/src/wasm_test/compare.rs +++ b/src/wasm_test/compare.rs @@ -1,5 +1,7 @@ use std::borrow::Cow; +use super::diff::WasmDiffResult; +use super::diff::WasmDiffStatus; use super::elisp_compare::WasmElispCompare; use crate::compare::get_emacs_standard_properties; use crate::compare::get_property_quoted_string; @@ -26,112 +28,6 @@ pub fn wasm_compare_document<'b, 's, 'p>( wasm.compare_ast_node(source, emacs) } -#[derive(Debug)] -pub struct WasmDiffResult<'s> { - status: Vec, - name: Cow<'s, str>, - children: Vec>, -} - -#[derive(Debug)] -pub(crate) enum WasmDiffStatus { - Good, - Bad(Cow<'static, str>), -} - -impl<'s> WasmDiffResult<'s> { - fn extend( - &mut self, - other: WasmDiffResult<'s>, - ) -> Result<&mut WasmDiffResult<'s>, Box> { - if self.name.is_empty() { - self.name = other.name; - } - self.status.extend(other.status); - self.children.extend(other.children); - Ok(self) - } - - pub fn is_bad(&self) -> bool { - self.is_self_bad() || self.has_bad_children() - } - - pub fn is_self_bad(&self) -> bool { - self.status - .iter() - .any(|status| matches!(status, WasmDiffStatus::Bad(_))) - } - - pub fn has_bad_children(&self) -> bool { - self.children.iter().any(WasmDiffResult::is_bad) - } - - pub fn print(&self, original_document: &str) -> Result<(), Box> { - self.print_indented(0, original_document) - // println!("{:#?}", self); - // todo!() - } - - fn print_indented( - &self, - indentation: usize, - original_document: &str, - ) -> Result<(), Box> { - let status_text = { - if self.is_bad() { - format!( - "{color}BAD{reset}", - color = foreground_color(255, 0, 0), - reset = reset_color(), - ) - } else if self.has_bad_children() { - format!( - "{color}BADCHILD{reset}", - color = foreground_color(255, 255, 0), - reset = reset_color(), - ) - } else { - format!( - "{color}GOOD{reset}", - color = foreground_color(0, 255, 0), - reset = reset_color(), - ) - } - }; - let message = self - .status - .iter() - .filter_map(|status| match status { - WasmDiffStatus::Good => None, - WasmDiffStatus::Bad(message) => Some(message), - }) - .next(); - println!( - "{indentation}{status_text} {name} {message}", - indentation = " ".repeat(indentation), - status_text = status_text, - name = self.name, - message = message.unwrap_or(&Cow::Borrowed("")) - ); - - for child in self.children.iter() { - child.print_indented(indentation + 1, original_document)?; - } - - Ok(()) - } -} - -impl<'s> Default for WasmDiffResult<'s> { - fn default() -> Self { - WasmDiffResult { - status: Vec::new(), - name: "".into(), - children: Vec::new(), - } - } -} - fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>( source: &'s str, emacs: EI, diff --git a/src/wasm_test/diff.rs b/src/wasm_test/diff.rs new file mode 100644 index 00000000..0cccf4d5 --- /dev/null +++ b/src/wasm_test/diff.rs @@ -0,0 +1,110 @@ +use std::borrow::Cow; + +use crate::util::foreground_color; +use crate::util::reset_color; + +#[derive(Debug)] +pub struct WasmDiffResult<'s> { + pub(crate) status: Vec, + pub(crate) name: Cow<'s, str>, + pub(crate) children: Vec>, +} + +#[derive(Debug)] +pub(crate) enum WasmDiffStatus { + Good, + Bad(Cow<'static, str>), +} + +impl<'s> WasmDiffResult<'s> { + pub(crate) fn extend( + &mut self, + other: WasmDiffResult<'s>, + ) -> Result<&mut WasmDiffResult<'s>, Box> { + if self.name.is_empty() { + self.name = other.name; + } + self.status.extend(other.status); + self.children.extend(other.children); + Ok(self) + } + + pub fn is_bad(&self) -> bool { + self.is_self_bad() || self.has_bad_children() + } + + pub fn is_self_bad(&self) -> bool { + self.status + .iter() + .any(|status| matches!(status, WasmDiffStatus::Bad(_))) + } + + pub fn has_bad_children(&self) -> bool { + self.children.iter().any(WasmDiffResult::is_bad) + } + + pub fn print(&self, original_document: &str) -> Result<(), Box> { + self.print_indented(0, original_document) + // println!("{:#?}", self); + // todo!() + } + + fn print_indented( + &self, + indentation: usize, + original_document: &str, + ) -> Result<(), Box> { + let status_text = { + if self.is_bad() { + format!( + "{color}BAD{reset}", + color = foreground_color(255, 0, 0), + reset = reset_color(), + ) + } else if self.has_bad_children() { + format!( + "{color}BADCHILD{reset}", + color = foreground_color(255, 255, 0), + reset = reset_color(), + ) + } else { + format!( + "{color}GOOD{reset}", + color = foreground_color(0, 255, 0), + reset = reset_color(), + ) + } + }; + let message = self + .status + .iter() + .filter_map(|status| match status { + WasmDiffStatus::Good => None, + WasmDiffStatus::Bad(message) => Some(message), + }) + .next(); + println!( + "{indentation}{status_text} {name} {message}", + indentation = " ".repeat(indentation), + status_text = status_text, + name = self.name, + message = message.unwrap_or(&Cow::Borrowed("")) + ); + + for child in self.children.iter() { + child.print_indented(indentation + 1, original_document)?; + } + + Ok(()) + } +} + +impl<'s> Default for WasmDiffResult<'s> { + fn default() -> Self { + WasmDiffResult { + status: Vec::new(), + name: "".into(), + children: Vec::new(), + } + } +} diff --git a/src/wasm_test/elisp_compare.rs b/src/wasm_test/elisp_compare.rs index 85291fd3..fc23b528 100644 --- a/src/wasm_test/elisp_compare.rs +++ b/src/wasm_test/elisp_compare.rs @@ -1,4 +1,4 @@ -use super::compare::WasmDiffResult; +use super::diff::WasmDiffResult; use crate::compare::Token; pub trait WasmElispCompare<'s, 'p> { diff --git a/src/wasm_test/mod.rs b/src/wasm_test/mod.rs index e2d68a94..84ab05ed 100644 --- a/src/wasm_test/mod.rs +++ b/src/wasm_test/mod.rs @@ -1,4 +1,5 @@ mod compare; +mod diff; mod elisp_compare; mod macros; mod runner;