@@ -35,6 +35,8 @@ flags](#feature-flags).
35
35
36
36
## Usage
37
37
38
+ ### Parsing and General Usage
39
+
38
40
To parse a [ ` Pointer ` ] from a string, use either [ ` Pointer::parse ` ] , for
39
41
potentially fallible parsing, or the ` const fn ` ` from_static ` to produce a
40
42
` &'static Pointer ` from a string that is known to be valid.
@@ -46,6 +48,8 @@ let ptr = Pointer::parse("/examples/0/name").unwrap();
46
48
let static_ptr = Pointer :: from_static (" /examples/0/name" );
47
49
assert_eq! (ptr , static_ptr );
48
50
51
+ assert_eq! (ptr . get (1 .. ). unwrap (), Pointer :: parse (" /0/name" ). unwrap ());
52
+
49
53
let parent = ptr . parent (). unwrap ();
50
54
assert_eq! (parent , Pointer :: parse (" /examples/0" ). unwrap ());
51
55
@@ -70,6 +74,8 @@ buf.push_back("/");
70
74
assert_eq! (buf . as_str (), " /~0/pointer/examples/0/name/~1" );
71
75
```
72
76
77
+ ### Token Iteration
78
+
73
79
Iterating over the tokens or components of a pointer:
74
80
75
81
``` rust
@@ -88,6 +94,8 @@ assert_eq!(components.next(), Some(Component::Token(Token::new("to"))));
88
94
assert_eq! (components . next (), Some (Component :: Token (Token :: new (" value" ))));
89
95
```
90
96
97
+ ### Resolving Values
98
+
91
99
To get a value at the location of a pointer, use either the [ ` Resolve ` ] and
92
100
[ ` ResolveMut ` ] traits or [ ` Pointer::resolve ` ] and [ ` Pointer::resolve_mut ` ]
93
101
methods. See the [ ` resolve ` ] mod for more information.
@@ -102,6 +110,8 @@ let bar = ptr.resolve(&data).unwrap();
102
110
assert_eq! (bar , & json! (34 ));
103
111
```
104
112
113
+ ### Assigning Values
114
+
105
115
Values can be set, with path expansion, using the either the [ ` Assign ` ] trait or
106
116
[ ` Pointer::assign ` ] . See [ ` assign ` ] for more information.
107
117
@@ -116,6 +126,8 @@ assert_eq!(replaced, Some(json!(42)));
116
126
assert_eq! (data , json! ({" secret" : { " universe" : 34 }}));
117
127
```
118
128
129
+ ### Deleting Values
130
+
119
131
Values can be removed with the either the [ ` Delete ` ] trait or
120
132
[ ` Pointer::delete ` ] . See [ ` delete ` ] for more information.
121
133
@@ -130,6 +142,30 @@ assert_eq!(replaced, Some(json!(42)));
130
142
assert_eq! (data , json! ({" secret" : { " universe" : 34 }}));
131
143
```
132
144
145
+ ### Error Reporting
146
+
147
+ Any error produced by function calls into methods of traits or types of this
148
+ crate can be converted into a [ ` Report ` ] which contains the original error
149
+ and the [ ` String ` ] which failed to parse or the [ ` PointerBuf ` ] which failed to
150
+ resolve or assign.
151
+
152
+ ``` rust
153
+ use jsonptr :: {Pointer , Diagnose };
154
+ let ptr_str = " foo/bar" ;
155
+ let err /* Result<&Pointer, Report<ParseError>> */ = Pointer :: parse (ptr_str ). diagnose (ptr_str ). unwrap_err ();
156
+ assert! (err . original (). is_no_leading_slash ());
157
+ ```
158
+
159
+ In the case of [ ` PointerBuf::parse ` ] , the [ ` ParseError ` ] is always wrapped in a
160
+ [ ` Report ` ] so that the input ` String ` is not dropped.
161
+
162
+ ``` rust
163
+ use jsonptr :: {PointerBuf };
164
+ let ptr_str = " foo/bar" ;
165
+ let err /* Result<&PointerBuf, Report<ParseError>> */ = PointerBuf :: parse (ptr_str ). unwrap_err ();
166
+ assert! (err . original (). is_no_leading_slash ());
167
+ ```
168
+
133
169
## Feature Flags
134
170
135
171
| Flag | Description | Enables | Default |
@@ -141,6 +177,7 @@ assert_eq!(data, json!({"secret": { "universe": 34 }}));
141
177
| ` "assign" ` | Enables the [ ` assign ` ] module and related pointer methods, providing a means to assign a value to a specific location within a document | | ✓ |
142
178
| ` "resolve" ` | Enables the [ ` resolve ` ] module and related pointer methods, providing a means to resolve a value at a specific location within a document | | ✓ |
143
179
| ` "delete" ` | Enables the [ ` delete ` ] module and related pointer methods, providing a means to delete a value at a specific location within a document | ` "resolve" ` | ✓ |
180
+ | ` "miette" ` | Enables integration with [ ` miette ` ] ( https://docs.rs/miette ) for error reporting | ` "std" ` | |
144
181
145
182
<div class =" rustdoc-hidden " >
146
183
0 commit comments