organic/src/wasm/document.rs
Tom Alexander 77e0dbb42e
Start working on a version of compare based on json values.
This will be a better test because it will be testing that what we export to json is equivalent to the elisp AST generated from emacs. Because of these tests, we could also confidently use the wasm structure to elisp.
2023-12-29 11:37:30 -05:00

88 lines
2.6 KiB
Rust

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;
use super::to_wasm::ToWasm;
#[cfg(feature = "wasm_test")]
use crate::compare::ElispFact;
use crate::types::Document;
use crate::wasm::to_wasm::ToWasmStandardProperties;
#[derive(Debug, Serialize)]
#[serde(tag = "__ast_node")]
#[serde(rename = "org-data")]
pub struct WasmDocument<'s, 'p> {
#[serde(rename = "standard-properties")]
pub(crate) standard_properties: WasmStandardProperties,
#[serde(flatten)]
pub(crate) additional_properties: AdditionalProperties<'s, 'p>,
#[serde(rename = "__children")]
pub(crate) children: Vec<WasmAstNode<'s, 'p>>,
#[serde(rename = "CATEGORY")]
pub(crate) category: Option<&'p str>,
pub(crate) path: Option<PathBuf>,
}
to_wasm!(
WasmDocument<'s, 'p>,
Document<'s>,
original,
wasm_context,
standard_properties,
{
let category = original.category.as_ref().map(String::as_str);
let path = original.path.clone();
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
.iter()
.map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode<'_, '_>>::into)
})
.chain(original.children.iter().map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode<'_, '_>>::into)
}))
.collect::<Result<Vec<_>, _>>()?;
Ok(WasmDocument {
standard_properties,
additional_properties,
children,
category,
path,
})
}
);
impl<'s, 'p> Into<WasmAstNode<'s, 'p>> for WasmDocument<'s, 'p> {
fn into(self) -> WasmAstNode<'s, 'p> {
WasmAstNode::Document(self)
}
}
#[cfg(feature = "wasm_test")]
impl<'s, 'p> ElispFact<'s> for WasmDocument<'s, 'p> {
fn get_elisp_name<'b>(&'b self) -> std::borrow::Cow<'s, str> {
"org-data".into()
}
}