organic/src/wasm/document.rs

88 lines
2.6 KiB
Rust
Raw Normal View History

2023-12-27 11:31:35 -05:00
use std::path::PathBuf;
2023-12-24 01:35:21 -05:00
use serde::Serialize;
use super::additional_property::AdditionalProperties;
use super::additional_property::AdditionalPropertyValue;
2023-12-25 11:51:39 -05:00
use super::ast_node::WasmAstNode;
2023-12-24 13:18:06 -05:00
use super::macros::to_wasm;
2023-12-24 15:48:33 -05:00
use super::standard_properties::WasmStandardProperties;
use super::to_wasm::ToWasm;
2023-12-27 16:00:16 -05:00
#[cfg(feature = "wasm_test")]
2023-12-27 15:14:42 -05:00
use crate::compare::ElispFact;
2023-12-27 09:31:54 -05:00
use crate::types::Document;
2023-12-24 15:48:33 -05:00
use crate::wasm::to_wasm::ToWasmStandardProperties;
2023-12-24 13:18:06 -05:00
2023-12-25 12:42:38 -05:00
#[derive(Debug, Serialize)]
#[serde(tag = "__ast_node")]
2023-12-24 01:35:21 -05:00
#[serde(rename = "org-data")]
pub struct WasmDocument<'s, 'p> {
#[serde(rename = "standard-properties")]
2023-12-27 17:07:42 -05:00
pub(crate) standard_properties: WasmStandardProperties,
2023-12-29 10:04:59 -05:00
#[serde(flatten)]
pub(crate) additional_properties: AdditionalProperties<'s, 'p>,
#[serde(rename = "__children")]
2023-12-27 13:21:20 -05:00
pub(crate) children: Vec<WasmAstNode<'s, 'p>>,
#[serde(rename = "CATEGORY")]
2023-12-27 16:34:04 -05:00
pub(crate) category: Option<&'p str>,
pub(crate) path: Option<PathBuf>,
2023-12-24 13:18:06 -05:00
}
2023-12-24 01:35:21 -05:00
2023-12-24 15:48:33 -05:00
to_wasm!(
WasmDocument<'s, 'p>,
2023-12-24 15:48:33 -05:00
Document<'s>,
2023-12-25 12:32:35 -05:00
original,
2023-12-24 15:48:33 -05:00
wasm_context,
standard_properties,
{
let category = original.category.as_ref().map(String::as_str);
2023-12-27 11:31:35 -05:00
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);
}
2023-12-25 12:32:35 -05:00
2023-12-25 12:55:48 -05:00
let children = original
.zeroth_section
.iter()
.map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode<'_, '_>>::into)
2023-12-25 12:55:48 -05:00
})
.chain(original.children.iter().map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode<'_, '_>>::into)
2023-12-25 12:55:48 -05:00
}))
.collect::<Result<Vec<_>, _>>()?;
2023-12-25 12:42:38 -05:00
2023-12-24 15:48:33 -05:00
Ok(WasmDocument {
standard_properties,
2023-12-25 12:42:38 -05:00
additional_properties,
2023-12-25 12:55:48 -05:00
children,
2023-12-27 11:31:35 -05:00
category,
path,
2023-12-24 15:48:33 -05:00
})
2023-12-24 01:35:21 -05:00
}
2023-12-24 15:48:33 -05:00
);
2023-12-25 11:51:39 -05:00
impl<'s, 'p> Into<WasmAstNode<'s, 'p>> for WasmDocument<'s, 'p> {
fn into(self) -> WasmAstNode<'s, 'p> {
2023-12-25 11:51:39 -05:00
WasmAstNode::Document(self)
}
}
2023-12-27 15:14:42 -05:00
2023-12-27 16:00:16 -05:00
#[cfg(feature = "wasm_test")]
2023-12-27 15:14:42 -05:00
impl<'s, 'p> ElispFact<'s> for WasmDocument<'s, 'p> {
fn get_elisp_name<'b>(&'b self) -> std::borrow::Cow<'s, str> {
"org-data".into()
}
}