Add conversion for affiliated keywords to wasm additional properties.
This commit is contained in:
parent
28ad4fd046
commit
9520e5814b
@ -2,7 +2,11 @@ use std::collections::HashMap;
|
||||
|
||||
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)]
|
||||
pub enum AdditionalPropertyValue<'s, 'p> {
|
||||
@ -25,3 +29,65 @@ impl<'s, 'p> AdditionalProperties<'s, 'p> {
|
||||
self.properties.keys().map(move |key| format!(":{}", key))
|
||||
}
|
||||
}
|
||||
|
||||
to_wasm!(
|
||||
AdditionalProperties<'s, 'p>,
|
||||
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)
|
||||
}
|
||||
AffiliatedKeywordValue::ListOfStrings(val) => {
|
||||
AdditionalPropertyValue::ListOfStrings(val.clone())
|
||||
}
|
||||
AffiliatedKeywordValue::OptionalPair { optval, val } => {
|
||||
AdditionalPropertyValue::OptionalPair {
|
||||
optval: optval.clone(),
|
||||
val: val,
|
||||
}
|
||||
}
|
||||
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(ret)
|
||||
}
|
||||
};
|
||||
additional_properties
|
||||
.properties
|
||||
.insert(name.clone(), converted_val);
|
||||
}
|
||||
Ok(additional_properties)
|
||||
}
|
||||
);
|
||||
|
@ -2,6 +2,19 @@
|
||||
///
|
||||
/// This exists to make changing the type signature easier.
|
||||
macro_rules! to_wasm {
|
||||
($ostruct:ty, $istruct:ty, $original:ident, $wasm_context:ident, $fnbody:tt) => {
|
||||
impl<'s, 'p> ToWasm<'p> for $istruct {
|
||||
type Output = $ostruct;
|
||||
|
||||
fn to_wasm(
|
||||
&'p self,
|
||||
$wasm_context: crate::wasm::to_wasm::ToWasmContext<'_>,
|
||||
) -> Result<Self::Output, crate::error::CustomError> {
|
||||
let $original = self;
|
||||
$fnbody
|
||||
}
|
||||
}
|
||||
};
|
||||
($ostruct:ty, $istruct:ty, $original:ident, $wasm_context:ident, $standard_properties:ident, $fnbody:tt) => {
|
||||
impl<'s, 'p> ToWasm<'p> for $istruct {
|
||||
type Output = $ostruct;
|
||||
|
@ -3,6 +3,10 @@ use serde::Serialize;
|
||||
use super::macros::to_wasm;
|
||||
use super::standard_properties::WasmStandardProperties;
|
||||
use super::to_wasm::ToWasm;
|
||||
use super::AdditionalProperties;
|
||||
#[cfg(feature = "wasm_test")]
|
||||
use crate::compare::ElispFact;
|
||||
use crate::types::GetAffiliatedKeywords;
|
||||
use crate::types::Paragraph;
|
||||
use crate::wasm::to_wasm::ToWasmStandardProperties;
|
||||
use crate::wasm::WasmAstNode;
|
||||
@ -11,8 +15,9 @@ use crate::wasm::WasmAstNode;
|
||||
#[serde(tag = "ast_node")]
|
||||
#[serde(rename = "paragraph")]
|
||||
pub struct WasmParagraph<'s, 'p> {
|
||||
standard_properties: WasmStandardProperties,
|
||||
children: Vec<WasmAstNode<'s, 'p>>,
|
||||
pub(crate) standard_properties: WasmStandardProperties,
|
||||
pub(crate) additional_properties: AdditionalProperties<'s, 'p>,
|
||||
pub(crate) children: Vec<WasmAstNode<'s, 'p>>,
|
||||
}
|
||||
|
||||
to_wasm!(
|
||||
@ -22,6 +27,10 @@ to_wasm!(
|
||||
wasm_context,
|
||||
standard_properties,
|
||||
{
|
||||
let additional_properties = original
|
||||
.get_affiliated_keywords()
|
||||
.to_wasm(wasm_context.clone())?;
|
||||
|
||||
let children = original
|
||||
.children
|
||||
.iter()
|
||||
@ -34,6 +43,7 @@ to_wasm!(
|
||||
|
||||
Ok(WasmParagraph {
|
||||
standard_properties,
|
||||
additional_properties,
|
||||
children,
|
||||
})
|
||||
}
|
||||
@ -44,3 +54,10 @@ impl<'s, 'p> Into<WasmAstNode<'s, 'p>> for WasmParagraph<'s, 'p> {
|
||||
WasmAstNode::Paragraph(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "wasm_test")]
|
||||
impl<'s, 'p> ElispFact<'s> for WasmParagraph<'s, 'p> {
|
||||
fn get_elisp_name<'b>(&'b self) -> std::borrow::Cow<'s, str> {
|
||||
"paragraph".into()
|
||||
}
|
||||
}
|
||||
|
@ -227,23 +227,7 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmParagraph<'s, 'p> {
|
||||
source: &'s str,
|
||||
emacs: &'b Token<'s>,
|
||||
) -> Result<WasmDiffResult<'s>, Box<dyn std::error::Error>> {
|
||||
// TODO: Implement this.
|
||||
let result = WasmDiffResult::default();
|
||||
// let result = wasm_compare!(
|
||||
// source,
|
||||
// emacs,
|
||||
// self,
|
||||
// (
|
||||
// EmacsField::Required(":path"),
|
||||
// |w| w.path.as_ref().and_then(|p| p.to_str()),
|
||||
// wasm_compare_property_quoted_string
|
||||
// ),
|
||||
// (
|
||||
// EmacsField::Required(":CATEGORY"),
|
||||
// |w| w.category.as_ref(),
|
||||
// wasm_compare_property_quoted_string
|
||||
// )
|
||||
// );
|
||||
let result = wasm_compare!(source, emacs, self,);
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user