diff --git a/CHANGELOG.md b/CHANGELOG.md index 42bcffe..15dd2c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). -## [Unreleased] +## [0.7.1] 2025-02-16 + +### Changed + +- Removes accidentally enabled default features `"miette"` and `"toml"` + +## [0.7.0] 2025-02-13 ### Added diff --git a/Cargo.lock b/Cargo.lock index b85f907..71a1d06 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -135,7 +135,7 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jsonptr" -version = "0.7.0" +version = "0.7.1" dependencies = [ "miette", "quickcheck", diff --git a/Cargo.toml b/Cargo.toml index 6ef6b2b..31ecca8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ license = "MIT OR Apache-2.0" name = "jsonptr" repository = "https://github.com/chanced/jsonptr" rust-version = "1.79.0" -version = "0.7.0" +version = "0.7.1" [dependencies] miette = { version = "7.4.0", optional = true, features = ["fancy"] } @@ -31,20 +31,11 @@ quickcheck_macros = "1.0.0" syn = { version = "1.0.109", optional = true } [features] -assign = [] -default = [ - "std", - "serde", - "json", - "toml", # TODO: remove - "resolve", - "assign", - "delete", - "miette", # TODO: remove -] -delete = ["resolve"] -json = ["dep:serde_json", "serde"] -miette = ["dep:miette", "std"] +assign = [] +default = ["std", "serde", "json", "resolve", "assign", "delete"] +delete = ["resolve"] +json = ["dep:serde_json", "serde"] +miette = ["dep:miette", "std"] resolve = [] -std = ["serde/std", "serde_json?/std"] -toml = ["dep:toml", "serde", "std"] +std = ["serde/std", "serde_json?/std"] +toml = ["dep:toml", "serde", "std"] diff --git a/README.md b/README.md index c3b4797..a971337 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ flags](#feature-flags). ## Usage +### Parsing and General Usage + To parse a [`Pointer`] from a string, use either [`Pointer::parse`], for potentially fallible parsing, or the `const fn` `from_static` to produce a `&'static Pointer` from a string that is known to be valid. @@ -46,6 +48,8 @@ let ptr = Pointer::parse("/examples/0/name").unwrap(); let static_ptr = Pointer::from_static("/examples/0/name"); assert_eq!(ptr, static_ptr); +assert_eq!(ptr.get(1..).unwrap(), Pointer::parse("/0/name").unwrap()); + let parent = ptr.parent().unwrap(); assert_eq!(parent, Pointer::parse("/examples/0").unwrap()); @@ -70,6 +74,8 @@ buf.push_back("/"); assert_eq!(buf.as_str(), "/~0/pointer/examples/0/name/~1"); ``` +### Token Iteration + Iterating over the tokens or components of a pointer: ```rust @@ -88,6 +94,8 @@ assert_eq!(components.next(), Some(Component::Token(Token::new("to")))); assert_eq!(components.next(), Some(Component::Token(Token::new("value")))); ``` +### Resolving Values + To get a value at the location of a pointer, use either the [`Resolve`] and [`ResolveMut`] traits or [`Pointer::resolve`] and [`Pointer::resolve_mut`] methods. See the [`resolve`] mod for more information. @@ -102,6 +110,8 @@ let bar = ptr.resolve(&data).unwrap(); assert_eq!(bar, &json!(34)); ``` +### Assigning Values + Values can be set, with path expansion, using the either the [`Assign`] trait or [`Pointer::assign`]. See [`assign`] for more information. @@ -116,6 +126,8 @@ assert_eq!(replaced, Some(json!(42))); assert_eq!(data, json!({"secret": { "universe": 34 }})); ``` +### Deleting Values + Values can be removed with the either the [`Delete`] trait or [`Pointer::delete`]. See [`delete`] for more information. @@ -130,6 +142,30 @@ assert_eq!(replaced, Some(json!(42))); assert_eq!(data, json!({"secret": { "universe": 34 }})); ``` +### Error Reporting + +Any error produced by function calls into methods of traits or types of this +crate can be converted into a [`Report`] which contains the original error +and the [`String`] which failed to parse or the [`PointerBuf`] which failed to +resolve or assign. + +```rust + use jsonptr::{Pointer, Diagnose}; + let ptr_str = "foo/bar"; + let err /* Result<&Pointer, Report> */ = Pointer::parse(ptr_str).diagnose(ptr_str).unwrap_err(); + assert!(err.original().is_no_leading_slash()); +``` + +In the case of [`PointerBuf::parse`], the [`ParseError`] is always wrapped in a +[`Report`] so that the input `String` is not dropped. + +```rust + use jsonptr::{PointerBuf}; + let ptr_str = "foo/bar"; + let err /* Result<&PointerBuf, Report> */ = PointerBuf::parse(ptr_str).unwrap_err(); + assert!(err.original().is_no_leading_slash()); +``` + ## Feature Flags | Flag | Description | Enables | Default | @@ -141,6 +177,7 @@ assert_eq!(data, json!({"secret": { "universe": 34 }})); | `"assign"` | Enables the [`assign`] module and related pointer methods, providing a means to assign a value to a specific location within a document | | ✓ | | `"resolve"` | Enables the [`resolve`] module and related pointer methods, providing a means to resolve a value at a specific location within a document | | ✓ | | `"delete"` | Enables the [`delete`] module and related pointer methods, providing a means to delete a value at a specific location within a document | `"resolve"` | ✓ | +| `"miette"` | Enables integration with [`miette`](https://docs.rs/miette) for error reporting | `"std"` | |
diff --git a/src/diagnostic.rs b/src/diagnostic.rs index 69dcb10..be65970 100644 --- a/src/diagnostic.rs +++ b/src/diagnostic.rs @@ -265,6 +265,7 @@ mod tests { } #[test] + #[cfg(feature = "miette")] fn parse_error() { let invalid = "/foo/bar/invalid~3~encoding/cannot/reach"; let report = Pointer::parse(invalid).diagnose(invalid).unwrap_err(); diff --git a/src/pointer.rs b/src/pointer.rs index be0be7a..d3be5ed 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -2345,6 +2345,7 @@ mod tests { } #[test] + #[cfg(feature = "miette")] fn quick_miette_spike() { let err = PointerBuf::parse("hello-world").unwrap_err(); println!("{:?}", miette::Report::from(err));