Files
organic/src/wasm/additional_property.rs
Tom Alexander f4e0dddd9d
All checks were successful
clippy Build clippy has succeeded
rust-foreign-document-test Build rust-foreign-document-test has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded
Fix clippy.
2023-12-30 23:14:40 -05:00

96 lines
3.3 KiB
Rust

use std::collections::HashMap;
use serde::Deserialize;
use serde::Serialize;
use super::macros::to_wasm;
use super::to_wasm::ToWasm;
use super::WasmAstNode;
use crate::types::AffiliatedKeywordValue;
use crate::types::AffiliatedKeywords;
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AdditionalPropertyValue {
SingleString(String),
ListOfStrings(Vec<String>),
OptionalPair {
optval: Option<String>,
val: String,
},
ObjectTree {
#[serde(rename = "object-tree")]
object_tree: Vec<(Option<Vec<WasmAstNode>>, Vec<WasmAstNode>)>,
},
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct AdditionalProperties {
#[serde(flatten)]
pub(crate) properties: HashMap<String, AdditionalPropertyValue>,
}
to_wasm!(
AdditionalProperties,
AffiliatedKeywords<'s>,
original,
wasm_context,
{
let mut additional_properties = AdditionalProperties::default();
for (name, val) in original.keywords.iter() {
let converted_val = match val {
AffiliatedKeywordValue::SingleString(val) => {
AdditionalPropertyValue::SingleString((*val).to_owned())
}
AffiliatedKeywordValue::ListOfStrings(val) => {
AdditionalPropertyValue::ListOfStrings(
val.iter().map(|s| (*s).to_owned()).collect(),
)
}
AffiliatedKeywordValue::OptionalPair { optval, val } => {
AdditionalPropertyValue::OptionalPair {
optval: optval.map(|s| (*s).to_owned()),
val: (*val).to_owned(),
}
}
AffiliatedKeywordValue::ObjectTree(val) => {
let mut ret = Vec::with_capacity(val.len());
for (optval, value) in val {
let converted_optval = if let Some(optval) = optval {
Some(
optval
.iter()
.map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode>::into)
})
.collect::<Result<Vec<_>, _>>()?,
)
} else {
None
};
let converted_value = value
.iter()
.map(|child| {
child
.to_wasm(wasm_context.clone())
.map(Into::<WasmAstNode>::into)
})
.collect::<Result<Vec<_>, _>>()?;
ret.push((converted_optval, converted_value));
}
AdditionalPropertyValue::ObjectTree { object_tree: ret }
}
};
additional_properties
.properties
.insert(name.clone(), converted_val);
}
Ok(additional_properties)
}
);