2023-12-27 18:01:56 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
2023-12-29 15:03:36 -05:00
|
|
|
use serde::Deserialize;
|
2023-12-27 18:01:56 -05:00
|
|
|
use serde::Serialize;
|
|
|
|
|
|
2023-12-27 20:33:02 -05:00
|
|
|
use super::macros::to_wasm;
|
|
|
|
|
use super::to_wasm::ToWasm;
|
2023-12-27 18:01:56 -05:00
|
|
|
use super::WasmAstNode;
|
2023-12-27 20:33:02 -05:00
|
|
|
use crate::types::AffiliatedKeywordValue;
|
|
|
|
|
use crate::types::AffiliatedKeywords;
|
2023-12-27 18:01:56 -05:00
|
|
|
|
2023-12-29 15:03:36 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2023-12-29 10:04:59 -05:00
|
|
|
#[serde(untagged)]
|
2023-12-29 12:49:43 -05:00
|
|
|
pub enum AdditionalPropertyValue {
|
|
|
|
|
SingleString(String),
|
|
|
|
|
ListOfStrings(Vec<String>),
|
2023-12-29 17:46:35 -05:00
|
|
|
OptionalPair {
|
|
|
|
|
optval: Option<String>,
|
|
|
|
|
val: String,
|
|
|
|
|
},
|
|
|
|
|
ObjectTree {
|
|
|
|
|
#[serde(rename = "object-tree")]
|
|
|
|
|
object_tree: Vec<(Option<Vec<WasmAstNode>>, Vec<WasmAstNode>)>,
|
|
|
|
|
},
|
2023-12-27 18:01:56 -05:00
|
|
|
}
|
|
|
|
|
|
2023-12-29 15:03:36 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
2023-12-29 12:49:43 -05:00
|
|
|
pub struct AdditionalProperties {
|
2023-12-29 10:04:59 -05:00
|
|
|
#[serde(flatten)]
|
2023-12-29 12:49:43 -05:00
|
|
|
pub(crate) properties: HashMap<String, AdditionalPropertyValue>,
|
2023-12-27 18:01:56 -05:00
|
|
|
}
|
|
|
|
|
|
2023-12-27 20:33:02 -05:00
|
|
|
to_wasm!(
|
2023-12-29 12:49:43 -05:00
|
|
|
AdditionalProperties,
|
2023-12-27 20:33:02 -05:00
|
|
|
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) => {
|
2023-12-29 12:49:43 -05:00
|
|
|
AdditionalPropertyValue::SingleString((*val).to_owned())
|
2023-12-27 20:33:02 -05:00
|
|
|
}
|
|
|
|
|
AffiliatedKeywordValue::ListOfStrings(val) => {
|
2023-12-29 12:49:43 -05:00
|
|
|
AdditionalPropertyValue::ListOfStrings(
|
|
|
|
|
val.iter().map(|s| (*s).to_owned()).collect(),
|
|
|
|
|
)
|
2023-12-27 20:33:02 -05:00
|
|
|
}
|
|
|
|
|
AffiliatedKeywordValue::OptionalPair { optval, val } => {
|
|
|
|
|
AdditionalPropertyValue::OptionalPair {
|
2023-12-29 12:49:43 -05:00
|
|
|
optval: optval.map(|s| (*s).to_owned()),
|
|
|
|
|
val: (*val).to_owned(),
|
2023-12-27 20:33:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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())
|
2023-12-29 12:49:43 -05:00
|
|
|
.map(Into::<WasmAstNode>::into)
|
2023-12-27 20:33:02 -05:00
|
|
|
})
|
|
|
|
|
.collect::<Result<Vec<_>, _>>()?,
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
let converted_value = value
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|child| {
|
|
|
|
|
child
|
|
|
|
|
.to_wasm(wasm_context.clone())
|
2023-12-29 12:49:43 -05:00
|
|
|
.map(Into::<WasmAstNode>::into)
|
2023-12-27 20:33:02 -05:00
|
|
|
})
|
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
|
|
|
|
|
ret.push((converted_optval, converted_value));
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 17:46:35 -05:00
|
|
|
AdditionalPropertyValue::ObjectTree { object_tree: ret }
|
2023-12-27 20:33:02 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
additional_properties
|
|
|
|
|
.properties
|
|
|
|
|
.insert(name.clone(), converted_val);
|
|
|
|
|
}
|
|
|
|
|
Ok(additional_properties)
|
|
|
|
|
}
|
|
|
|
|
);
|