Simplify wasm diff result types.
clippy Build clippy has failed Details
rust-foreign-document-test Build rust-foreign-document-test has succeeded Details
rust-build Build rust-build has succeeded Details
rust-test Build rust-test has succeeded Details

This commit is contained in:
Tom Alexander 2023-12-27 13:37:50 -05:00
parent 20a7c89084
commit 1faaeeebf1
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 92 additions and 85 deletions

View File

@ -9,25 +9,15 @@ pub fn wasm_compare_document<'b, 's, 'p>(
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
wasm: WasmDocument<'s, 'p>, wasm: WasmDocument<'s, 'p>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
wasm.compare_ast_node(source, emacs) wasm.compare_ast_node(source, emacs)
} }
#[derive(Debug)] #[derive(Debug)]
pub enum WasmDiffEntry<'b, 's> { pub struct WasmDiffResult<'s> {
WasmDiffResult(WasmDiffResult<'b, 's>), status: Vec<WasmDiffStatus>,
WasmDiffLayer(WasmDiffLayer<'b, 's>),
}
#[derive(Debug)]
pub struct WasmDiffResult<'b, 's> {
status: WasmDiffStatus,
name: Cow<'s, str>, name: Cow<'s, str>,
message: Option<String>, children: Vec<WasmDiffResult<'s>>,
children: Vec<WasmDiffEntry<'b, 's>>,
rust_source: &'s str,
#[allow(dead_code)]
emacs_token: &'b Token<'s>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -36,51 +26,26 @@ pub(crate) enum WasmDiffStatus {
Bad(Cow<'static, str>), Bad(Cow<'static, str>),
} }
#[derive(Debug)] impl<'s> WasmDiffResult<'s> {
pub struct WasmDiffLayer<'b, 's> { // fn apply(
name: Cow<'s, str>, // &self,
children: Vec<WasmDiffEntry<'b, 's>>, // status: &mut WasmDiffStatus,
} // children: &mut Vec<WasmDiffEntry<'s>>,
// ) -> Result<WasmDiffEntry<'s>, Box<dyn std::error::Error>> {
#[derive(Debug)] // todo!()
pub struct WasmDiffList<'b, 's> { // }
status: WasmDiffStatus,
children: Vec<WasmDiffEntry<'b, 's>>,
}
impl<'b, 's> WasmDiffList<'b, 's> {
fn apply(
&self,
status: &mut WasmDiffStatus,
children: &mut Vec<WasmDiffEntry<'b, 's>>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>> {
todo!()
}
fn extend( fn extend(
&self, &mut self,
other: &mut WasmDiffList<'b, 's>, other: WasmDiffResult<'s>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<&mut WasmDiffResult<'s>, Box<dyn std::error::Error>> {
todo!() if self.name.is_empty() {
self.name = other.name;
}
self.status.extend(other.status);
self.children.extend(other.children);
Ok(self)
} }
}
impl<'b, 's> WasmDiffEntry<'b, '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 { pub fn is_bad(&self) -> bool {
todo!() todo!()
@ -88,48 +53,88 @@ impl<'b, 's> WasmDiffEntry<'b, 's> {
} }
pub fn print(&self, original_document: &str) -> Result<(), Box<dyn std::error::Error>> { pub fn print(&self, original_document: &str) -> Result<(), Box<dyn std::error::Error>> {
self.print_indented(0, original_document) // self.print_indented(0, original_document)
}
fn print_indented(
&self,
indentation: usize,
original_document: &str,
) -> Result<(), Box<dyn std::error::Error>> {
todo!() 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, 'p, EI, WI, WC>( impl<'s> Default for WasmDiffResult<'s> {
fn default() -> Self {
WasmDiffResult {
status: Vec::new(),
name: "".into(),
children: Vec::new(),
}
}
}
// 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<dyn std::error::Error>> {
// self.print_indented(0, original_document)
// }
// fn print_indented(
// &self,
// indentation: usize,
// original_document: &str,
// ) -> Result<(), Box<dyn std::error::Error>> {
// 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, source: &'s str,
emacs: EI, emacs: EI,
wasm: WI, wasm: WI,
) -> Result<WasmDiffList<'b, 's>, Box<dyn std::error::Error>> ) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>>
where where
EI: Iterator<Item = &'b Token<'s>> + ExactSizeIterator, EI: Iterator<Item = &'b Token<'s>> + ExactSizeIterator,
WI: Iterator<Item = WC> + ExactSizeIterator, WI: Iterator<Item = WC> + ExactSizeIterator,
WC: WasmElispCompare<'s, 'p>, WC: WasmElispCompare<'s, 'p>,
{ {
let status = Vec::new();
let emacs_length = emacs.len(); let emacs_length = emacs.len();
let wasm_length = wasm.len(); let wasm_length = wasm.len();
if emacs_length != wasm_length { if emacs_length != wasm_length {
return Ok(WasmDiffList { return Ok(WasmDiffResult {
status: WasmDiffStatus::Bad( status: vec![WasmDiffStatus::Bad(
format!( format!(
"Child length mismatch (emacs != rust) {:?} != {:?}", "Child length mismatch (emacs != rust) {:?} != {:?}",
emacs_length, wasm_length emacs_length, wasm_length
) )
.into(), .into(),
), )],
children: Vec::new(), children: Vec::new(),
name: "".into(),
}); });
} }
@ -137,9 +142,10 @@ where
for (emacs_child, wasm_child) in emacs.zip(wasm) { for (emacs_child, wasm_child) in emacs.zip(wasm) {
child_status.push(wasm_child.compare_ast_node(source, emacs_child)?); child_status.push(wasm_child.compare_ast_node(source, emacs_child)?);
} }
Ok(WasmDiffList { Ok(WasmDiffResult {
status: WasmDiffStatus::Good, status,
children: child_status, children: child_status,
name: "".into(),
}) })
} }
@ -148,7 +154,7 @@ impl<'s, 'p, WAN: WasmElispCompare<'s, 'p>> WasmElispCompare<'s, 'p> for &WAN {
&self, &self,
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
(*self).compare_ast_node(source, emacs) (*self).compare_ast_node(source, emacs)
} }
} }
@ -158,7 +164,7 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmAstNode<'s, 'p> {
&self, &self,
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
todo!() todo!()
} }
} }
@ -168,10 +174,11 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmDocument<'s, 'p> {
&self, &self,
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>> { ) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let mut result = WasmDiffResult::default();
let emacs_children = emacs.as_list()?.iter().skip(2); let emacs_children = emacs.as_list()?.iter().skip(2);
let wasm_children = self.children.iter(); let wasm_children = self.children.iter();
let child_status = wasm_compare_list(source, emacs_children, wasm_children)?; result.extend(wasm_compare_list(source, emacs_children, wasm_children)?)?;
todo!() todo!()
} }
} }

View File

@ -1,4 +1,4 @@
use super::compare::WasmDiffEntry; use super::compare::WasmDiffResult;
use crate::compare::Token; use crate::compare::Token;
pub trait WasmElispCompare<'s, 'p> { pub trait WasmElispCompare<'s, 'p> {
@ -6,5 +6,5 @@ pub trait WasmElispCompare<'s, 'p> {
&self, &self,
source: &'s str, source: &'s str,
emacs: &'b Token<'s>, emacs: &'b Token<'s>,
) -> Result<WasmDiffEntry<'b, 's>, Box<dyn std::error::Error>>; ) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>>;
} }