-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.rs
358 lines (312 loc) · 15.3 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![warn(clippy::all, clippy::pedantic)]
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(
clippy::module_name_repetitions,
clippy::into_iter_without_iter,
clippy::needless_pass_by_value,
clippy::expect_fun_call
)]
#[cfg_attr(not(feature = "std"), macro_use)]
extern crate alloc;
use core::{fmt, num::ParseIntError};
pub mod prelude;
#[cfg(feature = "assign")]
pub mod assign;
#[cfg(feature = "assign")]
pub use assign::Assign;
#[cfg(feature = "delete")]
pub mod delete;
#[cfg(feature = "delete")]
pub use delete::Delete;
#[cfg(feature = "resolve")]
pub mod resolve;
#[cfg(feature = "resolve")]
pub use resolve::{Resolve, ResolveMut};
mod tokens;
pub use tokens::*;
mod pointer;
pub use pointer::*;
mod token;
pub use token::*;
pub mod index;
pub use index::Index;
#[cfg(test)]
mod arbitrary;
/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ ParseError ║
║ ¯¯¯¯¯¯¯¯¯¯¯¯ ║
╚══════════════════════════════════════════════════════════════════════════════╝
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// Indicates that a `Pointer` was malformed and unable to be parsed.
#[derive(Debug, PartialEq)]
pub enum ParseError {
/// `Pointer` did not start with a backslash (`'/'`).
NoLeadingBackslash,
/// `Pointer` contained invalid encoding (e.g. `~` not followed by `0` or
/// `1`).
InvalidEncoding {
/// Offset of the partial pointer starting with the token that contained
/// the invalid encoding
offset: usize,
/// The source `InvalidEncodingError`
source: InvalidEncodingError,
},
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoLeadingBackslash { .. } => {
write!(
f,
"json pointer is malformed as it does not start with a backslash ('/')"
)
}
Self::InvalidEncoding { source, .. } => write!(f, "{source}"),
}
}
}
impl ParseError {
/// Returns `true` if this error is `NoLeadingBackslash`; otherwise returns
/// `false`.
#[must_use]
pub fn is_no_leading_backslash(&self) -> bool {
matches!(self, Self::NoLeadingBackslash { .. })
}
/// Returns `true` if this error is `InvalidEncoding`; otherwise returns
/// `false`.
#[must_use]
pub fn is_invalid_encoding(&self) -> bool {
matches!(self, Self::InvalidEncoding { .. })
}
/// Offset of the partial pointer starting with the token which caused the
/// error.
/// ```text
/// "/foo/invalid~tilde/invalid"
/// ↑
/// 4
/// ```
/// ```
/// # use jsonptr::PointerBuf;
/// let err = PointerBuf::parse("/foo/invalid~tilde/invalid").unwrap_err();
/// assert_eq!(err.pointer_offset(), 4)
/// ```
#[must_use]
pub fn pointer_offset(&self) -> usize {
match *self {
Self::NoLeadingBackslash { .. } => 0,
Self::InvalidEncoding { offset, .. } => offset,
}
}
/// Offset of the character index from within the first token of
/// [`Self::pointer_offset`])
/// ```text
/// "/foo/invalid~tilde/invalid"
/// ↑
/// 8
/// ```
/// ```
/// # use jsonptr::PointerBuf;
/// let err = PointerBuf::parse("/foo/invalid~tilde/invalid").unwrap_err();
/// assert_eq!(err.source_offset(), 8)
/// ```
#[must_use]
pub fn source_offset(&self) -> usize {
match self {
Self::NoLeadingBackslash { .. } => 0,
Self::InvalidEncoding { source, .. } => source.offset,
}
}
/// Offset of the first invalid encoding from within the pointer.
/// ```text
/// "/foo/invalid~tilde/invalid"
/// ↑
/// 12
/// ```
/// ```
/// # use jsonptr::PointerBuf;
/// let err = PointerBuf::parse("/foo/invalid~tilde/invalid").unwrap_err();
/// assert_eq!(err.pointer_offset(), 4)
/// ```
#[must_use]
pub fn complete_offset(&self) -> usize {
self.source_offset() + self.pointer_offset()
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::InvalidEncoding { source, .. } => Some(source),
Self::NoLeadingBackslash => None,
}
}
}
/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ ParseIndexError ║
║ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ║
╚══════════════════════════════════════════════════════════════════════════════╝
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// Indicates that the `Token` could not be parsed as valid RFC 6901 index.
#[derive(Debug, PartialEq, Eq)]
pub struct ParseIndexError {
/// The source `ParseIntError`
pub source: ParseIntError,
}
impl From<ParseIntError> for ParseIndexError {
fn from(source: ParseIntError) -> Self {
Self { source }
}
}
impl fmt::Display for ParseIndexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to parse token as an integer")
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseIndexError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ InvalidEncodingError ║
║ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ║
╚══════════════════════════════════════════════════════════════════════════════╝
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// A token within a json pointer contained invalid encoding (`~` not followed
/// by `0` or `1`).
///
#[derive(Debug, PartialEq, Eq)]
pub struct InvalidEncodingError {
/// offset of the erroneous `~` from within the `Token`
pub offset: usize,
}
impl InvalidEncodingError {
/// The byte offset of the first invalid `~`.
#[must_use]
pub fn offset(&self) -> usize {
self.offset
}
}
impl fmt::Display for InvalidEncodingError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"json pointer is malformed due to invalid encoding ('~' not followed by '0' or '1')"
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for InvalidEncodingError {}
/// Indicates that the `Token` could not be parsed as valid RFC 6901 index.
#[derive(Debug, PartialEq, Eq)]
pub struct IndexError {
source: ParseIntError,
}
impl core::fmt::Display for IndexError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "failed to parse token as an integer")
}
}
#[cfg(feature = "std")]
impl std::error::Error for IndexError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ OutOfBoundsError ║
║ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ║
╚══════════════════════════════════════════════════════════════════════════════╝
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// Indicates that an `Index` is not within the given bounds.
#[derive(Debug, PartialEq, Eq)]
pub struct OutOfBoundsError {
/// The provided array length.
///
/// If the range is inclusive, the resolved numerical index will be strictly
/// less than this value, otherwise it could be equal to it.
pub length: usize,
/// The resolved numerical index.
///
/// Note that [`Index::Next`] always resolves to the given array length,
/// so it is only valid when the range is inclusive.
pub index: usize,
}
impl fmt::Display for OutOfBoundsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"index {} out of bounds (limit: {})",
self.index, self.length
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for OutOfBoundsError {}
/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ NotFoundError ║
║ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ║
╚══════════════════════════════════════════════════════════════════════════════╝
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// An error that indicates a [`Pointer`]'s path was not found in the data.
#[derive(Debug, PartialEq, Eq)]
pub struct NotFoundError {
/// The starting offset of the [`Token`] within the [`Pointer`] which could not
/// be resolved.
pub offset: usize,
}
impl fmt::Display for NotFoundError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "path starting at offset {} not found", self.offset)
}
}
#[cfg(feature = "std")]
impl std::error::Error for NotFoundError {}
/*
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
╔══════════════════════════════════════════════════════════════════════════════╗
║ ║
║ ReplaceTokenError ║
║ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ ║
╚══════════════════════════════════════════════════════════════════════════════╝
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
/// Returned from `Pointer::replace_token` when the provided index is out of
/// bounds.
#[derive(Debug, PartialEq, Eq)]
pub struct ReplaceTokenError {
/// The index of the token that was out of bounds.
pub index: usize,
/// The number of tokens in the `Pointer`.
pub count: usize,
}
impl fmt::Display for ReplaceTokenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "index {} is out of bounds ({})", self.index, self.count)
}
}
#[cfg(feature = "std")]
impl std::error::Error for ReplaceTokenError {}