Move the logic functions into their own module.

This commit is contained in:
Tom Alexander 2023-12-27 19:22:43 -05:00
parent 5a64db98fe
commit 121c0ce516
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 185 additions and 180 deletions

View File

@ -1,23 +1,18 @@
use std::borrow::Cow;
use super::diff::WasmDiffResult;
use super::diff::WasmDiffStatus;
use super::elisp_compare::WasmElispCompare;
use crate::compare::get_emacs_standard_properties;
use crate::compare::get_property_quoted_string;
use super::logic::wasm_compare_additional_properties;
use super::logic::wasm_compare_list;
use super::logic::wasm_compare_property_quoted_string;
use super::logic::wasm_compare_standard_properties;
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;
use crate::wasm::WasmParagraph;
use crate::wasm::WasmSection;
use crate::wasm::WasmStandardProperties;
use crate::wasm_test::macros::wasm_compare;
pub fn wasm_compare_document<'b, 's, 'p>(
@ -28,44 +23,6 @@ pub fn wasm_compare_document<'b, 's, 'p>(
wasm.compare_ast_node(source, emacs)
}
fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>(
source: &'s str,
emacs: EI,
wasm: WI,
) -> 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>,
{
let status = Vec::new();
let emacs_length = emacs.len();
let wasm_length = wasm.len();
if emacs_length != wasm_length {
return Ok(WasmDiffResult {
status: vec![WasmDiffStatus::Bad(
format!(
"Child length mismatch (emacs != rust) {:?} != {:?}",
emacs_length, wasm_length
)
.into(),
)],
children: Vec::new(),
name: "".into(),
});
}
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)?);
}
Ok(WasmDiffResult {
status,
children: child_status,
name: "".into(),
})
}
impl<'s, 'p, WAN: WasmElispCompare<'s, 'p>> WasmElispCompare<'s, 'p> for &WAN {
fn compare_ast_node<'b>(
&self,
@ -235,136 +192,3 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmParagraph<'s, 'p> {
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)
}
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)
}
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)
}

180
src/wasm_test/logic.rs Normal file
View File

@ -0,0 +1,180 @@
use super::diff::WasmDiffResult;
use super::diff::WasmDiffStatus;
use super::elisp_compare::WasmElispCompare;
use crate::compare::get_emacs_standard_properties;
use crate::compare::get_property_quoted_string;
use crate::compare::Token;
use crate::wasm::AdditionalProperties;
use crate::wasm::AdditionalPropertyValue;
use crate::wasm::WasmStandardProperties;
pub(crate) fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>(
source: &'s str,
emacs: EI,
wasm: WI,
) -> 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>,
{
let status = Vec::new();
let emacs_length = emacs.len();
let wasm_length = wasm.len();
if emacs_length != wasm_length {
return Ok(WasmDiffResult {
status: vec![WasmDiffStatus::Bad(
format!(
"Child length mismatch (emacs != rust) {:?} != {:?}",
emacs_length, wasm_length
)
.into(),
)],
children: Vec::new(),
name: "".into(),
});
}
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)?);
}
Ok(WasmDiffResult {
status,
children: child_status,
name: "".into(),
})
}
pub(crate) 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)
}
pub(crate) 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)
}
pub(crate) 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

@ -1,6 +1,7 @@
mod compare;
mod diff;
mod elisp_compare;
mod logic;
mod macros;
mod runner;