pub struct Pointer<'a>(/* private fields */);
Expand description
JSON pointer representation based on RFC6901.
This type offers strong ordering over the underlying Unicode string:
- JSON pointers are sorted by ascending depth.
- JSON pointers with the same depth are alphanumerically sorted.
Implementations§
Source§impl<'a> Pointer<'a>
impl<'a> Pointer<'a>
Sourcepub fn new(s: impl Into<Cow<'a, str>>) -> Result<Self, Error>
pub fn new(s: impl Into<Cow<'a, str>>) -> Result<Self, Error>
Creates a Pointer
from a Unicode string as describe in RFC6901.
§Arguments
s
: A Unicode string representing a JSON pointer.
§Examples
// Construct a `Pointer` from a string literal.
let pointer = Pointer::new("/a/b/c").unwrap();
// Construct a `Pointer` from a owned string.
let pointer = Pointer::new(String::from("/a/b/c")).unwrap();
Sourcepub fn key(&self) -> Option<String>
pub fn key(&self) -> Option<String>
Returns the last reference token of the JSON pointer, also called JSON key.
Note that the root pointer does not contains any reference tokens and so no JSON key.
§Example
let pointer = Pointer::new("/key").unwrap();
assert_eq!(pointer.key(), Some("key".to_string()));
let pointer = Pointer::root();
assert!(pointer.key().is_none());
Sourcepub fn parent(&self) -> Option<Pointer<'_>>
pub fn parent(&self) -> Option<Pointer<'_>>
Returns the parent JSON pointer.
Note that the returned JSON pointer borrows a part of the underlying Unicode string then it can be
clone
without any extra allocation.
§Example
let pointer = Pointer::new("/nested/key").unwrap();
let parent_pointer = Pointer::new("/nested").unwrap();
assert_eq!(pointer.parent(), Some(parent_pointer));
Sourcepub fn ancestors(&self) -> impl Iterator<Item = Pointer<'_>>
pub fn ancestors(&self) -> impl Iterator<Item = Pointer<'_>>
Produces an iterator over Pointer
and its parent JSON pointers.
As Pointer::parent
method, all the returned JSON pointers borrow parts of the underlying Unicode string
then any of them can be clone
without any extra allocation.
The iterator will yield the Pointer
then its parents like self
, self.parent().unwrap()
,
self.parent().unwrap().parent().unwrap()
and so on until reaching the root JSON pointer.
§Examples
let pointer = Pointer::new("/foo/bar/zoo").unwrap();
let ancestors = pointer.ancestors().collect::<Vec<_>>();
assert_eq!(
ancestors,
vec![
Pointer::new("/foo/bar/zoo").unwrap(),
Pointer::new("/foo/bar").unwrap(),
Pointer::new("/foo").unwrap(),
Pointer::root()
]
);
Sourcepub fn is_ancestor_of(&self, other: &Pointer<'_>) -> bool
pub fn is_ancestor_of(&self, other: &Pointer<'_>) -> bool
Indicates if Pointer
is an ancestor of the given JSON pointer.
Note that Pointer
is an ancestor of itself.
Sourcepub fn is_parent_of(&self, other: &Pointer<'_>) -> bool
pub fn is_parent_of(&self, other: &Pointer<'_>) -> bool
Indicates if Pointer
is a parent of the given JSON pointer.
Note that the root JSON pointer is the only one with no parent.
Sourcepub fn is_sibling_of(&self, other: &Pointer<'_>) -> bool
pub fn is_sibling_of(&self, other: &Pointer<'_>) -> bool
Indicates if Pointer
is a sibling of the given JSON pointer.
Sourcepub fn depth(&self) -> usize
pub fn depth(&self) -> usize
Indicates the number of reference tokens in the JSON pointer, in a zero-based indexed way.
Sourcepub fn into_owned(self) -> Pointer<'static>
pub fn into_owned(self) -> Pointer<'static>
Creates an owned instance of Pointer
.
Note that this function may call Clone::clone
if the underlying Unicode string is borrowed.