organic/src/wasm/document.rs

73 lines
1.9 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;
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 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)]
2023-12-24 01:35:21 -05:00
#[serde(tag = "ast_node")]
#[serde(rename = "org-data")]
2023-12-27 09:31:54 -05:00
pub struct WasmDocument<'s> {
2023-12-24 15:48:33 -05:00
standard_properties: WasmStandardProperties,
2023-12-25 12:42:38 -05:00
additional_properties: Vec<(String, &'s str)>,
children: Vec<WasmAstNode<'s>>,
2023-12-27 11:31:35 -05:00
category: Option<String>,
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!(
2023-12-25 11:19:09 -05:00
WasmDocument<'s>,
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,
{
2023-12-27 11:31:35 -05:00
let category = original.category.clone();
let path = original.path.clone();
2023-12-25 12:32:35 -05:00
let additional_properties: Vec<(String, &str)> = original
.get_additional_properties()
.map(|node_property| {
(
format!(":{}", node_property.property_name.to_uppercase()),
node_property.value.unwrap_or(""),
)
})
.collect();
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)
})
.chain(original.children.iter().map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode<'_>>::into)
}))
.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> Into<WasmAstNode<'s>> for WasmDocument<'s> {
fn into(self) -> WasmAstNode<'s> {
WasmAstNode::Document(self)
}
}