Struct jsonptr::PointerBuf
source · 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 parse<S: AsRef<str> + ?Sized>(s: &S) -> Result<Self, ParseError>
pub fn parse<S: AsRef<str> + ?Sized>(s: &S) -> Result<Self, 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>) -> Self
pub fn from_tokens<'a, T>(tokens: impl IntoIterator<Item = T>) -> Self
Creates a new PointerBuf from a slice of non-encoded strings.
sourcepub fn push_front<'t, T>(&mut self, token: T)
pub fn push_front<'t, T>(&mut self, token: T)
Pushes a Token onto the front 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.
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 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<(&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 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: 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);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<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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)