Skip to content

Commit

Permalink
fixes doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chanced committed Jun 23, 2024
1 parent 6043341 commit c6a4fea
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 41 deletions.
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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" } } });
Expand All @@ -115,12 +115,9 @@ assert!(data.is_null());

## Feature Flags

| Flag | Enables |
| :------------: | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `"std"` | implements `std::error::Error` for errors |
| `"url"` | `TryFrom<url::Url>` for [`Pointer`](`crate::Pointer`) |
| `"uniresid"` | `TryFrom<uniresid::Uri>` + `TryFrom<uniresid::AbsoluteUri>` for [`Pointer`](`crate::Pointer`) |
| `"fluent-uri"` | `TryFrom<fluent_uri::Uri<&str>>` + `TryFrom<fluent_uri::Uri<String>>` + `TryFrom<fluent_uri::Uri<&mut [u8]>>` for [`Pointer`](`crate::Pointer`) |
| Flag | Enables |
| :-----: | ----------------------------------------- |
| `"std"` | implements `std::error::Error` for errors |

## Contributions / Issues

Expand All @@ -131,7 +128,3 @@ If you find an issue, please open a ticket or a pull request.
## License

MIT or Apache 2.0.

```
```
32 changes: 27 additions & 5 deletions src/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<AssignError>,
expected_assigned: Option<Value>,
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
Expand Down
12 changes: 3 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions src/pointer.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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" } });
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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" } } });
Expand All @@ -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!({});
Expand All @@ -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" } });
Expand All @@ -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!([]);
Expand Down
6 changes: 5 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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},
};
2 changes: 1 addition & 1 deletion src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
Expand Down

0 comments on commit c6a4fea

Please sign in to comment.