From 1faaeeebf1e1810c126cf1637d023661a21a6b47 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Wed, 27 Dec 2023 13:37:50 -0500 Subject: [PATCH] Simplify wasm diff result types. --- src/wasm_test/compare.rs | 173 +++++++++++++++++---------------- src/wasm_test/elisp_compare.rs | 4 +- 2 files changed, 92 insertions(+), 85 deletions(-) diff --git a/src/wasm_test/compare.rs b/src/wasm_test/compare.rs index c375f30..1e639f1 100644 --- a/src/wasm_test/compare.rs +++ b/src/wasm_test/compare.rs @@ -9,25 +9,15 @@ pub fn wasm_compare_document<'b, 's, 'p>( source: &'s str, emacs: &'b Token<'s>, wasm: WasmDocument<'s, 'p>, -) -> Result, Box> { +) -> Result, Box> { wasm.compare_ast_node(source, emacs) } #[derive(Debug)] -pub enum WasmDiffEntry<'b, 's> { - WasmDiffResult(WasmDiffResult<'b, 's>), - WasmDiffLayer(WasmDiffLayer<'b, 's>), -} - -#[derive(Debug)] -pub struct WasmDiffResult<'b, 's> { - status: WasmDiffStatus, +pub struct WasmDiffResult<'s> { + status: Vec, name: Cow<'s, str>, - message: Option, - children: Vec>, - rust_source: &'s str, - #[allow(dead_code)] - emacs_token: &'b Token<'s>, + children: Vec>, } #[derive(Debug)] @@ -36,51 +26,26 @@ pub(crate) enum WasmDiffStatus { Bad(Cow<'static, str>), } -#[derive(Debug)] -pub struct WasmDiffLayer<'b, 's> { - name: Cow<'s, str>, - children: Vec>, -} - -#[derive(Debug)] -pub struct WasmDiffList<'b, 's> { - status: WasmDiffStatus, - children: Vec>, -} - -impl<'b, 's> WasmDiffList<'b, 's> { - fn apply( - &self, - status: &mut WasmDiffStatus, - children: &mut Vec>, - ) -> Result, Box> { - todo!() - } +impl<'s> WasmDiffResult<'s> { + // fn apply( + // &self, + // status: &mut WasmDiffStatus, + // children: &mut Vec>, + // ) -> Result, Box> { + // todo!() + // } fn extend( - &self, - other: &mut WasmDiffList<'b, 's>, - ) -> Result, Box> { - todo!() + &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) } -} - -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 { todo!() @@ -88,48 +53,88 @@ impl<'b, 's> WasmDiffEntry<'b, 's> { } 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> { + // self.print_indented(0, original_document) 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> { +// 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, wasm: WI, -) -> Result, Box> +) -> Result, Box> where EI: Iterator> + ExactSizeIterator, WI: Iterator + ExactSizeIterator, WC: WasmElispCompare<'s, 'p>, { + let status = Vec::new(); let emacs_length = emacs.len(); let wasm_length = wasm.len(); if emacs_length != wasm_length { - return Ok(WasmDiffList { - status: WasmDiffStatus::Bad( + return Ok(WasmDiffResult { + status: vec![WasmDiffStatus::Bad( format!( "Child length mismatch (emacs != rust) {:?} != {:?}", emacs_length, wasm_length ) .into(), - ), + )], children: Vec::new(), + name: "".into(), }); } @@ -137,9 +142,10 @@ where for (emacs_child, wasm_child) in emacs.zip(wasm) { child_status.push(wasm_child.compare_ast_node(source, emacs_child)?); } - Ok(WasmDiffList { - status: WasmDiffStatus::Good, + Ok(WasmDiffResult { + status, children: child_status, + name: "".into(), }) } @@ -148,7 +154,7 @@ impl<'s, 'p, WAN: WasmElispCompare<'s, 'p>> WasmElispCompare<'s, 'p> for &WAN { &self, source: &'s str, emacs: &'b Token<'s>, - ) -> Result, Box> { + ) -> Result, Box> { (*self).compare_ast_node(source, emacs) } } @@ -158,7 +164,7 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmAstNode<'s, 'p> { &self, source: &'s str, emacs: &'b Token<'s>, - ) -> Result, Box> { + ) -> Result, Box> { todo!() } } @@ -168,10 +174,11 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmDocument<'s, 'p> { &self, source: &'s str, emacs: &'b Token<'s>, - ) -> Result, Box> { + ) -> Result, Box> { + let mut result = WasmDiffResult::default(); let emacs_children = emacs.as_list()?.iter().skip(2); 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!() } } diff --git a/src/wasm_test/elisp_compare.rs b/src/wasm_test/elisp_compare.rs index 25de180..85291fd 100644 --- a/src/wasm_test/elisp_compare.rs +++ b/src/wasm_test/elisp_compare.rs @@ -1,4 +1,4 @@ -use super::compare::WasmDiffEntry; +use super::compare::WasmDiffResult; use crate::compare::Token; pub trait WasmElispCompare<'s, 'p> { @@ -6,5 +6,5 @@ pub trait WasmElispCompare<'s, 'p> { &self, source: &'s str, emacs: &'b Token<'s>, - ) -> Result, Box>; + ) -> Result, Box>; }