pub struct Token<'a> { /* private fields */ }
Expand description
A Token
is a segment of a JSON Pointer
, preceded by '/'
(%x2F
).
Token
s can represent a key in a JSON object or an index in an array.
- Indexes should not contain leading zeros.
- When dealing with arrays or path expansion for assignment,
"-"
represent the next, non-existent index in a JSON array.
Implementations§
Source§impl<'a> Token<'a>
impl<'a> Token<'a>
Sourcepub fn from_encoded(s: &'a str) -> Result<Self, EncodingError>
pub fn from_encoded(s: &'a str) -> Result<Self, EncodingError>
Constructs a Token
from an RFC 6901 encoded string.
To be valid, the string must not contain any /
characters, and any ~
characters must be followed by either 0
or 1
.
This function does not allocate.
§Examples
assert_eq!(Token::from_encoded("~1foo~1~0bar").unwrap().decoded(), "/foo/~bar");
let err = Token::from_encoded("foo/oops~bar").unwrap_err();
assert_eq!(err.offset, 3);
§Errors
Returns InvalidEncodingError
if the input string is not a valid RFC
6901 (~
must be followed by 0
or 1
)
Sourcepub fn new(s: impl Into<Cow<'a, str>>) -> Self
pub fn new(s: impl Into<Cow<'a, str>>) -> Self
Constructs a Token
from an arbitrary string.
If the string contains a /
or a ~
, then it will be assumed not
encoded, in which case this function will encode it, allocating a new
string.
If the string is already encoded per RFC 6901, use
Self::from_encoded
instead, otherwise it will end up double-encoded.
§Examples
assert_eq!(Token::new("/foo/~bar").encoded(), "~1foo~1~0bar");
Sourcepub fn into_owned(self) -> Token<'static>
pub fn into_owned(self) -> Token<'static>
Converts into an owned copy of this token.
If the token is not already owned, this will clone the referenced string slice.
Sourcepub fn to_owned(&self) -> Token<'static>
pub fn to_owned(&self) -> Token<'static>
Extracts an owned copy of this token.
If the token is not already owned, this will clone the referenced string slice.
This method is like Self::into_owned
, except it doesn’t take
ownership of the original Token
.
Sourcepub fn encoded(&self) -> &str
pub fn encoded(&self) -> &str
Returns the encoded string representation of the Token
.
§Examples
assert_eq!(Token::new("~bar").encoded(), "~0bar");
Sourcepub fn decoded(&self) -> Cow<'_, str>
pub fn decoded(&self) -> Cow<'_, str>
Returns the decoded string representation of the Token
.
§Examples
assert_eq!(Token::new("~bar").decoded(), "~bar");
Sourcepub fn to_index(&self) -> Result<Index, ParseIndexError>
pub fn to_index(&self) -> Result<Index, ParseIndexError>
Attempts to parse the given Token
as an array index.
Per RFC 6901,
the acceptable values are non-negative integers and the -
character,
which stands for the next, non-existent member after the last array
element.
§Examples
assert_eq!(Token::new("-").to_index(), Ok(Index::Next));
assert_eq!(Token::new("0").to_index(), Ok(Index::Num(0)));
assert_eq!(Token::new("2").to_index(), Ok(Index::Num(2)));
assert!(Token::new("a").to_index().is_err());
assert!(Token::new("-1").to_index().is_err());
§Errors
Returns ParseIndexError
if the token is not a valid array index.
Sourcepub fn is_next(&self) -> bool
pub fn is_next(&self) -> bool
Returns if the Token
is -
, which stands for the next array index.
See also Self::to_index
.
Trait Implementations§
Source§impl From<Token<'_>> for PointerBuf
impl From<Token<'_>> for PointerBuf
Source§impl<'a> Ord for Token<'a>
impl<'a> Ord for Token<'a>
Source§impl<'a> PartialOrd for Token<'a>
impl<'a> PartialOrd for Token<'a>
impl<'a> Eq for Token<'a>
impl<'a> StructuralPartialEq for Token<'a>
Auto Trait Implementations§
impl<'a> Freeze for Token<'a>
impl<'a> RefUnwindSafe for Token<'a>
impl<'a> Send for Token<'a>
impl<'a> Sync for Token<'a>
impl<'a> Unpin for Token<'a>
impl<'a> UnwindSafe for Token<'a>
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.