Implement table.

This commit is contained in:
Tom Alexander
2023-12-30 18:25:15 -05:00
parent 46c36d7f3e
commit 4e18cbafba
2 changed files with 84 additions and 2 deletions

View File

@@ -1,4 +1,6 @@
use std::borrow::Borrow;
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::collections::HashMap;
use super::diff::WasmDiffResult;
@@ -58,6 +60,10 @@ fn compare_json_value<'b, 's>(
// We hit an object tree additional property.
compare_number_lines(source, el, wasm)
}
(serde_json::Value::Object(wasm), Token::List(el)) if wasm.contains_key("string-set") => {
// We hit an object tree additional property.
compare_string_set(source, el, wasm)
}
(serde_json::Value::Object(w), Token::TextWithProperties(e)) if is_plain_text(w) => {
compare_plain_text(source, e, w)
}
@@ -719,6 +725,50 @@ fn compare_number_lines<'e, 's, 'w>(
Ok(result)
}
fn compare_string_set<'e, 's, 'w>(
source: &'s str,
emacs: &'e Vec<Token<'s>>,
wasm: &'w serde_json::Map<String, serde_json::Value>,
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
let mut result = WasmDiffResult::default();
let wasm_list = wasm
.get("value")
.ok_or(r#"Wasm string set should have a "value" attribute."#)?
.as_array()
.ok_or(r#"Wasm string set "value" attribute should be a list."#)?;
let wasm_strings = wasm_list
.iter()
.map(|v| v.as_str().ok_or("Non-string in wasm string set."))
.collect::<Result<BTreeSet<_>, &'static str>>()?;
let emacs_strings = emacs
.iter()
.map(|v| v.as_atom())
.collect::<Result<Vec<_>, Box<dyn std::error::Error>>>()?
.into_iter()
.map(|s| unquote(s))
.collect::<Result<Vec<_>, Box<dyn std::error::Error>>>()?;
let emacs_strings = emacs_strings
.iter()
.map(|s| s.borrow())
.collect::<BTreeSet<&str>>();
let mismatched: Vec<_> = emacs_strings
.symmetric_difference(&wasm_strings)
.copied()
.collect();
if !mismatched.is_empty() {
result.status.push(WasmDiffStatus::Bad(
format!(
"String set mismatch. MismatchedValues=({values:?}).",
values = mismatched.join(", ")
)
.into(),
));
}
Ok(result)
}
fn is_plain_text<'e, 's, 'w>(wasm: &'w serde_json::Map<String, serde_json::Value>) -> bool {
if let Some(serde_json::Value::String(node_type)) = wasm.get("ast-node") {
node_type == "plain-text"