diff --git a/apb/src/node.rs b/apb/src/node.rs index 1772f6c..c0716df 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -43,6 +43,7 @@ impl From> for Node { impl Node { /// return reference to embedded object (or first if many are present) + #[deprecated = "use .inner() instead"] pub fn get(&self) -> Option<&T> { match self { Node::Empty | Node::Link(_) => None, @@ -51,7 +52,8 @@ impl Node { } } - /// return reference to embedded object (or first if many are present) + /// return embedded object (or first if many are present) + #[deprecated = "use .into_inner() instead"] pub fn extract(self) -> Option { match self { Node::Empty | Node::Link(_) => None, @@ -60,6 +62,26 @@ impl Node { } } + /// return reference to embedded object (or first if many are present) + pub fn inner(&self) -> crate::Field<&T> { + match self { + Node::Empty => Err(crate::FieldErr("node is empty")), + Node::Link(_) => Err(crate::FieldErr("node has not been dereferenced")), + Node::Object(x) => Ok(x), + Node::Array(v) => v.iter().next().ok_or(crate::FieldErr("node contains no items"))?.inner(), + } + } + + /// return embedded object (or first if many are present) + pub fn into_inner(self) -> crate::Field { + match self { + Node::Empty => Err(crate::FieldErr("node is empty")), + Node::Link(_) => Err(crate::FieldErr("node has not been dereferenced")), + Node::Object(x) => Ok(*x), + Node::Array(v) => v.into_iter().next().ok_or(crate::FieldErr("node contains no items"))?.into_inner(), + } + } + /// true only if Node is empty pub fn is_empty(&self) -> bool { matches!(self, Node::Empty)