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

Improve docs & codecov for 0.5.0 #56

Merged
merged 57 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
4ce6c00
adds assert to example of `Index::for_len_unchecked`
chanced Jul 2, 2024
7b5d56d
fixes `Index::for_len_unchecked` test
chanced Jul 2, 2024
e70813f
ignores `src/arbitrary.rs` from codecov
chanced Jul 2, 2024
4e4bd89
`**/arbitrary.rs` from codecov
chanced Jul 2, 2024
4457b45
trying to get doc tests to count toward codecov by setting language
chanced Jul 2, 2024
2b2a4ec
removes ,
chanced Jul 2, 2024
3605468
trying cargo-tarpaulin
chanced Jul 2, 2024
f7a7816
attempts to fix test.yml
chanced Jul 2, 2024
c522b7a
switches back to cargo-llvm-cov
chanced Jul 2, 2024
695903e
fixes `TryFrom` for `Index` + adds tests
chanced Jul 2, 2024
35e821a
attempting to ignore arbitrary.rs from codecov
chanced Jul 2, 2024
102b431
adds tests for error display
chanced Jul 2, 2024
d3183d7
removes `language: rust` from codecov.yml
chanced Jul 2, 2024
02326bf
adds a few tests for Pointer
chanced Jul 2, 2024
59aee5f
fixes `replace_token` bound
chanced Jul 2, 2024
5a0c191
fixes adds tests for serde `Token`
chanced Jul 2, 2024
a8a1dc1
fixes test
chanced Jul 2, 2024
81e6d30
fixes test.. for real this time
chanced Jul 2, 2024
17f082a
more tests
chanced Jul 2, 2024
68b7079
cargo fmt
chanced Jul 2, 2024
84c5bf3
moves error types of out `lib.rs
chanced Jul 2, 2024
f673264
fixes fmt
chanced Jul 2, 2024
ea44854
increases test cov + adds some trait impls
chanced Jul 3, 2024
53436e5
removes println
chanced Jul 3, 2024
1e0de2a
more coverage
chanced Jul 3, 2024
2bf437a
working on docs
chanced Jul 3, 2024
0d9f4bb
adds ./cargo/config.toml
chanced Jul 4, 2024
55dffef
docs wip
chanced Jul 5, 2024
7ba779a
note on README
chanced Jul 5, 2024
9b7a4d2
adds derive traits to `Component` and `Components`
chanced Jul 5, 2024
ef5a881
restores lib back to being a `pub mod`
chanced Jul 6, 2024
29287d5
moves `has_sent` to `Components`
chanced Jul 7, 2024
bac26fe
restores `index` back to a mod
chanced Jul 7, 2024
e30e663
moves `has_sent` to `Components`
chanced Jul 7, 2024
fc24340
docs
chanced Jul 7, 2024
0b54373
fixes `TryFrom` for `Index` again
chanced Jul 7, 2024
2c13ba5
docs, removes re-exports of index types
chanced Jul 7, 2024
11ae515
fixes docs
chanced Jul 7, 2024
b6452e5
fixes example imports of `jsonptr::index::Index`
chanced Jul 7, 2024
540b6a1
docs + coverage for `Components`
chanced Jul 7, 2024
1321b1c
docs
chanced Jul 7, 2024
9a8d59e
minor cleanup
chanced Jul 7, 2024
e2e158e
fixes minor cleanup
chanced Jul 7, 2024
bf0e55f
minor change to doc
chanced Jul 8, 2024
e6e91af
utilizes rustdoc link hack
chanced Jul 8, 2024
6cb7903
fixes some rustdoc errors
chanced Jul 8, 2024
6512751
adds toml::Value to URLs
chanced Jul 8, 2024
03b0a7f
adds Root to urls
chanced Jul 8, 2024
f7629e4
Update src/resolve.rs
chanced Jul 8, 2024
f16b475
Update src/resolve.rs
chanced Jul 8, 2024
340f314
reduces links + fixes formatting of README.md
chanced Jul 8, 2024
702ca35
spelling, minor fixes
chanced Jul 8, 2024
572430b
fixes typo
chanced Jul 8, 2024
af879e4
reduces noise a bit more in README.md
chanced Jul 8, 2024
5c05dc9
removes unnecessary newlines
chanced Jul 9, 2024
75240e2
removes # from use statements in doc comments for resolve & assign
chanced Jul 9, 2024
b90b5e3
fixes spelling error
chanced Jul 9, 2024
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: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
cov = "llvm-cov --lcov --output-path lcov.info"

[build]
rustdocflags = ["--cfg", "docsrs"]
28 changes: 15 additions & 13 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# ref: https://docs.codecov.com/docs/codecovyml-reference
coverage:
# Hold ourselves to a high bar
range: 85..100
round: down
precision: 1
status:
# ref: https://docs.codecov.com/docs/commit-status
project:
default:
# Avoid false negatives
threshold: 1%
# Hold ourselves to a high bar
range: 85..100
round: down
precision: 1
status:
# ref: https://docs.codecov.com/docs/commit-status
project:
default:
# Avoid false negatives
threshold: 1%

# Test files aren't important for coverage
ignore:
- "tests"
- "tests"
- "arbitrary.rs"
- "src/arbitrary.rs"

# Make comments less noisy
comment:
layout: "files"
require_changes: true
layout: "files"
require_changes: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
# Cargo.lock
lcov.info
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ syn = { version = "1.0.109", optional = true }
assign = []
default = ["std", "serde", "json", "resolve", "assign", "delete"]
delete = ["resolve"]
impl = []
json = ["dep:serde_json", "serde"]
resolve = []
std = ["serde/std", "serde_json?/std"]
Expand Down
201 changes: 115 additions & 86 deletions README.md
chanced marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,131 +1,160 @@
# jsonptr - JSON Pointers for Rust
<div class="rustdoc-hidden">

# jsonptr - JSON Pointers (RFC 6901) for Rust

</div>

[<img alt="github" src="https://img.shields.io/badge/github-chanced/jsonptr-62D1FC?style=for-the-badge&labelColor=777&logo=github" height="21">](https://github.com/chanced/jsonptr)
[<img alt="crates.io" src="https://img.shields.io/crates/v/jsonptr.svg?style=for-the-badge&color=fc8d62&logo=rust" height="21">](https://crates.io/crates/jsonptr)
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-jsonptr-f0f0f0?style=for-the-badge&labelColor=777&logo=docs.rs" height="21">](https://docs.rs/jsonptr)
[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/chanced/jsonptr/test.yml?branch=main&style=for-the-badge" height="21">](https://github.com/chanced/jsonptr/actions?query=branch%3Amain)
[<img alt="code coverage" src="https://img.shields.io/codecov/c/github/chanced/jsonptr?style=for-the-badge&color=CBB88D" height="21">](https://codecov.io/gh/chanced/jsonptr)

Data structures and logic for resolving, assigning, and deleting by JSON Pointers ([RFC
6901](https://datatracker.ietf.org/doc/html/rfc6901)).

## Usage

JSON Pointers can be created either with a slice of strings or directly from a properly encoded string representing a JSON Pointer.
JSON Pointers ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901))
defines a string syntax for identifying a specific location within a JSON, or
similar, document. This crate provides two types, [`Pointer`] and [`PointerBuf`]
(akin to [`str`] and [`String`]), for working with them abstractly.

chanced marked this conversation as resolved.
Show resolved Hide resolved
### Resolve values
A [`Pointer`] is composed of zero or more [`Token`]s, single segments which
represent a field of an object or an [`index`] of an array, and are bounded by
either `'/'` or the end of the string. [`Token`]s are lightly encoded, where
`'~'` is escaped as `"~0"` and `'/'` as `"~1"`.

#### `Pointer::resolve`
[`Token`]s can be iterated over using either [`Tokens`], returned from the
[`tokens`] method of a pointer or [`Components`], returned from the
[`components`] method. The difference being that [`Tokens`] iterates over each
[`Token`] in the [`Pointer`], while a [`Component`] can represent the
[`Root`] document or a single [`Token`](Component::Token).

```rust
use jsonptr::Pointer;
use serde_json::json;
Operations [`resolve`], [`assign`] and [`delete`] are provided as traits with
corresponding methods on [`Pointer`]. Implementations of each trait are provided
for [`serde_json::Value`] and [`toml::Value`]. All
operations are enabled by default but are gated by [feature flags](#feature-flags).

let mut data = json!({ "foo": { "bar": "baz" } });
let ptr = Pointer::from_static("/foo/bar");
let bar = ptr.resolve(&data).unwrap();
assert_eq!(bar, "baz");
```
## Usage

#### `Resolve::resolve`
To parse a pointer from a string, use the [`parse`](Pointer::parse) method.
[`PointerBuf`] can use either the [`parse`](PointerBuf::parse) or
[`from_tokens`](PointerBuf::from_tokens) construct from an iterator of
[`Token`]s:

```rust
use jsonptr::{ Pointer, resolve::Resolve };
use jsonptr::{Pointer, PointerBuf};
use serde_json::json;

let mut data = json!({ "foo": { "bar": "baz" } });
let ptr = Pointer::from_static("/foo/bar");
let bar = data.resolve(&ptr).unwrap();
assert_eq!(bar, "baz");

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

#### `ResolveMut::resolve_mut`
let buf = PointerBuf::from_tokens(["examples", "0", "name"]);
assert_eq!(ptr, &buf);

```rust
use jsonptr::{Pointer, resolve::ResolveMut};
use serde_json::json;
let parent = ptr.parent().unwrap();
assert_eq!(parent, Pointer::parse("/examples/0").unwrap());

let ptr = Pointer::from_static("/foo/bar");
let mut data = json!({ "foo": { "bar": "baz" }});
let mut bar = data.resolve_mut(&ptr).unwrap();
assert_eq!(bar, "baz");
let (front, remaining) = ptr.split_front().unwrap();
assert_eq!(front.decoded(), "examples");
assert_eq!(remaining, Pointer::parse("/0/name").unwrap());
```

### Assign

#### `Pointer::assign`
Values can be resolved by `Pointer`s using either [`Resolve`] or [`ResolveMut`]
traits. See the [`resolve`] mod for more information.

```rust
use jsonptr::Pointer;
use serde_json::json;

let ptr = Pointer::from_static("/foo/bar");
let mut data = json!({});
let _previous = ptr.assign(&mut data, "qux").unwrap();
assert_eq!(data, json!({"foo": { "bar": "qux" }}))
```

#### `Assign::asign`

```rust
use jsonptr::{assign::Assign, Pointer};
use serde_json::json;

let ptr = Pointer::from_static("/foo/bar");
let mut data = json!({});
let _previous = data.assign(&ptr, "qux").unwrap();
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))
let ptr = Pointer::parse("/foo/bar").unwrap();
let data = json!({"foo": { "bar": 34 }});
let bar = ptr.resolve(&data).unwrap();
assert_eq!(bar, &json!(34));
```

### Delete

#### `Pointer::delete`
Values can be assigned using the [`Assign`] trait. See [`assign`] for more
information.

```rust
use jsonptr::Pointer;
use serde_json::json;

let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::from_static("/foo/bar/baz");
assert_eq!(ptr.delete(&mut data), Some("qux".into()));
assert_eq!(data, json!({ "foo": { "bar": {} } }));

// unresolved pointers return None
let mut data = json!({});
assert_eq!(ptr.delete(&mut data), None);
let ptr = Pointer::parse("/secret/universe").unwrap();
let mut data = json!({"secret": { "universe": 42 }});
let replaced = ptr.assign(&mut data, json!(34)).unwrap();
assert_eq!(replaced, Some(json!(42)));
assert_eq!(data, json!({"secret": { "universe": 34 }}));
```

#### `Delete::delete`
Values can be deleted with the [`Delete`] trait. See [`delete`] for more
information.

```rust
use jsonptr::{ Pointer, delete::Delete };
use jsonptr::Pointer;
use serde_json::json;

let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
let ptr = Pointer::from_static("/foo/bar/baz");
assert_eq!(ptr.delete(&mut data), Some("qux".into()));
assert_eq!(data, json!({ "foo": { "bar": {} } }));

// replacing a root pointer replaces data with `Value::Null`
let ptr = Pointer::root();
let deleted = json!({ "foo": { "bar": {} } });
assert_eq!(data.delete(&ptr), Some(deleted));
assert!(data.is_null());
let ptr = Pointer::parse("/secret/universe").unwrap();
let mut data = json!({"secret": { "universe": 42 }});
let replaced = ptr.assign(&mut data, json!(34)).unwrap();
assert_eq!(replaced, Some(json!(42)));
assert_eq!(data, json!({"secret": { "universe": 34 }}));
```

## Feature Flags

| Flag | Enables |
| :-----: | ----------------------------------------- |
| `"std"` | implements `std::error::Error` for errors |

## Contributions / Issues

Contributions and feedback are always welcome and appreciated.

If you find an issue, please open a ticket or a pull request.

| Flag | Description | Enables | Default |
| :---------: | ----------------------------------------------------------------------------------------------------------------------------------------- | --------------- | :-----: |
| `"std"` | Implements `std::error::Error` for error types | | ✓ |
| `"serde"` | Enables [`serde`] support for types | | ✓ |
| `"json"` | Implements ops for [`serde_json::Value`] | `"serde"` | ✓ |
| `"toml"` | Implements ops for [`toml::Value`] | `"std"`, `toml` | |
| `"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"` | ✓ |

<div class="rustdoc-hidden">
## License
chanced marked this conversation as resolved.
Show resolved Hide resolved

MIT or Apache 2.0.
Licensed under either of

- Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

## Contribution

Contributions and feedback are always welcome and appreciated. If you find an
issue, please open a ticket or a pull request.

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.

[LICENSE-APACHE]: LICENSE-APACHE
[LICENSE-MIT]: LICENSE-MIT

</div>

[`Pointer::components`]: (https://docs.rs/jsonptr/latest/jsonptrstruct.Pointer.html#method.components)
[`Pointer::tokens`]: (https://docs.rs/jsonptr/latest/jsonptrstruct.Pointer.html#method.tokens)
[`Pointer`]: https://docs.rs/jsonptr/latest/jsonptr/struct.Pointer.html
[`PointerBuf`]: https://docs.rs/jsonptr/latest/jsonptr/struct.PointerBuf.html
[`Token`]: https://docs.rs/jsonptr/latest/jsonptr/struct.Token.html
[`Tokens`]: https://docs.rs/jsonptr/latest/jsonptr/struct.Tokens.html
[`Components`]: https://docs.rs/jsonptr/latest/jsonptr/struct.Components.html
[`Component`]: https://docs.rs/jsonptr/latest/jsonptr/enum.Component.html
[`Root`]: https://docs.rs/jsonptr/latest/jsonptr/enum.Component.html#variant.Root
[`index`]: https://doc.rust-lang.org/std/primitive.usize.html
[`tokens`]: https://docs.rs/jsonptr/latest/jsonptr/struct.Pointer.html#method.tokens
[`components`]: https://docs.rs/jsonptr/latest/jsonptr/struct.Pointer.html#method.components
[`resolve`]: https://docs.rs/jsonptr/latest/jsonptr/resolve/index.html
[`assign`]: https://docs.rs/jsonptr/latest/jsonptr/assign/index.html
[`delete`]: https://docs.rs/jsonptr/latest/jsonptr/delete/index.html
[`Resolve`]: https://docs.rs/jsonptr/latest/jsonptr/resolve/trait.Resolve.html
[`ResolveMut`]: https://docs.rs/jsonptr/latest/jsonptr/resolve/trait.ResolveMut.html
[`Assign`]: https://docs.rs/jsonptr/latest/jsonptr/assign/trait.Assign.html
[`Delete`]: https://docs.rs/jsonptr/latest/jsonptr/delete/trait.Delete.html
[`serde`]: https://docs.rs/serde/1.0.120/serde/index
[`serde_json::Value`]: https://docs.rs/serde_json/1.0.120/serde_json/enum.Value.html
[`toml::Value`]: https://docs.rs/toml/0.8/toml/enum.Value.html
[`str`]: https://doc.rust-lang.org/std/primitive.str.html
[`String`]: https://doc.rust-lang.org/std/string/struct.String.html
Loading
Loading