Skip to content

Commit

Permalink
adds len and is_empty to Pointer (#69)
Browse files Browse the repository at this point in the history
* adds `len` and `is_empty` to `Pointer`
  • Loading branch information
chanced authored Sep 30, 2024
1 parent 4c71d51 commit 7d6cd24
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.1]
## [Unreleased]

### Added

- Adds methods `len` and `is_empty` to `Pointer`

## [0.6.1] 2024-09-26

## Added

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ potentially fallible parsing, or the `const fn` `from_static` to produce a
`&'static Pointer` from a string that is known to be valid.

```rust
# use jsonptr::Pointer;
let ptr = Pointer::parse("/examples/0/name").unwrap();
use jsonptr::Pointer;

let ptr = Pointer::parse("/examples/0/name").unwrap();
let static_ptr = Pointer::from_static("/examples/0/name");
assert_eq!(ptr, static_ptr);

Expand All @@ -58,7 +58,7 @@ assert_eq!(remaining, Pointer::parse("/0/name").unwrap());
iterator of [`Token`]s with the [`from_tokens`] method:

```rust
# use jsonptr::PointerBuf;
use jsonptr::PointerBuf;
let mut buf = PointerBuf::parse("/examples/0/name").unwrap();

let from_tokens = PointerBuf::from_tokens(["examples", "0", "name"]);
Expand All @@ -73,7 +73,7 @@ assert_eq!(buf.as_str(), "/~0/pointer/examples/0/name/~1");
Iterating over the tokens or components of a pointer:

```rust
# use jsonptr::{Pointer, Component, Token};
use jsonptr::{Pointer, Component, Token};
let ptr = Pointer::from_static("/path/to/value");

// Using the `tokens` method:
Expand Down
107 changes: 102 additions & 5 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl Pointer {
self.into()
}

/// Creates an owned [`Pointerbuf`] like `self` but with `token` appended.
/// Creates an owned [`PointerBuf`] like `self` but with `token` appended.
///
/// See [`PointerBuf::push_back`] for more details.
///
Expand All @@ -443,7 +443,7 @@ impl Pointer {
buf
}

/// Creates an owned [`Pointerbuf`] like `self` but with `token` prepended.
/// Creates an owned [`PointerBuf`] like `self` but with `token` prepended.
///
/// See [`PointerBuf::push_front`] for more details.
///
Expand All @@ -463,7 +463,7 @@ impl Pointer {
buf
}

/// Creates an owned [`Pointerbuf`] like `self` but with `other` appended to
/// Creates an owned [`PointerBuf`] like `self` but with `other` appended to
/// the end.
///
/// See [`PointerBuf::append`] for more details.
Expand All @@ -475,14 +475,46 @@ impl Pointer {
/// ## Examples
/// ```
/// let ptr = jsonptr::Pointer::from_static("/foo");
/// let barbaz = jsonptr::Pointer::from_static("/bar/baz");
/// assert_eq!(ptr.concat(&barbaz), "/foo/bar/baz");
/// let other = jsonptr::Pointer::from_static("/bar/baz");
/// assert_eq!(ptr.concat(other), "/foo/bar/baz");
/// ```
pub fn concat(&self, other: &Pointer) -> PointerBuf {
let mut buf = self.to_buf();
buf.append(other);
buf
}

// Returns the length of `self` in encoded format.
///
/// This length is in bytes, not [`char`]s or graphemes. In other words, it might
/// not be what a human considers the length of the string.
///
/// ## 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);
///
/// ```
pub fn len(&self) -> usize {
self.0.len()
}

/// Returns `true` if the `Pointer` is empty (i.e. root).
///
/// ## Examples
/// ```
/// let mut ptr = jsonptr::PointerBuf::new();
/// assert!(ptr.is_empty());
///
/// ptr.push_back("foo");
/// assert!(!ptr.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -800,6 +832,35 @@ impl PointerBuf {
self
}

// Returns the length of `self` in encoded format.
///
/// This length is in bytes, not [`char`]s or graphemes. In other words, it might
/// not be what a human considers the length of the string.
///
/// ## Examples
/// ```
/// let ptr = jsonptr::Pointer::from_static("/foo/bar");
/// assert_eq!(ptr.len(), 8);
///
/// ```
pub fn len(&self) -> usize {
self.0.len()
}

/// Returns `true` if the `Pointer` is empty (i.e. root).
///
/// ## Examples
/// ```
/// let mut ptr = jsonptr::PointerBuf::new();
/// assert!(ptr.is_empty());
///
/// ptr.push_back("foo");
/// assert!(!ptr.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Pushes a `Token` onto the front of this `Pointer`.
pub fn push_front<'t>(&mut self, token: impl Into<Token<'t>>) {
self.0.insert(0, '/');
Expand Down Expand Up @@ -1884,6 +1945,42 @@ mod tests {
assert_eq!(buf, "/bread/crumbs");
}

#[test]
fn concat() {
let ptr = Pointer::from_static("/foo");
let barbaz = Pointer::from_static("/bar/baz");
assert_eq!(ptr.concat(barbaz), "/foo/bar/baz");
}

#[test]
fn with_leading_token() {
let ptr = Pointer::from_static("/bar");
let foobar = ptr.with_leading_token("foo");
assert_eq!(foobar, "/foo/bar");
}

#[test]
fn with_trailing_token() {
let ptr = Pointer::from_static("/foo");
let foobar = ptr.with_trailing_token("bar");
assert_eq!(foobar, "/foo/bar");
}

#[test]
fn len() {
let ptr = Pointer::from_static("/foo/bar");
assert_eq!(ptr.len(), 8);
let mut ptr = ptr.to_buf();
ptr.push_back("~");
assert_eq!(ptr.len(), 11);
}

#[test]
fn is_empty() {
assert!(Pointer::from_static("").is_empty());
assert!(!Pointer::from_static("/").is_empty());
}

#[test]
#[allow(clippy::cmp_owned, unused_must_use)]
fn partial_eq() {
Expand Down

0 comments on commit 7d6cd24

Please sign in to comment.