From 7a78b07467878a56764c1743641cc1c2973b37a3 Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 20 Nov 2024 18:03:31 +0100 Subject: [PATCH] chore(apb): renamed .get and .extract are now .inner() and .into_inner() --- apb/src/node.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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)