Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- Added `Token::into_encoded`, which consumes the `Token` and returns its
encoded string representation as a `Cow<'a, str>` without allocating.
Resolves [#115](https://github.com/chanced/jsonptr/issues/115).

### Changed

- Resolved all `clippy` (`clippy::all` + `clippy::pedantic`) and `rustc` lint
Expand Down
25 changes: 25 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ impl<'a> Token<'a> {
&self.inner
}

/// Converts this `Token` into the encoded string representation, as a
/// `Cow<'a, str>`.
///
/// This is like [`Self::encoded`], except it consumes the `Token` and,
/// since the token is already encoded internally, does not allocate.
///
/// # Examples
///
/// ```
/// # use jsonptr::Token;
/// assert_eq!(Token::new("~bar").into_encoded(), "~0bar");
/// ```
pub fn into_encoded(self) -> Cow<'a, str> {
self.inner
}

/// Returns the decoded string representation of the `Token`.
///
/// # Examples
Expand Down Expand Up @@ -589,6 +605,15 @@ mod tests {
);
}

#[test]
fn into_encoded() {
let token = Token::from_encoded("foo~0").unwrap();
assert_eq!(token.into_encoded(), "foo~0");

let token = Token::new("~bar");
assert_eq!(token.into_encoded(), "~0bar");
}

#[test]
fn into_owned() {
let token = Token::from_encoded("foo~0").unwrap().into_owned();
Expand Down
Loading