Skip to content

Commit

Permalink
refactoring errors & assignment (#41)
Browse files Browse the repository at this point in the history
* refactoring errors & assignment

* fixes assign offset

* fixes creation of object in assignment

* backs out snafu

* quick cleanup

* fixes compiler errors in commented out tests - 4 tests still failing

* refactoring but not done

* assignment wip

* assignment refactor is close but still needs work

* fixes pathing issues in assignment

* still wip

* fixes assign

* removes println

* removes error.rs

* minor docs

* `Pointer::assign` now accepts `&mut A` `where A: Assign`
it now simply calls A::assign

* moves resolve tests

* adds TODOs to lib.rs

* adds tests for delete

* fixes parsing of "/" pointer

* adds more parse tests, some of which are failing

* minor resolve test output change

* adds a couple more test cases for parsing

* fixes tests

* `assigned_to` of `Assignment` to properly records the location of `assigned`

* adds comments

* minor comment update

* deletes Pointer::partial_path

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/assign.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/assign.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* Update src/lib.rs

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>

* possible refactor

* am refactors `Assign`, `Delete`, `Resolve`, `ResolveMut` to include associated type `Value`

* spiking an idea

* adds a potential solution to expansion in assign

* `Strategy` -> `AutoExpand`

* changes the signature of `Assign::assign` to accept new `Expansion` enum

* fixes expect message

* refactors and finishes the first draft of `Expand`

* fixes tests

* fixes doc tests

* style change of Cargo.toml, prep for insta

* removes yaml feature flag. no longer planning to support

* walks back `Expansion` & `Expand`, removes `Assignment`

* removes FailedToExpand from `AssignError`

* walks back taplo style

* spacing

* fixes build

* spacing

* walks back ops feature flags

* fixes doc tests

* CHANGELOG

* adds skeleton for failing test to demonstrate issue #44

---------

Co-authored-by: André Mello <3285133+asmello@users.noreply.github.com>
  • Loading branch information
chanced and asmello authored Jun 24, 2024
1 parent 54401ce commit bd92284
Show file tree
Hide file tree
Showing 13 changed files with 1,441 additions and 1,170 deletions.
42 changes: 20 additions & 22 deletions .taplo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
include = ["**/*.toml", "**/Cargo.toml", "Cargo.toml"]
[formatting]
align_comments = true
align_entries = true
allowed_blank_lines = 1
reorder_arrays = false
reorder_keys = true
trailing_newline = true

# indent_tables = true
# indent_entries = true
align_comments = true
align_entries = true
allowed_blank_lines = 1
indent_entries = false
reorder_arrays = false
reorder_keys = true
trailing_newline = true

[[rule]]
formatting.align_entries = true
formatting.array_auto_expand = false
formatting.reorder_arrays = true
formatting.reorder_keys = true
include = ["Cargo.toml", "**/Cargo.toml"]
keys = [
"dependencies",
"dev-dependencies",
"build-dependencies",
"workspace.dependencies",
"workspace.dev-dependencies",
"workspace.build-dependencies",
]
formatting.align_entries = true
formatting.array_auto_expand = false
formatting.reorder_arrays = true
formatting.reorder_keys = true
include = ["Cargo.toml", "**/Cargo.toml"]
keys = [
"dependencies",
"dev-dependencies",
"build-dependencies",
"workspace.dependencies",
"workspace.dev-dependencies",
"workspace.build-dependencies",
]
19 changes: 14 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

This is a breaking release including [#30](https://github.com/chanced/jsonptr/pull/30) and [#37](https://github.com/chanced/jsonptr/pull/37) by [@asmello](https://github.com/asmello)
This is a breaking release including:

- [#30](https://github.com/chanced/jsonptr/pull/30) and [#37](https://github.com/chanced/jsonptr/pull/37) by [@asmello](https://github.com/asmello)
- [#41](https://github.com/chanced/jsonptr/pull/41) by [@chanced](https://github.com/chanced)

### Added

Expand All @@ -17,26 +20,32 @@ This is a breaking release including [#30](https://github.com/chanced/jsonptr/pu
- [Quickcheck](https://docs.rs/quickcheck/latest/quickcheck/index.html)-based testing
- New methods: `Pointer::split_front`, `Pointer::split_back`, `Pointer::parent`, `Pointer::strip_suffix`
- Implemented `Display` and `Debug` for `ParseError`
- Adds `Pointer::split_at` which utilizes character offsets to split a pointer at a seperator
- Adds specific error types `ParseError`, `ResolveError`, `AssignError`

### Changed

- `Assign`, `Resolve`, `ResolveMut`, `Delete` all now use associated types `Value` and `Error`, allowing for more impls other than JSON
- Debug implementation now preserves type information (e.g. prints `PathBuf("/foo/bar")` instead of `"/foo/bar"`) - `Display` remains the same
- Original `Pointer` type renamed to `PointerBuf`
- Error types now use character `offset` indexing instead of owned copies of `Pointer` and `Token`.
- `Pointer::root` is now `PointerBuf::new`
- `Pointer::new` is now `PointerBuf::from_tokens` (and takes an `IntoIterator` argument - arrays still work)
- `Pointer::union` is now `PointerBuf::intersection`
- `Token` type has been simplified and is now by default a borrowed type (use `Token::to_owned` or `Token::into_owned` to make it owned)
- `Assign::assign` now returns `Result<Option<Assign::Value>, AssignError>`, where `Option<Assign::Value>` is the replaced value

### Fixed

- Fixes [#28](https://github.com/chanced/jsonptr/pull/28): `Pointer::union` is misleadingly named

### Removed

- Removes `impl Deref<Target=&str>`
- Removes optional dependencies of `url`, `fluent-uri` and `uniresid` as well
as the `TryFrom` implementations for `fluent_uri::Uri<String>`, `url::Url`,
`uniresid::AbsoluteUri`, and `uniresid::Uri`
- Removes `Assignment`
- Removes `MaybePointer`
- Removes `Error`
- Removes `impl Deref<Target=&str>` from `Pointer`
- Removes optional dependencies of `url`, `fluent-uri` and `uniresid` as well as the `TryFrom` implementations for their respective types
- Removed `Token::as_key` and `Token::as_str` - use `Token::decoded().as_ref()` to achieve the same effect
- Several redundant or error-prone trait implementations were removed from `Token`

Expand Down
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ repository = "https://github.com/chanced/jsonptr"
version = "0.4.7"

[dependencies]
serde = { version = "1.0", default-features = false, features = ["alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

[features]
default = ["std"]
std = ["serde/std", "serde_json/std"]
serde = { version = "1.0", optional = true, features = ["alloc"] }
serde_json = { version = "1.0", optional = true, features = ["alloc"] }
toml = { version = "0.8", optional = true }

[dev-dependencies]
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"

[features]
default = ["std", "json"]
json = ["dep:serde_json"]
std = ["serde/std", "serde_json?/std"]
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 @@ -62,13 +62,13 @@ 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" }}))
assert_eq!(data, json!({"foo": { "bar": "qux" }}))
```

#### `Assign::asign`

```rust
use jsonptr::{Assign, Pointer};
use jsonptr::{assign::Assign, 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.

```
```
Loading

0 comments on commit bd92284

Please sign in to comment.