diff --git a/apb/src/macros.rs b/apb/src/macros.rs index 3a46c59a..bc291227 100644 --- a/apb/src/macros.rs +++ b/apb/src/macros.rs @@ -339,27 +339,10 @@ pub(crate) use setter; #[cfg(feature = "unstructured")] pub fn set_maybe_node(obj: &mut serde_json::Value, key: &str, node: crate::Node) { - match node { - crate::Node::Object(x) => { - set_maybe_value( - obj, key, Some(*x), - ); - }, - crate::Node::Link(l) => { - set_maybe_value( - obj, key, Some(serde_json::Value::String(l.href().to_string())), - ); - }, - crate::Node::Array(_) => { - set_maybe_value( - obj, key, Some(serde_json::Value::Array(node.into_iter().collect())), - ); - }, - crate::Node::Empty => { - set_maybe_value( - obj, key, None, - ); - }, + if node.is_nothing() { + set_maybe_value(obj, key, None) + } else { + set_maybe_value(obj, key, Some(node.into())) } } @@ -386,27 +369,9 @@ pub(crate) trait InsertValue { #[cfg(feature = "unstructured")] impl InsertValue for serde_json::Map { fn insert_node(&mut self, k: &str, node: crate::Node) { - match node { - crate::Node::Object(x) => { - self.insert( - k.to_string(), - *x, - ); - }, - crate::Node::Array(ref _arr) => { - self.insert( - k.to_string(), - serde_json::Value::Array(node.into_iter().collect()), - ); - }, - crate::Node::Link(l) => { - self.insert( - k.to_string(), - serde_json::Value::String(l.href().to_string()), - ); - }, - crate::Node::Empty => {}, - }; + if !node.is_nothing() { + self.insert(k.to_string(), node.into()); + } } fn insert_str(&mut self, k: &str, v: Option<&str>) { diff --git a/apb/src/node.rs b/apb/src/node.rs index d8b427a6..aa369d0d 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -222,3 +222,15 @@ impl From for Node { } } +#[cfg(feature = "unstructured")] +impl From> for serde_json::Value { + fn from(value: Node) -> Self { + match value { + Node::Empty => serde_json::Value::Null, + Node::Link(l) => serde_json::Value::String(l.href().to_string()), // TODO there could be more + Node::Object(o) => *o, + Node::Array(arr) => + serde_json::Value::Array(arr.into_iter().map(|x| x.into()).collect()), + } + } +}