111 lines
3.0 KiB
Rust
111 lines
3.0 KiB
Rust
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(),
|
|
}
|
|
}
|
|
}
|