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
use alloc::{
format,
string::{FromUtf8Error, String, ToString},
vec::Vec,
};
use crate::{Pointer, Token};
use core::{
fmt::{Debug, Display, Formatter},
num::ParseIntError,
};
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Error {
Index(IndexError),
Unresolvable(UnresolvableError),
NotFound(NotFoundError),
MalformedPointer(MalformedPointerError),
}
impl Error {
pub fn is_index(&self) -> bool {
matches!(self, Error::Index(_))
}
pub fn is_unresolvable(&self) -> bool {
matches!(self, Error::Unresolvable(_))
}
pub fn is_not_found(&self) -> bool {
matches!(self, Error::NotFound(_))
}
pub fn is_malformed_pointer(&self) -> bool {
matches!(self, Error::MalformedPointer(_))
}
}
impl From<MalformedPointerError> for Error {
fn from(err: MalformedPointerError) -> Self {
Error::MalformedPointer(err)
}
}
impl From<IndexError> for Error {
fn from(err: IndexError) -> Self {
Error::Index(err)
}
}
impl From<NotFoundError> for Error {
fn from(err: NotFoundError) -> Self {
Error::NotFound(err)
}
}
impl From<OutOfBoundsError> for Error {
fn from(err: OutOfBoundsError) -> Self {
Error::Index(IndexError::from(err))
}
}
impl From<UnresolvableError> for Error {
fn from(err: UnresolvableError) -> Self {
Error::Unresolvable(err)
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
Error::Index(err) => Display::fmt(err, f),
Error::Unresolvable(err) => Display::fmt(err, f),
Error::NotFound(err) => Display::fmt(err, f),
Error::MalformedPointer(err) => Display::fmt(err, f),
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct UnresolvableError {
pub pointer: Pointer,
pub leaf: Option<Token>,
}
impl UnresolvableError {
pub fn new(pointer: Pointer) -> Self {
let leaf = if pointer.count() >= 2 {
Some(pointer.get(pointer.count() - 2).unwrap())
} else {
None
};
Self { pointer, leaf }
}
}
impl Display for UnresolvableError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(
f,
"can not resolve \"{}\" due to {} being a scalar value",
self.pointer,
self.leaf
.as_deref()
.map_or_else(|| "the root value".to_string(), |l| format!("\"{l}\""))
)
}
}
#[derive(PartialEq, Eq, Clone)]
pub enum IndexError {
Parse(ParseError),
OutOfBounds(OutOfBoundsError),
}
impl Display for IndexError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
IndexError::Parse(err) => Display::fmt(&err, f),
IndexError::OutOfBounds(err) => Display::fmt(&err, f),
}
}
}
impl Debug for IndexError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
Display::fmt(self, f)
}
}
#[cfg(feature = "std")]
impl std::error::Error for IndexError {}
impl From<OutOfBoundsError> for IndexError {
fn from(err: OutOfBoundsError) -> Self {
IndexError::OutOfBounds(err)
}
}
#[derive(PartialEq, Eq, Clone)]
pub struct ParseError {
pub source: ParseIntError,
pub token: Token,
}
impl Display for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.source)
}
}
impl Debug for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ParseError")
.field("source", &self.source)
.field("token", &self.token)
.finish()
}
}
#[cfg(feature = "std")]
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.source)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct OutOfBoundsError {
pub len: usize,
pub index: usize,
pub token: Token,
}
#[cfg(feature = "std")]
impl std::error::Erorr for OutOfBoundsError {}
impl Display for OutOfBoundsError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "index {} out of bounds", self.index)
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct NotUtf8Error {
pub source: FromUtf8Error,
pub path: Vec<u8>,
}
impl core::fmt::Display for NotUtf8Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "not utf8: {}", self.source)
}
}
#[cfg(feature = "std")]
impl std::error::Error for NotUtf8Error {}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MalformedPointerError {
NoLeadingSlash(String),
InvalidEncoding(String),
NotUtf8(NotUtf8Error),
}
impl From<NotUtf8Error> for MalformedPointerError {
fn from(err: NotUtf8Error) -> Self {
MalformedPointerError::NotUtf8(err)
}
}
impl From<FromUtf8Error> for MalformedPointerError {
fn from(err: FromUtf8Error) -> Self {
MalformedPointerError::NotUtf8(NotUtf8Error {
source: err,
path: Vec::new(),
})
}
}
impl Display for MalformedPointerError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self {
MalformedPointerError::NoLeadingSlash(s) => {
write!(
f,
"json pointer \"{s}\" is malformed due to missing starting slash",
)
}
MalformedPointerError::InvalidEncoding(s) => {
write!(f, "json pointer \"{s}\" is improperly encoded")
}
MalformedPointerError::NotUtf8(err) => {
write!(f, "json pointer is not UTF-8: {}", err.source)
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for MalformedPointerError {}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct NotFoundError {
pub pointer: Pointer,
}
impl NotFoundError {
pub fn new(pointer: Pointer) -> Self {
NotFoundError { pointer }
}
}
impl Display for NotFoundError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(
f,
"the resource at json pointer \"{}\" was not found",
self.pointer
)
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ReplaceTokenError {
pub index: usize,
pub count: usize,
pub pointer: Pointer,
}
#[cfg(feature = "std")]
impl std::error::Error for ReplaceTokenError {}
impl Display for ReplaceTokenError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(
f,
"index {} is out of bounds ({}) for the pointer {}",
self.index, self.count, self.pointer
)
}
}