Skip to content

Commit c1961df

Browse files
authored
Merge pull request #13 from chanced/remove-error-from-pointer
Remove `MalformedPointer` from `Pointer`
2 parents 45fbe20 + cc12ecd commit c1961df

File tree

7 files changed

+268
-116
lines changed

7 files changed

+268
-116
lines changed

CHANGELOG.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [0.4.0] 2023-05-31
9+
10+
### Added
11+
12+
- Adds `CHANGELOG.md` which will be better upkept moving forward.
13+
- Adds `MaybePointer` to assist with deserialization which should not fail fast.
14+
15+
### Changed
16+
17+
- `Pointer::new` now accepts a generic list, so `&["example"]` can be replaced by `["example"]`. For untyped, empty slices (i.e. `Pointer::new(&[])`), use `Pointer::default()`.
18+
- `std` is now enabled by default.
19+
20+
### Removed
21+
22+
- Removes optional `MalformedPointerError` from `Pointer`.
23+
24+
## [0.3.6] 2023-05-23
25+
26+
### Changed
27+
28+
- Adds quotes around `Pointer` debug output (#11)
29+
30+
### Fixed
31+
32+
- Adds missing `impl std::error::Error` for `Error`, `NotFoundError`, `MalformedError`
33+
- Fixes build for `std` feature flag
34+
35+
## [0.3.4] 2023-05-11
36+
37+
### Added
38+
39+
- Adds feature flag `fluent-uri` for `From<fluent_uri::Uri<_>` impl (#3)
40+
41+
## [0.2.0] 2023-02-24
42+
43+
### Changed
44+
45+
- `std` is now optional
46+
- Adds feature flags `"uniresid"`, `"url"` to enable implementing `From<Uri>`, `From<Url>` (respectively).
47+
48+
### Removed
49+
50+
- Removes `Cargo.lock`
51+
- Makes `uniresid` and `uri` optional
52+
53+
## [0.1.0] - 2022-06-12
54+
55+
### Fixed
56+
57+
- Fixes root pointer representation `""` rather than the erroneous `"/"`
58+
- Fixes an issue where encoded tokens were not being resolved properly

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ keywords = ["json-pointer", "rfc-6901", "6901"]
88
license = "MIT OR Apache-2.0"
99
name = "jsonptr"
1010
repository = "https://github.com/chanced/jsonptr"
11-
version = "0.3.6"
11+
version = "0.4.0"
1212

1313
[dependencies]
1414
fluent-uri = { version = "0.1.4", optional = true, default-features = false }
@@ -18,5 +18,5 @@ uniresid = { version = "0.1.4", optional = true }
1818
url = { version = "2", optional = true }
1919

2020
[features]
21-
default = []
21+
default = ["std"]
2222
std = ["serde/std", "serde_json/std", "fluent-uri?/std"]

README.md

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,71 +12,105 @@ Data structures and logic for resolving, assigning, and deleting by JSON Pointer
1212

1313
JSON Pointers can be created either with a slice of strings or directly from a properly encoded string representing a JSON Pointer.
1414

15-
### Resolve
15+
### Resolve values
16+
17+
#### `Pointer::resolve`
18+
19+
```rust
20+
use jsonptr::Pointer;
21+
use serde_json::json;
22+
23+
let mut data = json!({"foo": { "bar": "baz" }});
24+
let ptr = Pointer::new(["foo", "bar"]);
25+
let bar = ptr.resolve(&data).unwrap();
26+
assert_eq!(bar, "baz");
27+
```
28+
29+
#### `Resolve::resolve`
1630

1731
```rust
18-
use jsonptr::{Pointer, Resolve, ResolveMut};
32+
use jsonptr::{Pointer, Resolve};
1933
use serde_json::json;
2034

21-
fn main() {
22-
let mut data = json!({
23-
"foo": {
24-
"bar": "baz"
25-
}
26-
});
35+
let mut data = json!({ "foo": { "bar": "baz" }});
36+
let ptr = Pointer::new(["foo", "bar"]);
37+
let bar = data.resolve(&ptr).unwrap();
38+
assert_eq!(bar, "baz");
2739

28-
let ptr = Pointer::new(&["foo", "bar"]);
29-
let bar = ptr.resolve(&data).unwrap();
30-
assert_eq!(bar, "baz");
40+
```
3141

32-
let bar = data.resolve(&ptr).unwrap();
33-
assert_eq!(bar, "baz");
42+
#### `ResolveMut::resolve_mut`
3443

35-
let ptr = Pointer::try_from("/foo/bar").unwrap();
36-
let mut bar = data.resolve_mut(&ptr).unwrap();
37-
assert_eq!(bar, "baz");
38-
}
44+
```rust
45+
use jsonptr::{Pointer, ResolveMut};
46+
use serde_json::json;
3947

48+
let ptr = Pointer::try_from("/foo/bar").unwrap();
49+
let mut data = json!({ "foo": { "bar": "baz" }});
50+
let mut bar = data.resolve_mut(&ptr).unwrap();
51+
assert_eq!(bar, "baz");
4052
```
4153

4254
### Assign
4355

56+
#### `Pointer::assign`
57+
58+
```rust
59+
use jsonptr::Pointer;
60+
use serde_json::json;
61+
62+
let ptr = Pointer::try_from("/foo/bar").unwrap();
63+
let mut data = json!({});
64+
let _previous = ptr.assign(&mut data, "qux").unwrap();
65+
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))
66+
```
67+
68+
#### `Assign::asign`
69+
4470
```rust
45-
use jsonptr::{Pointer, Assign};
71+
use jsonptr::{Assign, Pointer};
4672
use serde_json::json;
4773

48-
fn main() {
49-
let ptr = Pointer::try_from("/foo/bar").unwrap();
50-
let mut data = json!({});
51-
let assignment = data.assign(&ptr, "qux");
52-
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))
53-
}
74+
let ptr = Pointer::try_from("/foo/bar").unwrap();
75+
let mut data = json!({});
76+
let _previous = data.assign(&ptr, "qux").unwrap();
77+
assert_eq!(data, json!({ "foo": { "bar": "qux" }}))
5478
```
5579

5680
### Delete
5781

82+
#### `Pointer::delete`
83+
84+
```rust
85+
use jsonptr::Pointer;
86+
use serde_json::json;
87+
88+
let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
89+
let ptr = Pointer::new(&["foo", "bar", "baz"]);
90+
assert_eq!(ptr.delete(&mut data), Some("qux".into()));
91+
assert_eq!(data, json!({ "foo": { "bar": {} } }));
92+
93+
// unresolved pointers return None
94+
let mut data = json!({});
95+
assert_eq!(ptr.delete(&mut data), None);
96+
```
97+
98+
#### `Delete::delete`
99+
58100
```rust
59-
use jsonptr::{Pointer, Delete};
60-
use serde_json::json;
61-
fn main() {
62-
let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
63-
let ptr = Pointer::new(&["foo", "bar", "baz"]);
64-
assert_eq!(data.delete(&ptr), Ok(Some("qux".into())));
65-
assert_eq!(data, json!({ "foo": { "bar": {} } }));
66-
67-
// unresolved pointers return Ok(None)
68-
let mut data = json!({});
69-
let ptr = Pointer::new(&["foo", "bar", "baz"]);
70-
assert_eq!(ptr.delete(&mut data), Ok(None));
71-
assert_eq!(data, json!({}));
72-
73-
// replacing a root pointer replaces data with `Value::Null`
74-
let mut data = json!({ "foo": { "bar": "baz" } });
75-
let ptr = Pointer::default();
76-
let expected = json!({ "foo": { "bar": "baz" } });
77-
assert_eq!(data.delete(&ptr), Ok(Some(expected)));
78-
assert!(data.is_null());
79-
}
101+
use jsonptr::{Pointer, Delete};
102+
use serde_json::json;
103+
104+
let mut data = json!({ "foo": { "bar": { "baz": "qux" } } });
105+
let ptr = Pointer::new(["foo", "bar", "baz"]);
106+
assert_eq!(ptr.delete(&mut data), Some("qux".into()));
107+
assert_eq!(data, json!({ "foo": { "bar": {} } }));
108+
109+
// replacing a root pointer replaces data with `Value::Null`
110+
let ptr = Pointer::default();
111+
let deleted = json!({ "foo": { "bar": {} } });
112+
assert_eq!(data.delete(&ptr), Some(deleted));
113+
assert!(data.is_null());
80114
```
81115

82116
## Feature Flags
@@ -97,3 +131,7 @@ If you find an issue, please open a ticket or a pull request.
97131
## License
98132

99133
MIT or Apache 2.0.
134+
135+
```
136+
137+
```

src/delete.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ pub trait Delete {
77
/// Error associated with `Delete`
88
type Error;
99
/// Attempts to internally delete a value based upon a [Pointer].
10-
fn delete(&mut self, ptr: &Pointer) -> Result<Option<Value>, Self::Error>;
10+
fn delete(&mut self, ptr: &Pointer) -> Option<Value>;
1111
}
1212

1313
impl Delete for Value {
1414
type Error = MalformedPointerError;
15-
fn delete(&mut self, ptr: &Pointer) -> Result<Option<Value>, Self::Error> {
15+
fn delete(&mut self, ptr: &Pointer) -> Option<Value> {
1616
ptr.delete(self)
1717
}
1818
}

src/error.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub enum Error {
2424
Unresolvable(UnresolvableError),
2525
/// Indicates that a Pointer was not found in the data.
2626
NotFound(NotFoundError),
27-
/// Indicates that a Pointer was malformed.
28-
MalformedPointer(MalformedPointerError),
27+
// /// Indicates that a Pointer was malformed.
28+
// MalformedPointer(MalformedPointerError),
2929
}
3030

3131
impl Error {
@@ -41,16 +41,16 @@ impl Error {
4141
pub fn is_not_found(&self) -> bool {
4242
matches!(self, Error::NotFound(_))
4343
}
44-
/// Returns `true` if the error is `Error::MalformedPointerError`.
45-
pub fn is_malformed_pointer(&self) -> bool {
46-
matches!(self, Error::MalformedPointer(_))
47-
}
48-
}
49-
impl From<MalformedPointerError> for Error {
50-
fn from(err: MalformedPointerError) -> Self {
51-
Error::MalformedPointer(err)
52-
}
53-
}
44+
// /// Returns `true` if the error is `Error::MalformedPointerError`.
45+
// pub fn is_malformed_pointer(&self) -> bool {
46+
// matches!(self, Error::MalformedPointer(_))
47+
// }
48+
}
49+
// impl From<MalformedPointerError> for Error {
50+
// fn from(err: MalformedPointerError) -> Self {
51+
// Error::MalformedPointer(err)
52+
// }
53+
// }
5454
impl From<IndexError> for Error {
5555
fn from(err: IndexError) -> Self {
5656
Error::Index(err)
@@ -80,7 +80,7 @@ impl Display for Error {
8080
Error::Index(err) => Display::fmt(err, f),
8181
Error::Unresolvable(err) => Display::fmt(err, f),
8282
Error::NotFound(err) => Display::fmt(err, f),
83-
Error::MalformedPointer(err) => Display::fmt(err, f),
83+
// Error::MalformedPointer(err) => Display::fmt(err, f),
8484
}
8585
}
8686
}

0 commit comments

Comments
 (0)