From c6a4fea02dc1b329044eb020f4745fbb449f2668 Mon Sep 17 00:00:00 2001 From: Chance Date: Sun, 23 Jun 2024 18:08:01 -0400 Subject: [PATCH] fixes doc tests --- README.md | 27 ++++++++++----------------- src/assign.rs | 32 +++++++++++++++++++++++++++----- src/lib.rs | 12 +++--------- src/pointer.rs | 18 ++++++++++-------- src/prelude.rs | 6 +++++- src/resolve.rs | 2 +- 6 files changed, 56 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 6098162..1abdedc 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ JSON Pointers can be created either with a slice of strings or directly from a p use jsonptr::Pointer; use serde_json::json; -let mut data = json!({"foo": { "bar": "baz" }}); +let mut data = json!({ "foo": { "bar": "baz" } }); let ptr = Pointer::from_static("/foo/bar"); let bar = ptr.resolve(&data).unwrap(); assert_eq!(bar, "baz"); @@ -29,10 +29,10 @@ assert_eq!(bar, "baz"); #### `Resolve::resolve` ```rust -use jsonptr::{Pointer, Resolve}; +use jsonptr::{ Pointer, resolve::Resolve }; use serde_json::json; -let mut data = json!({ "foo": { "bar": "baz" }}); +let mut data = json!({ "foo": { "bar": "baz" } }); let ptr = Pointer::from_static("/foo/bar"); let bar = data.resolve(&ptr).unwrap(); assert_eq!(bar, "baz"); @@ -42,7 +42,7 @@ assert_eq!(bar, "baz"); #### `ResolveMut::resolve_mut` ```rust -use jsonptr::{Pointer, ResolveMut}; +use jsonptr::{Pointer, resolve::ResolveMut}; use serde_json::json; let ptr = Pointer::from_static("/foo/bar"); @@ -56,7 +56,7 @@ assert_eq!(bar, "baz"); #### `Pointer::assign` ```rust -use jsonptr::{Pointer, Expansion}; +use jsonptr::{Pointer, assign::Expansion}; use serde_json::json; let ptr = Pointer::from_static("/foo/bar"); @@ -68,7 +68,7 @@ assert_eq!(data, json!({ "foo": { "bar": "qux" }})) #### `Assign::asign` ```rust -use jsonptr::{Assign, Pointer, Expansion}; +use jsonptr::{ assign::{ Assign, Expansion }, Pointer }; use serde_json::json; let ptr = Pointer::from_static("/foo/bar"); @@ -98,7 +98,7 @@ assert_eq!(ptr.delete(&mut data), None); #### `Delete::delete` ```rust -use jsonptr::{Pointer, Delete}; +use jsonptr::{ Pointer, delete::Delete }; use serde_json::json; let mut data = json!({ "foo": { "bar": { "baz": "qux" } } }); @@ -115,12 +115,9 @@ assert!(data.is_null()); ## Feature Flags -| Flag | Enables | -| :------------: | ----------------------------------------------------------------------------------------------------------------------------------------------- | -| `"std"` | implements `std::error::Error` for errors | -| `"url"` | `TryFrom` for [`Pointer`](`crate::Pointer`) | -| `"uniresid"` | `TryFrom` + `TryFrom` for [`Pointer`](`crate::Pointer`) | -| `"fluent-uri"` | `TryFrom>` + `TryFrom>` + `TryFrom>` for [`Pointer`](`crate::Pointer`) | +| Flag | Enables | +| :-----: | ----------------------------------------- | +| `"std"` | implements `std::error::Error` for errors | ## Contributions / Issues @@ -131,7 +128,3 @@ If you find an issue, please open a ticket or a pull request. ## License MIT or Apache 2.0. - -``` - -``` diff --git a/src/assign.rs b/src/assign.rs index 729b503..147597a 100644 --- a/src/assign.rs +++ b/src/assign.rs @@ -84,9 +84,9 @@ pub enum Expansion<'e, V> { /// If a scalar or non-existent path is encountered before the [`Pointer`] /// is exhausted, the path will automatically be expanded into /// [`Assign::Value`] based upon a best-guess effort on the meaning of each - /// [`Token`]: - /// - If the [`Token`] is equal to `"0"` or `"-"`, the token will be - /// considered an index of an array. + /// [`Token`](crate::Token): + /// - If the [`Token`](crate::Token) is equal to `"0"` or `"-"`, the token + /// will be considered an index of an array. /// - All tokens not equal to `"0"` or `"-"` will be considered keys of an /// object. /// @@ -151,7 +151,7 @@ pub struct Assignment<'v, V> { /// ## Example /// ```rust /// # use serde_json::json; - /// # use jsonptr::{Pointer, Assign, Expansion}; + /// # use jsonptr::{ Pointer, assign::{ Assign, Expansion }}; /// let mut data = json!({ "foo": ["zero"] }); /// let mut ptr = Pointer::from_static("/foo/-"); /// let assignment = data.assign(&mut ptr, "one", Expansion::BestGuess).unwrap(); @@ -535,7 +535,9 @@ mod json { use serde_json::{json, Value}; - use crate::{assign::Expansion, AssignError, ParseIndexError, Pointer, PointerBuf}; + use crate::{ParseIndexError, Pointer, PointerBuf}; + + use super::*; #[test] fn test_assign_error_partial_eq() { @@ -618,6 +620,26 @@ mod json { assert_eq!(assignment.assigned, &val2); assert_eq!(assignment.assigned_to, "/foo"); + struct Test<'e> { + data: Value, + ptr: &'static str, + expansion: Expansion<'e, Value>, + expected_data: Value, + expected_error: Option, + expected_assigned: Option, + expected_assigned_to: &'static str, + } + + let _tests = [Test { + data: json!({}), + ptr: "/foo", + expansion: Expansion::BestGuess, + expected_data: json!({ "foo": "bar" }), + expected_error: None, + expected_assigned: Some(json!("bar")), + expected_assigned_to: "/foo", + }]; + // let tests = [ // ( // // `pointer` of the assignment diff --git a/src/lib.rs b/src/lib.rs index 2b193a8..fea9a24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,17 +6,11 @@ extern crate alloc; use core::{fmt, num::ParseIntError}; -// TODO(chanced): make these pub? -mod assign; -pub use assign::{Assign, AssignError, Assignment, Expand, Expansion}; -mod delete; -pub use delete::Delete; -mod resolve; -pub use resolve::{Resolve, ResolveError, ResolveMut}; - +pub mod assign; +pub mod delete; pub mod prelude; +pub mod resolve; -// TODO(chanced): move these into lib.rs mod tokens; pub use tokens::*; mod pointer; diff --git a/src/pointer.rs b/src/pointer.rs index 55cf5e5..7bf5381 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -1,6 +1,8 @@ use crate::{ - Assign, Assignment, Delete, Expansion, InvalidEncodingError, ParseError, ReplaceTokenError, - Resolve, ResolveMut, Token, Tokens, + assign::{Assign, Assignment, Expansion}, + delete::Delete, + resolve::{Resolve, ResolveMut}, + InvalidEncodingError, ParseError, ReplaceTokenError, Token, Tokens, }; use alloc::{ borrow::ToOwned, @@ -88,7 +90,7 @@ const fn is_valid_ptr(value: &str) -> bool { /// /// ## Example /// ```rust -/// use jsonptr::{Pointer, Resolve}; +/// use jsonptr::{Pointer, resolve::Resolve}; /// use serde_json::{json, Value}; /// /// let data = json!({ "foo": { "bar": "baz" } }); @@ -134,7 +136,7 @@ impl Pointer { /// # Examples /// /// ``` - /// use jsonptr::{Pointer, Resolve}; + /// use jsonptr::{Pointer, resolve::Resolve}; /// use serde_json::{json, Value}; /// /// const POINTER: &Pointer = Pointer::from_static("/foo/bar"); @@ -345,7 +347,7 @@ impl Pointer { /// ## Examples /// ### Deleting a resolved pointer: /// ```rust - /// use jsonptr::{Pointer, Delete}; + /// use jsonptr::{Pointer, delete::Delete}; /// use serde_json::json; /// /// let mut data = json!({ "foo": { "bar": { "baz": "qux" } } }); @@ -355,7 +357,7 @@ impl Pointer { /// ``` /// ### Deleting a non-existent Pointer returns `None`: /// ```rust - /// use jsonptr::Pointer; + /// use jsonptr::{ Pointer, delete::Delete }; /// use serde_json::json; /// /// let mut data = json!({}); @@ -365,7 +367,7 @@ impl Pointer { /// ``` /// ### Deleting a root pointer replaces the value with `Value::Null`: /// ```rust - /// use jsonptr::{Pointer, Delete}; + /// use jsonptr::{Pointer, delete::Delete}; /// use serde_json::json; /// /// let mut data = json!({ "foo": { "bar": "baz" } }); @@ -385,7 +387,7 @@ impl Pointer { /// /// ## Example /// ```rust - /// use jsonptr::{ Pointer, Expansion }; + /// use jsonptr::{ Pointer, assign::Expansion }; /// use jsonptr::prelude::*; // <-- for Assign trait /// use serde_json::{json, Value}; /// let mut data = json!([]); diff --git a/src/prelude.rs b/src/prelude.rs index 72b377c..5d8c18d 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,2 +1,6 @@ //! Exposes the traits `Assign`, `Delete`, `Resolve`, `ResolveMut` -pub use crate::{Assign, Delete, Resolve, ResolveMut}; +pub use crate::{ + assign::Assign, + delete::Delete, + resolve::{Resolve, ResolveMut}, +}; diff --git a/src/resolve.rs b/src/resolve.rs index 59db8c1..2620da4 100644 --- a/src/resolve.rs +++ b/src/resolve.rs @@ -25,7 +25,7 @@ pub trait Resolve { /// Error associated with `Resolve` type Error; - /// Resolve a reference to a `serde_json::Value` based on the path in a + /// Resolve a reference to `Self::Value` based on the path in a /// [Pointer]. fn resolve(&self, ptr: &Pointer) -> Result<&Self::Value, Self::Error>; }