Add compare logic for optional pair.
Some checks failed
clippy Build clippy has failed
rust-foreign-document-test Build rust-foreign-document-test has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded

This commit is contained in:
Tom Alexander 2023-12-27 21:23:06 -05:00
parent f227d8405e
commit eef3571299
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -151,6 +151,122 @@ pub(crate) fn wasm_compare_property_list_of_quoted_string<
Ok(result)
}
pub(crate) fn wasm_compare_property_optional_pair<
'b,
's,
W,
WV: AsRef<str> + std::fmt::Debug,
WOV: AsRef<str> + std::fmt::Debug,
WG: Fn(W) -> Option<(Option<WOV>, 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(emacs, emacs_field)?
.map(Token::as_list)
.map_or(Ok(None), |r| r.map(Some))?;
let wasm_value = wasm_value_getter(wasm_node);
match (emacs_value, &wasm_value) {
(None, None) => {}
(None, Some(_)) | (Some(_), None) => {
return Ok(WasmDiffResult {
status: vec![WasmDiffStatus::Bad(
format!(
"Property list length mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = emacs_field,
emacs = if emacs_value.is_some() {"Some"} else {"None"},
wasm = if !emacs_value.is_some() {"Some"} else {"None"}
)
.into(),
)],
children: Vec::new(),
name: "".into(),
});
}
(Some(el), Some((Some(owl), wl))) if el.len() == 3 => {
let e = el
.first()
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.map(unquote)
.map_or(Ok(None), |r| r.map(Some))?
.expect("Above match proved length to be 3.");
let oe = el
.get(2)
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.map(unquote)
.map_or(Ok(None), |r| r.map(Some))?
.expect("Above match proved length to be 3.");
let w = wl.as_ref();
let ow = owl.as_ref();
if e != w {
result.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "end",
emacs = e,
wasm = w,
)
.into(),
));
}
if oe != ow {
result.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "end",
emacs = oe,
wasm = ow,
)
.into(),
));
}
}
(Some(el), Some((None, wl))) if el.len() == 1 => {
let e = el
.first()
.map(Token::as_atom)
.map_or(Ok(None), |r| r.map(Some))?
.map(unquote)
.map_or(Ok(None), |r| r.map(Some))?
.expect("Above match proved length to be 3.");
let w = wl.as_ref();
if e != w {
result.status.push(WasmDiffStatus::Bad(
format!(
"Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = "end",
emacs = e,
wasm = w,
)
.into(),
));
}
}
(Some(el), Some(_)) => {
return Ok(WasmDiffResult {
status: vec![WasmDiffStatus::Bad(
format!(
"Property list length mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).",
property = emacs_field,
emacs = el.len(),
wasm = wasm_value
)
.into(),
)],
children: Vec::new(),
name: "".into(),
});
}
}
Ok(result)
}
pub(crate) fn wasm_compare_standard_properties<'b, 's>(
_source: &'s str,
emacs: &'b Token<'s>,
@ -253,7 +369,15 @@ pub(crate) fn wasm_compare_additional_properties<'b, 's>(
)?)?;
}
// TODO: similar to compare_affiliated_keywords
AdditionalPropertyValue::OptionalPair { optval, val } => todo!(),
AdditionalPropertyValue::OptionalPair { optval, val } => {
layer.extend(wasm_compare_property_optional_pair(
source,
emacs,
wasm,
&emacs_property_name,
|_| Some((*optval, *val)),
)?)?;
}
AdditionalPropertyValue::ObjectTree(_) => todo!(),
}
}