Move wasm diff structs to their own module.

This commit is contained in:
Tom Alexander 2023-12-27 19:15:39 -05:00
parent abfae9c6c0
commit 5a64db98fe
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
5 changed files with 115 additions and 108 deletions

View File

@ -9,7 +9,7 @@ use crate::wasm::WasmAstNode;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
#[serde(tag = "ast_node")] #[serde(tag = "ast_node")]
#[serde(rename = "org-data")] #[serde(rename = "paragraph")]
pub struct WasmParagraph<'s, 'p> { pub struct WasmParagraph<'s, 'p> {
standard_properties: WasmStandardProperties, standard_properties: WasmStandardProperties,
children: Vec<WasmAstNode<'s, 'p>>, children: Vec<WasmAstNode<'s, 'p>>,

View File

@ -1,5 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use super::diff::WasmDiffResult;
use super::diff::WasmDiffStatus;
use super::elisp_compare::WasmElispCompare; use super::elisp_compare::WasmElispCompare;
use crate::compare::get_emacs_standard_properties; use crate::compare::get_emacs_standard_properties;
use crate::compare::get_property_quoted_string; 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) wasm.compare_ast_node(source, emacs)
} }
#[derive(Debug)]
pub struct WasmDiffResult<'s> {
status: Vec<WasmDiffStatus>,
name: Cow<'s, str>,
children: Vec<WasmDiffResult<'s>>,
}
#[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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
self.print_indented(0, original_document)
// println!("{:#?}", self);
// todo!()
}
fn print_indented(
&self,
indentation: usize,
original_document: &str,
) -> Result<(), Box<dyn std::error::Error>> {
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>( fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>(
source: &'s str, source: &'s str,
emacs: EI, emacs: EI,

110
src/wasm_test/diff.rs Normal file
View File

@ -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<WasmDiffStatus>,
pub(crate) name: Cow<'s, str>,
pub(crate) children: Vec<WasmDiffResult<'s>>,
}
#[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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
self.print_indented(0, original_document)
// println!("{:#?}", self);
// todo!()
}
fn print_indented(
&self,
indentation: usize,
original_document: &str,
) -> Result<(), Box<dyn std::error::Error>> {
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(),
}
}
}

View File

@ -1,4 +1,4 @@
use super::compare::WasmDiffResult; use super::diff::WasmDiffResult;
use crate::compare::Token; use crate::compare::Token;
pub trait WasmElispCompare<'s, 'p> { pub trait WasmElispCompare<'s, 'p> {

View File

@ -1,4 +1,5 @@
mod compare; mod compare;
mod diff;
mod elisp_compare; mod elisp_compare;
mod macros; mod macros;
mod runner; mod runner;