Implement table.

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

View File

@ -1,3 +1,5 @@
use std::collections::BTreeSet;
use serde::Deserialize;
use serde::Serialize;
@ -6,6 +8,7 @@ use super::macros::to_wasm;
use super::to_wasm::ToWasm;
use super::AdditionalProperties;
use crate::compare::ElispFact;
use crate::types::GetAffiliatedKeywords;
use crate::types::Table;
use crate::wasm::to_wasm::ToWasmStandardProperties;
@ -13,6 +16,18 @@ use crate::wasm::to_wasm::ToWasmStandardProperties;
pub struct WasmTable {
#[serde(flatten)]
pub(crate) additional_properties: AdditionalProperties,
#[serde(rename = "tblfm")]
pub(crate) formulas: Option<WasmStringSet>,
#[serde(rename = "type")]
pub(crate) table_type: String,
pub(crate) value: Option<String>, // Always None
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "string-set")]
#[serde(rename = "string-set")]
struct WasmStringSet {
value: BTreeSet<String>,
}
to_wasm!(
@ -21,8 +36,12 @@ to_wasm!(
original,
wasm_context,
{ WasmAstNode::Table(original) },
{ "TODO".into() },
{ "table".into() },
{
let additional_properties = original
.get_affiliated_keywords()
.to_wasm(wasm_context.clone())?;
let children = original
.children
.iter()
@ -36,7 +55,20 @@ to_wasm!(
Ok((
children,
WasmTable {
additional_properties: AdditionalProperties::default(),
additional_properties,
formulas: if original.formulas.is_empty() {
None
} else {
Some(WasmStringSet {
value: original
.formulas
.iter()
.map(|kw| kw.value.to_owned())
.collect(),
})
},
table_type: "org".to_owned(),
value: None,
},
))
}

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"