diff --git a/apb/src/node.rs b/apb/src/node.rs index 506b7492..0ff4eb65 100644 --- a/apb/src/node.rs +++ b/apb/src/node.rs @@ -99,22 +99,21 @@ impl Node { } /// returns id of object: url for link, id for object, None if empty or array - // TODO return Option<&str> and avoid inner clone - pub fn id(&self) -> Option { + pub fn id(&self) -> crate::Field<&str> { match self { - Node::Empty => None, - Node::Link(uri) => Some(uri.href().to_string()), - Node::Object(obj) => Some(obj.id().ok()?.to_string()), - Node::Array(arr) => Some(arr.front()?.id()?.to_string()), + Node::Empty => Err(crate::FieldErr("id")), + Node::Link(uri) => Ok(uri.href()), + Node::Object(obj) => obj.id(), + Node::Array(arr) => arr.front().map(|x| x.id()).ok_or(crate::FieldErr("id"))?, } } - pub fn ids(&self) -> Vec { + pub fn all_ids(&self) -> Vec { match self { Node::Empty => vec![], Node::Link(uri) => vec![uri.href().to_string()], Node::Object(x) => x.id().map_or(vec![], |x| vec![x.to_string()]), - Node::Array(x) => x.iter().filter_map(Self::id).collect() + Node::Array(x) => x.iter().filter_map(|x| x.id().ok()).map(|x| x.to_string()).collect() } }