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 new() -> PointerBuf
pub fn new() -> PointerBuf
Creates a new PointerBuf
pointing to a document root.
Sourcepub fn parse<S>(s: &S) -> Result<PointerBuf, ParseError>
pub fn parse<S>(s: &S) -> Result<PointerBuf, ParseError>
Attempts to parse a string into a PointerBuf
.
§Errors
Returns a ParseError
if the string is not a valid JSON Pointer.
Sourcepub fn from_tokens<'a, T>(tokens: impl IntoIterator<Item = T>) -> PointerBuf
pub fn from_tokens<'a, T>(tokens: impl IntoIterator<Item = T>) -> PointerBuf
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>(&mut self, other: P) -> &PointerBuf
pub fn append<P>(&mut self, other: P) -> &PointerBuf
Merges two Pointer
s 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<'_>, &Pointer)>
pub fn split_front(&self) -> Option<(Token<'_>, &Pointer)>
Splits the Pointer
into the first Token
and a remainder Pointer
.
Sourcepub fn split_at(&self, offset: usize) -> Option<(&Pointer, &Pointer)>
pub fn split_at(&self, offset: usize) -> Option<(&Pointer, &Pointer)>
Splits the Pointer
at the given index if the character at the index is
a separator backslash ('/'
), 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 8
All 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<(&Pointer, Token<'_>)>
pub fn split_back(&self) -> Option<(&Pointer, Token<'_>)>
Splits the Pointer
into the parent path and the last Token
.
Sourcepub fn strip_suffix<'a>(&'a self, suffix: &Pointer) -> Option<&'a Pointer>
pub fn strip_suffix<'a>(&'a self, suffix: &Pointer) -> Option<&'a Pointer>
Returns the pointer stripped of the given suffix.
Sourcepub fn strip_prefix<'a>(&'a self, prefix: &Pointer) -> Option<&'a Pointer>
pub fn strip_prefix<'a>(&'a self, prefix: &Pointer) -> Option<&'a Pointer>
Returns the pointer stripped of the given prefix.
Sourcepub fn get(&self, index: usize) -> Option<Token<'_>>
pub fn get(&self, index: usize) -> Option<Token<'_>>
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");
assert_eq!(ptr.get(0), Some("foo".into()));
assert_eq!(ptr.get(1), Some("bar".into()));
assert_eq!(ptr.get(2), None);
let ptr = Pointer::root();
assert_eq!(ptr.get(0), None);
Sourcepub fn resolve<'v, R>(
&self,
value: &'v R,
) -> Result<&'v <R as Resolve>::Value, <R as Resolve>::Error>where
R: Resolve,
pub fn resolve<'v, R>(
&self,
value: &'v R,
) -> Result<&'v <R as Resolve>::Value, <R as Resolve>::Error>where
R: Resolve,
Sourcepub fn resolve_mut<'v, R>(
&self,
value: &'v mut R,
) -> Result<&'v mut <R as ResolveMut>::Value, <R as ResolveMut>::Error>where
R: ResolveMut,
pub fn resolve_mut<'v, R>(
&self,
value: &'v mut R,
) -> Result<&'v mut <R as ResolveMut>::Value, <R as ResolveMut>::Error>where
R: ResolveMut,
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: &Pointer) -> &'a Pointer
pub fn intersection<'a>(&'a self, other: &Pointer) -> &'a Pointer
Finds the commonality between this and another Pointer
.
Sourcepub fn delete<D>(&self, value: &mut D) -> Option<<D as Delete>::Value>where
D: Delete,
pub fn delete<D>(&self, value: &mut D) -> Option<<D as Delete>::Value>where
D: Delete,
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
Pointer
can be resolved, theValue
is deleted and returned. - If the
Pointer
fails to resolve for any reason,Ok(None)
is returned. - If the
Pointer
is root,value
is 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 as Assign>::Value>, <D as Assign>::Error>
pub fn assign<D, V>( &self, dest: &mut D, src: V, ) -> Result<Option<<D as Assign>::Value>, <D as Assign>::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 once for a 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::Pointer::from_static("/foo/bar").to_buf();
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<PointerBuf, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<PointerBuf, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Display for PointerBuf
impl Display for PointerBuf
Source§impl From<Token<'_>> for PointerBuf
impl From<Token<'_>> for PointerBuf
Source§fn from(t: Token<'_>) -> PointerBuf
fn from(t: Token<'_>) -> PointerBuf
Source§impl From<usize> for PointerBuf
impl From<usize> for PointerBuf
Source§fn from(value: usize) -> PointerBuf
fn from(value: usize) -> PointerBuf
Source§impl FromStr for PointerBuf
impl FromStr for PointerBuf
Source§type Err = ParseError
type Err = ParseError
Source§fn from_str(s: &str) -> Result<PointerBuf, <PointerBuf as FromStr>::Err>
fn from_str(s: &str) -> Result<PointerBuf, <PointerBuf as FromStr>::Err>
s
to return a value of this type. Read moreSource§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 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 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§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Source§impl TryFrom<&str> for PointerBuf
impl TryFrom<&str> for PointerBuf
Source§type Error = ParseError
type Error = ParseError
Source§fn try_from(
value: &str,
) -> Result<PointerBuf, <PointerBuf as TryFrom<&str>>::Error>
fn try_from( value: &str, ) -> Result<PointerBuf, <PointerBuf as TryFrom<&str>>::Error>
Source§impl TryFrom<String> for PointerBuf
impl TryFrom<String> for PointerBuf
Source§type Error = ParseError
type Error = ParseError
Source§fn try_from(
value: String,
) -> Result<PointerBuf, <PointerBuf as TryFrom<String>>::Error>
fn try_from( value: String, ) -> Result<PointerBuf, <PointerBuf as TryFrom<String>>::Error>
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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)