Compare commits

...

3 Commits

Author SHA1 Message Date
Tom Alexander
90d4b11922
Switch to a formatted print of the wasm compare status.
Some checks failed
clippy Build clippy has failed
rust-build Build rust-build has succeeded
rust-foreign-document-test Build rust-foreign-document-test has succeeded
rust-test Build rust-test has succeeded
2023-12-27 18:39:10 -05:00
Tom Alexander
d552ef6569
Compare the additional properties. 2023-12-27 18:20:23 -05:00
Tom Alexander
f050e9b6a8
Taking into account additional property names but not comparing their values. 2023-12-27 18:01:56 -05:00
5 changed files with 172 additions and 68 deletions

View File

@ -0,0 +1,27 @@
use std::collections::HashMap;
use serde::Serialize;
use super::WasmAstNode;
#[derive(Debug, Serialize)]
pub enum AdditionalPropertyValue<'s, 'p> {
SingleString(&'s str),
ListOfStrings(Vec<&'s str>),
OptionalPair {
optval: Option<&'s str>,
val: &'s str,
},
ObjectTree(Vec<(Option<Vec<WasmAstNode<'s, 'p>>>, Vec<WasmAstNode<'s, 'p>>)>),
}
#[derive(Debug, Serialize, Default)]
pub struct AdditionalProperties<'s, 'p> {
pub(crate) properties: HashMap<String, AdditionalPropertyValue<'s, 'p>>,
}
impl<'s, 'p> AdditionalProperties<'s, 'p> {
pub(crate) fn get_elisp_names<'c>(&'c self) -> impl Iterator<Item = String> + 'c {
self.properties.keys().map(move |key| format!(":{}", key))
}
}

View File

@ -2,6 +2,8 @@ use std::path::PathBuf;
use serde::Serialize;
use super::additional_property::AdditionalProperties;
use super::additional_property::AdditionalPropertyValue;
use super::ast_node::WasmAstNode;
use super::macros::to_wasm;
use super::standard_properties::WasmStandardProperties;
@ -16,7 +18,7 @@ use crate::wasm::to_wasm::ToWasmStandardProperties;
#[serde(rename = "org-data")]
pub struct WasmDocument<'s, 'p> {
pub(crate) standard_properties: WasmStandardProperties,
additional_properties: Vec<(String, &'s str)>,
pub(crate) additional_properties: AdditionalProperties<'s, 'p>,
pub(crate) children: Vec<WasmAstNode<'s, 'p>>,
pub(crate) category: Option<&'p str>,
pub(crate) path: Option<PathBuf>,
@ -32,15 +34,15 @@ to_wasm!(
let category = original.category.as_ref().map(String::as_str);
let path = original.path.clone();
let additional_properties: Vec<(String, &str)> = original
.get_additional_properties()
.map(|node_property| {
(
format!(":{}", node_property.property_name.to_uppercase()),
node_property.value.unwrap_or(""),
)
})
.collect();
let mut additional_properties = AdditionalProperties::default();
for (name, val) in original.get_additional_properties().map(|node_property| {
(
node_property.property_name.to_uppercase(),
AdditionalPropertyValue::SingleString(node_property.value.unwrap_or("")),
)
}) {
additional_properties.properties.insert(name, val);
}
let children = original
.zeroth_section

View File

@ -1,3 +1,4 @@
mod additional_property;
mod angle_link;
mod ast_node;
mod babel_call;
@ -62,6 +63,8 @@ mod underline;
mod verbatim;
mod verse_block;
pub use additional_property::AdditionalProperties;
pub use additional_property::AdditionalPropertyValue;
pub use ast_node::WasmAstNode;
pub use document::WasmDocument;
pub(crate) use headline::WasmHeadline;

View File

@ -6,6 +6,10 @@ use crate::compare::get_property_quoted_string;
use crate::compare::ElispFact;
use crate::compare::EmacsField;
use crate::compare::Token;
use crate::util::foreground_color;
use crate::util::reset_color;
use crate::wasm::AdditionalProperties;
use crate::wasm::AdditionalPropertyValue;
use crate::wasm::WasmAstNode;
use crate::wasm::WasmDocument;
use crate::wasm::WasmHeadline;
@ -35,14 +39,6 @@ pub(crate) enum WasmDiffStatus {
}
impl<'s> WasmDiffResult<'s> {
// fn apply(
// &self,
// status: &mut WasmDiffStatus,
// children: &mut Vec<WasmDiffEntry<'s>>,
// ) -> Result<WasmDiffEntry<'s>, Box<dyn std::error::Error>> {
// todo!()
// }
fn extend(
&mut self,
other: WasmDiffResult<'s>,
@ -56,14 +52,72 @@ impl<'s> WasmDiffResult<'s> {
}
pub fn is_bad(&self) -> bool {
todo!()
// self.is_immediately_bad() || self.has_bad_children()
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!()
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(())
}
}
@ -77,49 +131,6 @@ impl<'s> Default for WasmDiffResult<'s> {
}
}
// 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,
emacs: EI,
@ -416,3 +427,35 @@ fn wasm_compare_standard_properties<'b, 's>(
result.children.push(layer);
Ok(result)
}
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)
}

View File

@ -6,7 +6,8 @@ macro_rules! wasm_compare {
let emacs_name = emacs_list_iter
.next()
.ok_or("Should have an attributes child.")?
.as_atom()?;
.as_atom()?;
result.name = emacs_name.into();
let emacs_attributes_map = emacs_list_iter
.next()
.ok_or("Should have an attributes child.")?
@ -40,10 +41,38 @@ macro_rules! wasm_compare {
}
{
// Compare standard properties
// Compare standard properties.
result.extend(wasm_compare_standard_properties($source, $emacs, &$wasm.standard_properties)?)?;
}
{
// Compare additional properties.
let additional_property_names: Vec<String> = $wasm.additional_properties.get_elisp_names().collect();
for additional_property in additional_property_names.iter().map(String::as_str).map(EmacsField::Required) {
match additional_property {
EmacsField::Required(name) if emacs_keys.contains(name) => {
emacs_keys.remove(name);
}
EmacsField::Optional(name) if emacs_keys.contains(name) => {
emacs_keys.remove(name);
}
EmacsField::Required(name) => {
result.status.push(WasmDiffStatus::Bad(
format!(
"Emacs node lacked required field ({name}).",
name = name,
)
.into(),
));
}
EmacsField::Optional(_name) => {}
}
}
result.extend(wasm_compare_additional_properties($source, $emacs, &$wasm.additional_properties)?)?;
}
{
// Compare children.
result.extend(wasm_compare_list(