1
0
Fork 0
forked from alemi/upub

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
This commit is contained in:
əlemi 2024-05-23 00:19:28 +02:00
parent dd16279003
commit 7e93bc6454
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 19 additions and 42 deletions

View file

@ -339,27 +339,10 @@ pub(crate) use setter;
#[cfg(feature = "unstructured")] #[cfg(feature = "unstructured")]
pub fn set_maybe_node(obj: &mut serde_json::Value, key: &str, node: crate::Node<serde_json::Value>) { pub fn set_maybe_node(obj: &mut serde_json::Value, key: &str, node: crate::Node<serde_json::Value>) {
match node { if node.is_nothing() {
crate::Node::Object(x) => { set_maybe_value(obj, key, None)
set_maybe_value( } else {
obj, key, Some(*x), set_maybe_value(obj, key, Some(node.into()))
);
},
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,
);
},
} }
} }
@ -386,27 +369,9 @@ pub(crate) trait InsertValue {
#[cfg(feature = "unstructured")] #[cfg(feature = "unstructured")]
impl InsertValue for serde_json::Map<String, serde_json::Value> { impl InsertValue for serde_json::Map<String, serde_json::Value> {
fn insert_node(&mut self, k: &str, node: crate::Node<serde_json::Value>) { fn insert_node(&mut self, k: &str, node: crate::Node<serde_json::Value>) {
match node { if !node.is_nothing() {
crate::Node::Object(x) => { self.insert(k.to_string(), node.into());
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 => {},
};
} }
fn insert_str(&mut self, k: &str, v: Option<&str>) { fn insert_str(&mut self, k: &str, v: Option<&str>) {

View file

@ -222,3 +222,15 @@ impl From<serde_json::Value> for Node<serde_json::Value> {
} }
} }
#[cfg(feature = "unstructured")]
impl From<Node<serde_json::Value>> for serde_json::Value {
fn from(value: Node<serde_json::Value>) -> 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()),
}
}
}