pub struct PointerBuf(/* private fields */);Expand description
An owned, mutable Pointer (akin to String).
This type provides methods like PointerBuf::push_back and
PointerBuf::replace that mutate the pointer in place. It also
implements core::ops::Deref to Pointer, meaning that all methods on
Pointer slices are available on PointerBuf values as well.
Implementations§
Source§impl PointerBuf
impl PointerBuf
Sourcepub fn root() -> Self
pub fn root() -> Self
Creates a new PointerBuf pointing to a document root.
This is an alias to Self::new.
Sourcepub unsafe fn new_unchecked(s: impl Into<String>) -> Self
pub unsafe fn new_unchecked(s: impl Into<String>) -> Self
Create a PointerBuf from a string that is known to be correctly encoded.
§Safety
The provided string must adhere to RFC 6901.
Sourcepub fn parse(s: impl Into<String>) -> Result<Self, RichParseError>
pub fn parse(s: impl Into<String>) -> Result<Self, RichParseError>
Attempts to parse a string into a PointerBuf.
§Errors
Returns a RichParseError if the string is not a valid JSON Pointer.
Sourcepub fn from_tokens<'t>(tokens: impl IntoIterator<Item: Into<Token<'t>>>) -> Self
pub fn from_tokens<'t>(tokens: impl IntoIterator<Item: Into<Token<'t>>>) -> Self
Creates a new PointerBuf from a slice of non-encoded strings.
Sourcepub fn push_front<'t>(&mut self, token: impl Into<Token<'t>>)
pub fn push_front<'t>(&mut self, token: impl Into<Token<'t>>)
Pushes a Token onto the front of this Pointer.
Sourcepub fn push_back<'t>(&mut self, token: impl Into<Token<'t>>)
pub fn push_back<'t>(&mut self, token: impl Into<Token<'t>>)
Pushes a Token onto the back of this Pointer.
Sourcepub fn pop_back(&mut self) -> Option<Token<'static>>
pub fn pop_back(&mut self) -> Option<Token<'static>>
Removes and returns the last Token in the Pointer if it exists.
Sourcepub fn pop_front(&mut self) -> Option<Token<'static>>
pub fn pop_front(&mut self) -> Option<Token<'static>>
Removes and returns the first Token in the Pointer if it exists.
Sourcepub fn append<P: AsRef<Pointer>>(&mut self, other: P) -> &PointerBuf
pub fn append<P: AsRef<Pointer>>(&mut self, other: P) -> &PointerBuf
Merges two Pointers by appending other onto self.
Sourcepub fn replace<'t>(
&mut self,
index: usize,
token: impl Into<Token<'t>>,
) -> Result<Option<Token<'_>>, ReplaceError>
pub fn replace<'t>( &mut self, index: usize, token: impl Into<Token<'t>>, ) -> Result<Option<Token<'_>>, ReplaceError>
Attempts to replace a Token by the index, returning the replaced
Token if it already exists. Returns None otherwise.
§Errors
A [ReplaceError] is returned if the index is out of bounds.
Methods from Deref<Target = Pointer>§
Sourcepub fn to_buf(&self) -> PointerBuf
pub fn to_buf(&self) -> PointerBuf
Converts into an owned PointerBuf
Sourcepub fn to_json_value(&self) -> Value
pub fn to_json_value(&self) -> Value
Returns a serde_json::Value representation of this Pointer
Sourcepub fn first(&self) -> Option<Token<'_>>
pub fn first(&self) -> Option<Token<'_>>
Returns the first Token in the Pointer.
alias for front
Sourcepub fn split_front(&self) -> Option<(Token<'_>, &Self)>
pub fn split_front(&self) -> Option<(Token<'_>, &Self)>
Splits the Pointer into the first Token and a remainder Pointer.
Sourcepub fn split_at(&self, offset: usize) -> Option<(&Self, &Self)>
pub fn split_at(&self, offset: usize) -> Option<(&Self, &Self)>
Splits the Pointer at the given index if the character at the index is
a separator slash ('/'), returning Some((head, tail)). Otherwise,
returns None.
For the following JSON Pointer, the following splits are possible (0, 4, 8):
/foo/bar/baz
↑ ↑ ↑
0 4 8All other indices will return None.
§Example
let ptr = Pointer::from_static("/foo/bar/baz");
let (head, tail) = ptr.split_at(4).unwrap();
assert_eq!(head, Pointer::from_static("/foo"));
assert_eq!(tail, Pointer::from_static("/bar/baz"));
assert_eq!(ptr.split_at(3), None);Sourcepub fn split_back(&self) -> Option<(&Self, Token<'_>)>
pub fn split_back(&self) -> Option<(&Self, Token<'_>)>
Splits the Pointer into the parent path and the last Token.
Sourcepub fn strip_suffix<'a>(&'a self, suffix: &Self) -> Option<&'a Self>
pub fn strip_suffix<'a>(&'a self, suffix: &Self) -> Option<&'a Self>
Returns the pointer stripped of the given suffix.
Sourcepub fn strip_prefix<'a>(&'a self, prefix: &Self) -> Option<&'a Self>
pub fn strip_prefix<'a>(&'a self, prefix: &Self) -> Option<&'a Self>
Returns the pointer stripped of the given prefix.
Sourcepub fn ends_with(&self, other: &Self) -> bool
pub fn ends_with(&self, other: &Self) -> bool
Returns whether self has a suffix of other.
Note that Pointer::root is only a valid suffix of itself.
Sourcepub fn starts_with(&self, other: &Self) -> bool
pub fn starts_with(&self, other: &Self) -> bool
Returns whether self has a prefix of other.
Note that Pointer::root is a valid prefix of any Pointer (including
itself).
Sourcepub fn get<'p, I>(&'p self, index: I) -> Option<I::Output>where
I: PointerIndex<'p>,
pub fn get<'p, I>(&'p self, index: I) -> Option<I::Output>where
I: PointerIndex<'p>,
Attempts to get a Token by the index. Returns None if the index is
out of bounds.
§Example
use jsonptr::{Pointer, Token};
let ptr = Pointer::from_static("/foo/bar/qux");
assert_eq!(ptr.get(0), Some("foo".into()));
assert_eq!(ptr.get(1), Some("bar".into()));
assert_eq!(ptr.get(3), None);
assert_eq!(ptr.get(..), Some(Pointer::from_static("/foo/bar/qux")));
assert_eq!(ptr.get(..1), Some(Pointer::from_static("/foo")));
assert_eq!(ptr.get(1..3), Some(Pointer::from_static("/bar/qux")));
assert_eq!(ptr.get(1..=2), Some(Pointer::from_static("/bar/qux")));
let ptr = Pointer::root();
assert_eq!(ptr.get(0), None);
assert_eq!(ptr.get(..), Some(Pointer::root()));Sourcepub fn resolve<'v, R: Resolve>(
&self,
value: &'v R,
) -> Result<&'v R::Value, R::Error>
pub fn resolve<'v, R: Resolve>( &self, value: &'v R, ) -> Result<&'v R::Value, R::Error>
Sourcepub fn resolve_mut<'v, R: ResolveMut>(
&self,
value: &'v mut R,
) -> Result<&'v mut R::Value, R::Error>
pub fn resolve_mut<'v, R: ResolveMut>( &self, value: &'v mut R, ) -> Result<&'v mut R::Value, R::Error>
Attempts to resolve a mutable R::Value based on the path in this
Pointer.
§Errors
Returns R::Error if an error occurs while
resolving.
The rules of such are determined by the R’s implementation of
ResolveMut but provided implementations return ResolveError if:
Sourcepub fn intersection<'a>(&'a self, other: &Self) -> &'a Self
pub fn intersection<'a>(&'a self, other: &Self) -> &'a Self
Finds the commonality between this and another Pointer.
Sourcepub fn delete<D: Delete>(&self, value: &mut D) -> Option<D::Value>
pub fn delete<D: Delete>(&self, value: &mut D) -> Option<D::Value>
Attempts to delete a serde_json::Value based upon the path in this
Pointer.
The rules of deletion are determined by the D’s implementation of
Delete. The supplied implementations ("json" & "toml") operate
as follows:
- If the
Pointercan be resolved, theValueis deleted and returned. - If the
Pointerfails to resolve for any reason,Ok(None)is returned. - If the
Pointeris root,valueis replaced:"json":serde_json::Value::Null"toml":toml::Value::Table::Default
§Examples
§Deleting a resolved pointer:
use jsonptr::{Pointer, delete::Delete};
use serde_json::json;
let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::from_static("/foo/bar/baz");
assert_eq!(data.delete(&ptr), Some("qux".into()));
assert_eq!(data, json!({ "foo": { "bar": {} } }));§Deleting a non-existent Pointer returns None:
use jsonptr::{ Pointer, delete::Delete };
use serde_json::json;
let mut data = json!({});
let ptr = Pointer::from_static("/foo/bar/baz");
assert_eq!(ptr.delete(&mut data), None);
assert_eq!(data, json!({}));§Deleting a root pointer replaces the value with Value::Null:
use jsonptr::{Pointer, delete::Delete};
use serde_json::json;
let mut data = json!({ "foo": { "bar": "baz" } });
let ptr = Pointer::root();
assert_eq!(data.delete(&ptr), Some(json!({ "foo": { "bar": "baz" } })));
assert!(data.is_null());Sourcepub fn assign<D, V>(
&self,
dest: &mut D,
src: V,
) -> Result<Option<D::Value>, D::Error>
pub fn assign<D, V>( &self, dest: &mut D, src: V, ) -> Result<Option<D::Value>, D::Error>
Attempts to assign src to dest based on the path in this Pointer.
If the path is partially available, the missing portions will be created. If the path
contains a zero index, such as "/0", then an array will be created. Otherwise, objects
will be utilized to create the missing path.
§Example
use jsonptr::Pointer;
use serde_json::{json, Value};
let mut data = json!([]);
let mut ptr = Pointer::from_static("/0/foo");
let replaced = ptr.assign(&mut data, json!("bar")).unwrap();
assert_eq!(data, json!([{"foo": "bar"}]));
assert_eq!(replaced, None);§Errors
Returns Assign::Error if the path is invalid or if the value cannot be assigned.
Sourcepub fn components(&self) -> Components<'_> ⓘ
pub fn components(&self) -> Components<'_> ⓘ
Returns Components of this JSON Pointer.
A Component is either Token or the root
location of a document.
§Example
let ptr = Pointer::parse("/a/b").unwrap();
let mut components = ptr.components();
assert_eq!(components.next(), Some(Component::Root));
assert_eq!(components.next(), Some(Component::Token("a".into())));
assert_eq!(components.next(), Some(Component::Token("b".into())));
assert_eq!(components.next(), None);Sourcepub fn with_trailing_token<'t>(&self, token: impl Into<Token<'t>>) -> PointerBuf
pub fn with_trailing_token<'t>(&self, token: impl Into<Token<'t>>) -> PointerBuf
Creates an owned PointerBuf like self but with token appended.
See PointerBuf::push_back for more details.
Note: this method allocates. If you find yourself calling it more
than once for a given pointer, consider using PointerBuf::push_back
instead.
§Examples
let ptr = jsonptr::Pointer::from_static("/foo");
let foobar = ptr.with_trailing_token("bar");
assert_eq!(foobar, "/foo/bar");Sourcepub fn with_leading_token<'t>(&self, token: impl Into<Token<'t>>) -> PointerBuf
pub fn with_leading_token<'t>(&self, token: impl Into<Token<'t>>) -> PointerBuf
Creates an owned PointerBuf like self but with token prepended.
See PointerBuf::push_front for more details.
Note: this method allocates. If you find yourself calling it more
than once for a given pointer, consider using PointerBuf::push_front
instead.
§Examples
let ptr = jsonptr::Pointer::from_static("/bar");
let foobar = ptr.with_leading_token("foo");
assert_eq!(foobar, "/foo/bar");Sourcepub fn concat(&self, other: &Pointer) -> PointerBuf
pub fn concat(&self, other: &Pointer) -> PointerBuf
Creates an owned PointerBuf like self but with other appended to
the end.
See PointerBuf::append for more details.
Note: this method allocates. If you find yourself calling it more
than given pointer, consider using PointerBuf::append
instead.
§Examples
let ptr = jsonptr::Pointer::from_static("/foo");
let other = jsonptr::Pointer::from_static("/bar/baz");
assert_eq!(ptr.concat(other), "/foo/bar/baz");Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
This length expresses the byte count of the underlying string that
represents the RFC 6091 Pointer. See also [std::str::len].
§Examples
let mut ptr = jsonptr::PointerBuf::parse("/foo/bar").unwrap();
assert_eq!(ptr.len(), 8);
ptr.push_back("~");
assert_eq!(ptr.len(), 11);
Trait Implementations§
Source§impl AsRef<Pointer> for PointerBuf
impl AsRef<Pointer> for PointerBuf
Source§impl Borrow<Pointer> for PointerBuf
impl Borrow<Pointer> for PointerBuf
Source§impl Clone for PointerBuf
impl Clone for PointerBuf
Source§fn clone(&self) -> PointerBuf
fn clone(&self) -> PointerBuf
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PointerBuf
impl Debug for PointerBuf
Source§impl Default for PointerBuf
impl Default for PointerBuf
Source§fn default() -> PointerBuf
fn default() -> PointerBuf
Source§impl Deref for PointerBuf
impl Deref for PointerBuf
Source§impl<'de> Deserialize<'de> for PointerBuf
impl<'de> Deserialize<'de> for PointerBuf
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for PointerBuf
impl Display for PointerBuf
Source§impl From<PointerBuf> for Box<Pointer>
impl From<PointerBuf> for Box<Pointer>
Source§fn from(value: PointerBuf) -> Self
fn from(value: PointerBuf) -> Self
Source§impl From<PointerBuf> for Cow<'static, Pointer>
impl From<PointerBuf> for Cow<'static, Pointer>
Source§fn from(value: PointerBuf) -> Self
fn from(value: PointerBuf) -> Self
Source§impl From<Token<'_>> for PointerBuf
impl From<Token<'_>> for PointerBuf
Source§impl From<usize> for PointerBuf
impl From<usize> for PointerBuf
Source§impl FromStr for PointerBuf
impl FromStr for PointerBuf
Source§impl Hash for PointerBuf
impl Hash for PointerBuf
Source§impl<'a> IntoIterator for &'a PointerBuf
impl<'a> IntoIterator for &'a PointerBuf
Source§impl Ord for PointerBuf
impl Ord for PointerBuf
Source§fn cmp(&self, other: &PointerBuf) -> Ordering
fn cmp(&self, other: &PointerBuf) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq<&Pointer> for PointerBuf
impl PartialEq<&Pointer> for PointerBuf
Source§impl PartialEq<&str> for PointerBuf
impl PartialEq<&str> for PointerBuf
Source§impl PartialEq<Pointer> for PointerBuf
impl PartialEq<Pointer> for PointerBuf
Source§impl PartialEq<PointerBuf> for &Pointer
impl PartialEq<PointerBuf> for &Pointer
Source§impl PartialEq<PointerBuf> for &str
impl PartialEq<PointerBuf> for &str
Source§impl PartialEq<PointerBuf> for Pointer
impl PartialEq<PointerBuf> for Pointer
Source§impl PartialEq<PointerBuf> for String
impl PartialEq<PointerBuf> for String
Source§impl PartialEq<PointerBuf> for str
impl PartialEq<PointerBuf> for str
Source§impl PartialEq<String> for PointerBuf
impl PartialEq<String> for PointerBuf
Source§impl PartialEq<str> for PointerBuf
impl PartialEq<str> for PointerBuf
Source§impl PartialEq for PointerBuf
impl PartialEq for PointerBuf
Source§impl PartialOrd<&Pointer> for PointerBuf
impl PartialOrd<&Pointer> for PointerBuf
Source§impl PartialOrd<&str> for PointerBuf
impl PartialOrd<&str> for PointerBuf
Source§impl PartialOrd<Pointer> for PointerBuf
impl PartialOrd<Pointer> for PointerBuf
Source§impl<'p> PartialOrd<PointerBuf> for &'p Pointer
impl<'p> PartialOrd<PointerBuf> for &'p Pointer
Source§impl PartialOrd<PointerBuf> for &str
impl PartialOrd<PointerBuf> for &str
Source§impl PartialOrd<PointerBuf> for Pointer
impl PartialOrd<PointerBuf> for Pointer
Source§impl PartialOrd<PointerBuf> for String
impl PartialOrd<PointerBuf> for String
Source§impl PartialOrd<PointerBuf> for str
impl PartialOrd<PointerBuf> for str
Source§impl PartialOrd<String> for PointerBuf
impl PartialOrd<String> for PointerBuf
Source§impl PartialOrd for PointerBuf
impl PartialOrd for PointerBuf
Source§impl Serialize for PointerBuf
impl Serialize for PointerBuf
Source§impl TryFrom<&str> for PointerBuf
impl TryFrom<&str> for PointerBuf
Source§impl TryFrom<String> for PointerBuf
impl TryFrom<String> for PointerBuf
impl Eq for PointerBuf
impl StructuralPartialEq for PointerBuf
Auto Trait Implementations§
impl Freeze for PointerBuf
impl RefUnwindSafe for PointerBuf
impl Send for PointerBuf
impl Sync for PointerBuf
impl Unpin for PointerBuf
impl UnwindSafe for PointerBuf
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.