2023-12-27 15:52:59 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2023-12-27 17:38:21 +00:00
|
|
|
use super::elisp_compare::WasmElispCompare;
|
2023-12-27 14:31:54 +00:00
|
|
|
use crate::compare::Token;
|
2023-12-27 16:10:40 +00:00
|
|
|
use crate::wasm::WasmAstNode;
|
2023-12-27 14:31:54 +00:00
|
|
|
use crate::wasm::WasmDocument;
|
|
|
|
|
2023-12-27 16:36:47 +00:00
|
|
|
pub fn wasm_compare_document<'b, 's, 'p>(
|
2023-12-27 16:10:40 +00:00
|
|
|
source: &'s str,
|
2023-12-27 14:31:54 +00:00
|
|
|
emacs: &'b Token<'s>,
|
2023-12-27 16:36:47 +00:00
|
|
|
wasm: WasmDocument<'s, 'p>,
|
2023-12-27 18:37:50 +00:00
|
|
|
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
|
2023-12-27 17:38:21 +00:00
|
|
|
wasm.compare_ast_node(source, emacs)
|
2023-12-27 14:31:54 +00:00
|
|
|
}
|
2023-12-27 15:52:59 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-12-27 18:37:50 +00:00
|
|
|
pub struct WasmDiffResult<'s> {
|
|
|
|
status: Vec<WasmDiffStatus>,
|
2023-12-27 15:52:59 +00:00
|
|
|
name: Cow<'s, str>,
|
2023-12-27 18:37:50 +00:00
|
|
|
children: Vec<WasmDiffResult<'s>>,
|
2023-12-27 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) enum WasmDiffStatus {
|
|
|
|
Good,
|
2023-12-27 18:21:20 +00:00
|
|
|
Bad(Cow<'static, str>),
|
2023-12-27 15:52:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 18:37:50 +00:00
|
|
|
impl<'s> WasmDiffResult<'s> {
|
|
|
|
// fn apply(
|
|
|
|
// &self,
|
|
|
|
// status: &mut WasmDiffStatus,
|
|
|
|
// children: &mut Vec<WasmDiffEntry<'s>>,
|
|
|
|
// ) -> Result<WasmDiffEntry<'s>, Box<dyn std::error::Error>> {
|
|
|
|
// todo!()
|
|
|
|
// }
|
2023-12-27 18:21:20 +00:00
|
|
|
|
|
|
|
fn extend(
|
2023-12-27 18:37:50 +00:00
|
|
|
&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)
|
2023-12-27 18:21:20 +00:00
|
|
|
}
|
2023-12-27 15:52:59 +00:00
|
|
|
|
|
|
|
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>> {
|
2023-12-27 18:37:50 +00:00
|
|
|
// self.print_indented(0, original_document)
|
|
|
|
todo!()
|
2023-12-27 15:52:59 +00:00
|
|
|
}
|
2023-12-27 18:37:50 +00:00
|
|
|
}
|
2023-12-27 15:52:59 +00:00
|
|
|
|
2023-12-27 18:37:50 +00:00
|
|
|
impl<'s> Default for WasmDiffResult<'s> {
|
|
|
|
fn default() -> Self {
|
|
|
|
WasmDiffResult {
|
|
|
|
status: Vec::new(),
|
|
|
|
name: "".into(),
|
|
|
|
children: Vec::new(),
|
|
|
|
}
|
2023-12-27 15:52:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-27 16:10:40 +00:00
|
|
|
|
2023-12-27 18:37:50 +00:00
|
|
|
// 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>(
|
2023-12-27 16:10:40 +00:00
|
|
|
source: &'s str,
|
2023-12-27 17:38:21 +00:00
|
|
|
emacs: EI,
|
|
|
|
wasm: WI,
|
2023-12-27 18:37:50 +00:00
|
|
|
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>>
|
2023-12-27 17:38:21 +00:00
|
|
|
where
|
|
|
|
EI: Iterator<Item = &'b Token<'s>> + ExactSizeIterator,
|
|
|
|
WI: Iterator<Item = WC> + ExactSizeIterator,
|
|
|
|
WC: WasmElispCompare<'s, 'p>,
|
|
|
|
{
|
2023-12-27 18:37:50 +00:00
|
|
|
let status = Vec::new();
|
2023-12-27 18:21:20 +00:00
|
|
|
let emacs_length = emacs.len();
|
|
|
|
let wasm_length = wasm.len();
|
|
|
|
if emacs_length != wasm_length {
|
2023-12-27 18:37:50 +00:00
|
|
|
return Ok(WasmDiffResult {
|
|
|
|
status: vec![WasmDiffStatus::Bad(
|
2023-12-27 18:21:20 +00:00
|
|
|
format!(
|
|
|
|
"Child length mismatch (emacs != rust) {:?} != {:?}",
|
|
|
|
emacs_length, wasm_length
|
|
|
|
)
|
|
|
|
.into(),
|
2023-12-27 18:37:50 +00:00
|
|
|
)],
|
2023-12-27 18:21:20 +00:00
|
|
|
children: Vec::new(),
|
2023-12-27 18:37:50 +00:00
|
|
|
name: "".into(),
|
2023-12-27 18:21:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut child_status = Vec::with_capacity(emacs_length);
|
|
|
|
for (emacs_child, wasm_child) in emacs.zip(wasm) {
|
|
|
|
child_status.push(wasm_child.compare_ast_node(source, emacs_child)?);
|
|
|
|
}
|
2023-12-27 18:37:50 +00:00
|
|
|
Ok(WasmDiffResult {
|
|
|
|
status,
|
2023-12-27 18:21:20 +00:00
|
|
|
children: child_status,
|
2023-12-27 18:37:50 +00:00
|
|
|
name: "".into(),
|
2023-12-27 18:21:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'s, 'p, WAN: WasmElispCompare<'s, 'p>> WasmElispCompare<'s, 'p> for &WAN {
|
|
|
|
fn compare_ast_node<'b>(
|
|
|
|
&self,
|
|
|
|
source: &'s str,
|
|
|
|
emacs: &'b Token<'s>,
|
2023-12-27 18:37:50 +00:00
|
|
|
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
|
2023-12-27 18:21:20 +00:00
|
|
|
(*self).compare_ast_node(source, emacs)
|
|
|
|
}
|
|
|
|
}
|
2023-12-27 16:36:47 +00:00
|
|
|
|
2023-12-27 18:21:20 +00:00
|
|
|
impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmAstNode<'s, 'p> {
|
|
|
|
fn compare_ast_node<'b>(
|
|
|
|
&self,
|
|
|
|
source: &'s str,
|
|
|
|
emacs: &'b Token<'s>,
|
2023-12-27 18:37:50 +00:00
|
|
|
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
|
2023-12-27 18:21:20 +00:00
|
|
|
todo!()
|
|
|
|
}
|
2023-12-27 16:10:40 +00:00
|
|
|
}
|
2023-12-27 17:38:21 +00:00
|
|
|
|
|
|
|
impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmDocument<'s, 'p> {
|
|
|
|
fn compare_ast_node<'b>(
|
|
|
|
&self,
|
|
|
|
source: &'s str,
|
|
|
|
emacs: &'b Token<'s>,
|
2023-12-27 18:37:50 +00:00
|
|
|
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
|
|
|
|
let mut result = WasmDiffResult::default();
|
2023-12-27 18:21:20 +00:00
|
|
|
let emacs_children = emacs.as_list()?.iter().skip(2);
|
|
|
|
let wasm_children = self.children.iter();
|
2023-12-27 18:37:50 +00:00
|
|
|
result.extend(wasm_compare_list(source, emacs_children, wasm_children)?)?;
|
2023-12-27 17:38:21 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|