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<Token<'a>, InvalidEncodingError>
pub fn from_encoded(s: &'a str) -> Result<Token<'a>, InvalidEncodingError>
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>>) -> Token<'a>
pub fn new(s: impl Into<Cow<'a, str>>) -> Token<'a>
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.