From 7e93bc645447ecf1aa30afe5deb5a4ffd2a6cd9c Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 23 May 2024 00:19:28 +0200 Subject: [PATCH] fix(apb): simpler macros, added into value, fix links before array of links were silently discarded because macros relied on into_iter().collect()... oops! technical debt, but should be ok now --- apb/src/macros.rs | 49 +++++++---------------------------------------- apb/src/node.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+), 42 deletions(-) diff --git a/apb/src/macros.rs b/apb/src/macros.rs index 3a46c59..bc29122 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 d8b427a..aa369d0 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()), + } + } +}