organic/src/wasm_test/compare.rs

475 lines
16 KiB
Rust
Raw Normal View History

use std::borrow::Cow;
use super::elisp_compare::WasmElispCompare;
2023-12-27 22:07:42 +00:00
use crate::compare::get_emacs_standard_properties;
use crate::compare::get_property_quoted_string;
2023-12-27 20:14:42 +00:00
use crate::compare::ElispFact;
use crate::compare::EmacsField;
2023-12-27 14:31:54 +00:00
use crate::compare::Token;
use crate::util::foreground_color;
use crate::util::reset_color;
2023-12-27 23:20:23 +00:00
use crate::wasm::AdditionalProperties;
use crate::wasm::AdditionalPropertyValue;
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 21:47:02 +00:00
use crate::wasm::WasmHeadline;
2023-12-27 23:47:59 +00:00
use crate::wasm::WasmParagraph;
2023-12-27 21:47:02 +00:00
use crate::wasm::WasmSection;
2023-12-27 22:07:42 +00:00
use crate::wasm::WasmStandardProperties;
2023-12-27 20:14:42 +00:00
use crate::wasm_test::macros::wasm_compare;
2023-12-27 14:31:54 +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>,
wasm: WasmDocument<'s, 'p>,
2023-12-27 18:37:50 +00:00
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
wasm.compare_ast_node(source, emacs)
2023-12-27 14:31:54 +00:00
}
#[derive(Debug)]
2023-12-27 18:37:50 +00:00
pub struct WasmDiffResult<'s> {
status: Vec<WasmDiffStatus>,
name: Cow<'s, str>,
2023-12-27 18:37:50 +00:00
children: Vec<WasmDiffResult<'s>>,
}
#[derive(Debug)]
pub(crate) enum WasmDiffStatus {
Good,
2023-12-27 18:21:20 +00:00
Bad(Cow<'static, str>),
}
2023-12-27 18:37:50 +00:00
impl<'s> WasmDiffResult<'s> {
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
}
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(())
}
2023-12-27 18:37:50 +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 16:10:40 +00:00
2023-12-27 18:37:50 +00:00
fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>(
2023-12-27 16:10:40 +00:00
source: &'s str,
emacs: EI,
wasm: WI,
2023-12-27 18:37:50 +00:00
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>>
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 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 20:14:42 +00:00
match self {
WasmAstNode::Document(inner) => inner.compare_ast_node(source, emacs),
2023-12-27 21:47:02 +00:00
WasmAstNode::Headline(inner) => inner.compare_ast_node(source, emacs),
WasmAstNode::Section(inner) => inner.compare_ast_node(source, emacs),
2023-12-27 23:47:59 +00:00
WasmAstNode::Paragraph(inner) => inner.compare_ast_node(source, emacs),
2023-12-27 20:14:42 +00:00
WasmAstNode::PlainList(_) => todo!(),
WasmAstNode::PlainListItem(_) => todo!(),
WasmAstNode::CenterBlock(_) => todo!(),
WasmAstNode::QuoteBlock(_) => todo!(),
WasmAstNode::SpecialBlock(_) => todo!(),
WasmAstNode::DynamicBlock(_) => todo!(),
WasmAstNode::FootnoteDefinition(_) => todo!(),
WasmAstNode::Comment(_) => todo!(),
WasmAstNode::Drawer(_) => todo!(),
WasmAstNode::PropertyDrawer(_) => todo!(),
WasmAstNode::NodeProperty(_) => todo!(),
WasmAstNode::Table(_) => todo!(),
WasmAstNode::TableRow(_) => todo!(),
WasmAstNode::VerseBlock(_) => todo!(),
WasmAstNode::CommentBlock(_) => todo!(),
WasmAstNode::ExampleBlock(_) => todo!(),
WasmAstNode::ExportBlock(_) => todo!(),
WasmAstNode::SrcBlock(_) => todo!(),
WasmAstNode::Clock(_) => todo!(),
WasmAstNode::DiarySexp(_) => todo!(),
WasmAstNode::Planning(_) => todo!(),
WasmAstNode::FixedWidthArea(_) => todo!(),
WasmAstNode::HorizontalRule(_) => todo!(),
WasmAstNode::Keyword(_) => todo!(),
WasmAstNode::BabelCall(_) => todo!(),
WasmAstNode::LatexEnvironment(_) => todo!(),
WasmAstNode::Bold(_) => todo!(),
WasmAstNode::Italic(_) => todo!(),
WasmAstNode::Underline(_) => todo!(),
WasmAstNode::StrikeThrough(_) => todo!(),
WasmAstNode::Code(_) => todo!(),
WasmAstNode::Verbatim(_) => todo!(),
WasmAstNode::PlainText(_) => todo!(),
WasmAstNode::RegularLink(_) => todo!(),
WasmAstNode::RadioLink(_) => todo!(),
WasmAstNode::RadioTarget(_) => todo!(),
WasmAstNode::PlainLink(_) => todo!(),
WasmAstNode::AngleLink(_) => todo!(),
WasmAstNode::OrgMacro(_) => todo!(),
WasmAstNode::Entity(_) => todo!(),
WasmAstNode::LatexFragment(_) => todo!(),
WasmAstNode::ExportSnippet(_) => todo!(),
WasmAstNode::FootnoteReference(_) => todo!(),
WasmAstNode::Citation(_) => todo!(),
WasmAstNode::CitationReference(_) => todo!(),
WasmAstNode::InlineBabelCall(_) => todo!(),
WasmAstNode::InlineSourceBlock(_) => todo!(),
WasmAstNode::LineBreak(_) => todo!(),
WasmAstNode::Target(_) => todo!(),
WasmAstNode::StatisticsCookie(_) => todo!(),
WasmAstNode::Subscript(_) => todo!(),
WasmAstNode::Superscript(_) => todo!(),
WasmAstNode::TableCell(_) => todo!(),
WasmAstNode::Timestamp(_) => todo!(),
}
2023-12-27 18:21:20 +00:00
}
2023-12-27 16:10:40 +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>> {
2023-12-27 20:14:42 +00:00
let result = wasm_compare!(
source,
emacs,
self,
(
EmacsField::Required(":path"),
|w| w.path.as_ref().and_then(|p| p.to_str()),
wasm_compare_property_quoted_string
2023-12-27 21:34:04 +00:00
),
(
EmacsField::Required(":CATEGORY"),
|w| w.category.as_ref(),
wasm_compare_property_quoted_string
2023-12-27 20:14:42 +00:00
)
);
Ok(result)
}
}
2023-12-27 21:47:02 +00:00
impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmHeadline<'s, 'p> {
fn compare_ast_node<'b>(
&self,
source: &'s str,
emacs: &'b Token<'s>,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let result = WasmDiffResult::default();
// let result = wasm_compare!(
// source,
// emacs,
// self,
// (
// EmacsField::Required(":path"),
// |w| w.path.as_ref().and_then(|p| p.to_str()),
// wasm_compare_property_quoted_string
// ),
// (
// EmacsField::Required(":CATEGORY"),
// |w| w.category.as_ref(),
// wasm_compare_property_quoted_string
// )
// );
Ok(result)
}
}
impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmSection<'s, 'p> {
fn compare_ast_node<'b>(
&self,
source: &'s str,
emacs: &'b Token<'s>,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
2023-12-28 00:10:43 +00:00
let result = wasm_compare!(source, emacs, self,);
2023-12-27 23:47:59 +00:00
Ok(result)
}
}
impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmParagraph<'s, 'p> {
fn compare_ast_node<'b>(
&self,
source: &'s str,
emacs: &'b Token<'s>,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let result = WasmDiffResult::default();
// let result = wasm_compare!(
// source,
// emacs,
// self,
2023-12-27 21:47:02 +00:00
// (
// EmacsField::Required(":path"),
// |w| w.path.as_ref().and_then(|p| p.to_str()),
// wasm_compare_property_quoted_string
// ),
// (
// EmacsField::Required(":CATEGORY"),
// |w| w.category.as_ref(),
// wasm_compare_property_quoted_string
// )
// );
Ok(result)
}
}
fn wasm_compare_property_quoted_string<
'b,
's,
W,
WV: AsRef<str> + std::fmt::Debug,
WG: Fn(W) -> Option<WV>,
>(
_source: &'s str,
emacs: &'b Token<'s>,
wasm_node: W,
emacs_field: &str,
wasm_value_getter: WG,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let mut result = WasmDiffResult::default();
let emacs_value = get_property_quoted_string(emacs, emacs_field)?;
let wasm_value = wasm_value_getter(wasm_node);
if wasm_value.as_ref().map(|s| s.as_ref()) != emacs_value.as_deref() {
result.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = emacs_field,
emacs = emacs_value,
wasm = wasm_value,
)
.into(),
))
}
Ok(result)
}
2023-12-27 22:07:42 +00:00
fn wasm_compare_standard_properties<'b, 's>(
_source: &'s str,
emacs: &'b Token<'s>,
wasm: &WasmStandardProperties,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let mut result = WasmDiffResult::default();
let mut layer = WasmDiffResult::default();
layer.name = "standard-properties".into();
let standard_properties = get_emacs_standard_properties(emacs)?;
if Some(wasm.begin) != standard_properties.begin {
layer.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "begin",
emacs = standard_properties.begin,
wasm = Some(wasm.begin),
)
.into(),
));
}
if Some(wasm.end) != standard_properties.end {
layer.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "end",
emacs = standard_properties.end,
wasm = Some(wasm.end),
)
.into(),
));
}
if wasm.contents_begin != standard_properties.contents_begin {
layer.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "contents-begin",
emacs = standard_properties.contents_begin,
wasm = wasm.contents_begin,
)
.into(),
));
}
if wasm.contents_end != standard_properties.contents_end {
layer.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "contents-end",
emacs = standard_properties.contents_end,
wasm = wasm.contents_end,
)
.into(),
));
}
if Some(wasm.post_blank).map(|post_blank| post_blank as usize) != standard_properties.post_blank
{
layer.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "post-blank",
emacs = standard_properties.post_blank,
wasm = Some(wasm.post_blank),
)
.into(),
));
}
result.children.push(layer);
Ok(result)
}
2023-12-27 23:20:23 +00:00
fn wasm_compare_additional_properties<'b, 's>(
source: &'s str,
emacs: &'b Token<'s>,
wasm: &AdditionalProperties<'_, '_>,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let mut result = WasmDiffResult::default();
let mut layer = WasmDiffResult::default();
layer.name = "additional-properties".into();
for (property_name, property_value) in wasm.properties.iter() {
let emacs_property_name = format!(":{property_name}", property_name = property_name);
match property_value {
AdditionalPropertyValue::SingleString(wasm_value) => {
layer.extend(wasm_compare_property_quoted_string(
source,
emacs,
wasm,
&emacs_property_name,
|_| Some(wasm_value),
)?)?;
}
// TODO: similar to compare_affiliated_keywords
AdditionalPropertyValue::ListOfStrings(_) => todo!(),
AdditionalPropertyValue::OptionalPair { optval, val } => todo!(),
AdditionalPropertyValue::ObjectTree(_) => todo!(),
}
}
result.children.push(layer);
Ok(result)
}