From 90d4b11922451880a1b04ce9f0183f9af33516bb Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 27 Dec 2023 18:39:10 -0500 Subject: [PATCH] Switch to a formatted print of the wasm compare status. --- src/wasm_test/compare.rs | 121 +++++++++++++++++++++------------------ src/wasm_test/macros.rs | 3 +- 2 files changed, 67 insertions(+), 57 deletions(-) diff --git a/src/wasm_test/compare.rs b/src/wasm_test/compare.rs index 1336740..59ed97f 100644 --- a/src/wasm_test/compare.rs +++ b/src/wasm_test/compare.rs @@ -6,6 +6,8 @@ use crate::compare::get_property_quoted_string; use crate::compare::ElispFact; use crate::compare::EmacsField; use crate::compare::Token; +use crate::util::foreground_color; +use crate::util::reset_color; use crate::wasm::AdditionalProperties; use crate::wasm::AdditionalPropertyValue; use crate::wasm::WasmAstNode; @@ -37,14 +39,6 @@ pub(crate) enum WasmDiffStatus { } impl<'s> WasmDiffResult<'s> { - // fn apply( - // &self, - // status: &mut WasmDiffStatus, - // children: &mut Vec>, - // ) -> Result, Box> { - // todo!() - // } - fn extend( &mut self, other: WasmDiffResult<'s>, @@ -58,14 +52,72 @@ impl<'s> WasmDiffResult<'s> { } pub fn is_bad(&self) -> bool { - todo!() - // self.is_immediately_bad() || self.has_bad_children() + 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!() + 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(()) } } @@ -79,49 +131,6 @@ impl<'s> Default for WasmDiffResult<'s> { } } -// impl<'s> WasmDiffEntry<'s> { -// // fn has_bad_children(&self) -> bool { -// // match self { -// // DiffEntry::DiffResult(diff) => &diff.children, -// // DiffEntry::DiffLayer(diff) => &diff.children, -// // } -// // .iter() -// // .any(|child| child.is_immediately_bad() || child.has_bad_children()) -// // } - -// // fn is_immediately_bad(&self) -> bool { -// // match self { -// // DiffEntry::DiffResult(diff) => matches!(diff.status, DiffStatus::Bad), -// // DiffEntry::DiffLayer(_) => false, -// // } -// // } - -// pub fn is_bad(&self) -> bool { -// todo!() -// // self.is_immediately_bad() || self.has_bad_children() -// } - -// pub fn print(&self, original_document: &str) -> Result<(), Box> { -// self.print_indented(0, original_document) -// } - -// fn print_indented( -// &self, -// indentation: usize, -// original_document: &str, -// ) -> Result<(), Box> { -// todo!() -// // match self { -// // WasmDiffEntry::WasmDiffResult(diff) => { -// // diff.print_indented(indentation, original_document) -// // } -// // WasmDiffEntry::WasmDiffLayer(diff) => { -// // diff.print_indented(indentation, original_document) -// // } -// // } -// } -// } - fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>( source: &'s str, emacs: EI, diff --git a/src/wasm_test/macros.rs b/src/wasm_test/macros.rs index 5e0f181..2ae0692 100644 --- a/src/wasm_test/macros.rs +++ b/src/wasm_test/macros.rs @@ -6,7 +6,8 @@ macro_rules! wasm_compare { let emacs_name = emacs_list_iter .next() .ok_or("Should have an attributes child.")? - .as_atom()?; + .as_atom()?; + result.name = emacs_name.into(); let emacs_attributes_map = emacs_list_iter .next() .ok_or("Should have an attributes child.")?