Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecates Pointer::split_at, adds Pointer::split_at_offset #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@ 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).


## [Unreleased]

### Added

- Adds method `into_buf` for `Box<Pointer>` and `impl From<PathBuf> for Box<Pointer>`.
- Adds method `into_buf` for `Box<Pointer>` and `impl From<PathBuf> for Box<Pointer>`.
- Adds unsafe associated methods `Pointer::new_unchecked` and `PointerBuf::new_unchecked` for
external zero-cost construction.
- Adds `Pointer::starts_with` and `Pointer::ends_with` for prefix and suffix matching.
- Adds new `ParseIndexError` variant to express the presence non-digit characters in the token.
- Adds `Token::is_next` for checking if a token represents the `-` character.
- Adds `Pointer::split_at_offset` to replace the deprecated `Pointer::split_at`.

### Changed

- Changed signature of `PathBuf::parse` to avoid requiring allocation.
- Bumps minimum Rust version to 1.79.
- `Pointer::get` now accepts ranges and can produce `Pointer` segments as output (similar to
`slice::get`).
- Deprecates `Pointer::split_at`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deprecated can have its own section.


### Fixed

Expand Down
32 changes: 31 additions & 1 deletion src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,37 @@ impl Pointer {
/// assert_eq!(tail, Pointer::from_static("/bar/baz"));
/// assert_eq!(ptr.split_at(3), None);
/// ```
#[deprecated(
since = "0.8.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we do 0.7 already?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, no. I skipped ahead. Thanks!

note = "renamed to `split_at_offset` - `split_at` will become position (index) based by 1.0"
)]
pub fn split_at(&self, offset: usize) -> Option<(&Self, &Self)> {
self.split_at_offset(offset)
}

/// Splits the `Pointer` at the given offset 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):
/// ```text
/// /foo/bar/baz
/// ↑ ↑ ↑
/// 0 4 8
/// ```
/// All other indices will return `None`.
///
/// ## Example
///
/// ```rust
/// # use jsonptr::Pointer;
/// 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_offset(3), None);
/// ```
pub fn split_at_offset(&self, offset: usize) -> Option<(&Self, &Self)> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is safe because it returns an Option, but should we maybe make it unsafe (and not check the boundary condition)?

I imagine that one either will have a valid offset to use already, or they'll use split_at instead. Seems unlikely they'll try some offset they're unsure is correct.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the signature the same as split_at assuming we'd deprecate it and phase it out. Then perhaps re-introduce it using the position instead.

I'm truly not sure why I opted to go for offset in hindsight. It's like I spaced on the fact that I had get etc that were index based. Rather annoying mistake on my part.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding split_at_offset_unchecked? The reason I'm hesitant is solely for crates that avoid unsafe entirely.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm truly not sure why I opted to go for offset in hindsight. It's like I spaced on the fact that I had get etc that were index based. Rather annoying mistake on my part.

Don't beat yourself over it, it wasn't as obvious back then. I also didn't realise we could extend the get method to achieve what we really wanted to do with that; if I had, I'd have pushed back more.

What about adding split_at_offset_unchecked? The reason I'm hesitant is solely for crates that avoid unsafe entirely.

Those can opt to use split_at ;)

Unless your concern is over a lack of alternative in the interim? I suppose we can also keep split_at_offset with the current signature, though we'd probably deprecate it too longer term. Feels a bit odd to add something we may remove later, but I think it makes sense in this case.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Good points. You're right that it is odd - especially if we don't end up spacing the releases.

if self.0.as_bytes().get(offset).copied() != Some(b'/') {
return None;
}
Expand Down Expand Up @@ -393,7 +423,7 @@ impl Pointer {
}
idx += a.encoded().len() + 1;
}
self.split_at(idx).map_or(self, |(head, _)| head)
self.split_at_offset(idx).map_or(self, |(head, _)| head)
}

/// Attempts to delete a `serde_json::Value` based upon the path in this
Expand Down
Loading