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
359
360
361
362
363
364
365
366
367
368
369
370
371
use core::char::ParseCharError;
use core::num::{ParseFloatError, ParseIntError};
use core::str::ParseBoolError;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(feature = "alloc")]
use alloc::string::ToString;
#[cfg(feature = "std")]
use std::error;
#[cfg(feature = "std")]
use std::net::AddrParseError;
/// The error returned when trying to parse a type using [`try_parse`](crate::try_parse) or [`Parse`](crate::Parse).
#[derive(Debug)]
pub enum ParseError {
/// The variant returned when an integer cannot be parsed.
Int(ParseIntError),
/// The variant returned when a bool cannot be parsed.
Bool(ParseBoolError),
/// The variant returned when a char cannot be parsed.
Char(ParseCharError),
/// The variant returned when a float cannot be parsed.
Float(ParseFloatError),
#[cfg(feature = "std")]
/// The variant returned when an ip address cannot be parsed.
/// This variant is only enabled with the `std` feature.
Addr(AddrParseError),
#[cfg(feature = "std")]
/// The variant returned when you want to return an error that is not defined here.
///
/// This variant is only enabled with the `std` feature as the
/// [`Error`](error::Error) trait is a part of std.
Dyn(Box<dyn error::Error + Send + Sync>),
/// The variant returned when [`parse!`](crate::parse) found an unexpected literal.
/// When not using the `alloc` feature, `Literal` is a unit variant.
#[cfg(feature = "alloc")]
Literal {
/// What it expected.
expected: String,
/// What it actually found.
found: String,
},
/// The variant returned when [`parse!`](crate::parse) found an unexpected literal.
/// When not using the `alloc` feature, `Literal` is a unit variant.
#[cfg(not(feature = "alloc"))]
Literal,
/// The variant returned when parsing an array and finding more or less elements than what was expected.
Array {
/// The size of the array it was expecting.
expected: u8,
/// The size of the array it found.
found: u8,
},
/// A variant that can be used when you need to return a simple error.
/// When not using the `alloc` feature, `Other` is a unit variant.
#[cfg(feature = "alloc")]
Other(String),
/// A variant that can be used when you need to return a simple error.
/// When not using the `alloc` feature, `Other` is a unit variant.
#[cfg(not(feature = "alloc"))]
Other,
#[cfg(feature = "alloc")]
/// A variant that wraps a [`ParseError`] to add more context about the error
/// when parsing repetition sequences.
/// This variant is only enabled with the `alloc` feature.
MultiContext {
/// The string part of the repetition sequence that was trying to be parsed
multi_string: String,
/// The string that cause the parsing to fail
failed_string: String,
/// The wrapped error
error: Box<ParseError>,
},
/// A variant that wraps a [`ParseError`] to add more context about the error.
/// This variant is only enabled with the `alloc` feature.
#[cfg(feature = "alloc")]
Context {
/// The full string that was trying to be parsed
full_string: String,
/// The string that cause the parsing to fail
failed_item: String,
/// The wrapped error
error: Box<ParseError>,
},
}
#[cfg(feature = "alloc")]
impl ParseError {
/// Create a new ParseError from a printable error message.
///
/// This function stores the passed message into the `Other` variant.
/// And as such can only be used when using the `alloc` feature.
///
/// This function can be especially useful when trying to implement [`Parse`](crate::Parse).
/// ```
/// # use prse::{Parse, ParseError, ExtParseStr, parse};
/// # #[derive(PartialEq, Debug)]
/// struct Bool(bool);
///
/// impl<'a> Parse<'a> for Bool {
/// fn from_str(s: &'a str) -> Result<Self, ParseError> {
/// match s {
/// "false" | "False" => Ok(Bool(false)),
/// "true" | "True" => Ok(Bool(true)),
/// _ => Err(ParseError::new(format!("expected to find true or false but found {s}.")))
/// }
/// }
/// }
///
/// # fn main() -> Result<(), ParseError> {
/// let b: Bool = parse!("True", "{}");
/// assert_eq!(b, Bool(true));
/// # Ok(())}
/// ```
pub fn new<T: core::fmt::Display>(message: T) -> Self {
Self::Other(message.to_string())
}
}
#[cfg(feature = "std")]
impl error::Error for ParseError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
ParseError::Int(source) => Some(source),
ParseError::Bool(source) => Some(source),
ParseError::Char(source) => Some(source),
ParseError::Float(source) => Some(source),
ParseError::Addr(source) => Some(source),
ParseError::Dyn(source) => Some(&**source),
ParseError::MultiContext { error, .. } => Some(error),
ParseError::Context { error, .. } => Some(error),
ParseError::Literal { .. } | ParseError::Array { .. } | ParseError::Other(_) => None,
}
}
}
impl core::fmt::Display for ParseError {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
ParseError::Int(_) => write!(fmt, "unable to parse as an integer"),
ParseError::Bool(_) => write!(fmt, "unable to parse as a boolean"),
ParseError::Char(_) => write!(fmt, "unable to parse as a character"),
ParseError::Float(_) => write!(fmt, "unable to parse as a float"),
#[cfg(feature = "std")]
ParseError::Addr(_) => write!(fmt, "unable to parse as an address"),
#[cfg(feature = "std")]
ParseError::Dyn(_) => write!(fmt, "unable to parse into type"),
#[cfg(feature = "alloc")]
ParseError::Literal { expected, found } => write!(
fmt,
"invalid literal match (expected to find {expected:?}, found {found:?})"
),
#[cfg(not(feature = "alloc"))]
ParseError::Literal => write!(fmt, "invalid literal match"),
ParseError::Array { expected, found } => write!(
fmt,
"invalid number of items (expected to find {expected:?}, found {found:?})"
),
#[cfg(feature = "alloc")]
ParseError::Other(message) => write!(fmt, "{message}"),
#[cfg(not(feature = "alloc"))]
ParseError::Other => write!(fmt, "unable to parse into type"),
#[cfg(feature = "alloc")]
ParseError::MultiContext {
multi_string,
failed_string: failed_item,
error,
} => {
write!(
fmt,
"unable to parse multi-item \"{failed_item}\" when parsing \"{multi_string}\":\n\t{error}"
)
}
#[cfg(feature = "alloc")]
ParseError::Context {
full_string,
failed_item,
error,
} => {
write!(
fmt,
"unable to parse \"{failed_item}\" when parsing \"{full_string}\":\n\t{error}"
)
}
}
}
}
impl PartialEq for ParseError {
fn eq(&self, other: &Self) -> bool {
use ParseError as E;
match (self, other) {
(E::Int(x), E::Int(y)) => x == y,
(E::Bool(x), E::Bool(y)) => x == y,
(E::Char(x), E::Char(y)) => x == y,
(E::Float(x), E::Float(y)) => x == y,
#[cfg(feature = "std")]
(E::Addr(x), E::Addr(y)) => x == y,
#[cfg(feature = "alloc")]
(
E::Literal {
expected: lx,
found: ly,
},
E::Literal {
expected: rx,
found: ry,
},
) => lx == rx && ly == ry,
#[cfg(not(feature = "alloc"))]
(E::Literal, E::Literal) => true,
(
E::Array {
expected: lx,
found: ly,
},
E::Array {
expected: rx,
found: ry,
},
) => lx == rx && ly == ry,
#[cfg(feature = "alloc")]
(E::Other(x), E::Other(y)) => x == y,
#[cfg(not(feature = "alloc"))]
(E::Other, E::Other) => true,
#[cfg(feature = "alloc")]
(
E::MultiContext {
multi_string: lm,
failed_string: lf,
error: le,
},
E::MultiContext {
multi_string: rm,
failed_string: rf,
error: re,
},
) => lm == rm && lf == rf && le == re,
#[cfg(feature = "alloc")]
(
E::Context {
full_string: ls,
failed_item: lf,
error: le,
},
E::Context {
full_string: rs,
failed_item: rf,
error: re,
},
) => ls == rs && lf == rf && le == re,
_ => false,
}
}
}
macro_rules! impl_from_parse_error {
($Ty: ty, $Id: ident) => {
impl From<$Ty> for ParseError {
fn from(source: $Ty) -> Self {
ParseError::$Id(source)
}
}
};
}
impl_from_parse_error!(ParseIntError, Int);
impl_from_parse_error!(ParseBoolError, Bool);
impl_from_parse_error!(ParseCharError, Char);
impl_from_parse_error!(ParseFloatError, Float);
#[cfg(feature = "std")]
impl_from_parse_error!(AddrParseError, Addr);
#[cfg(feature = "std")]
impl_from_parse_error!(Box<dyn error::Error + Send + Sync>, Dyn);
#[cfg(feature = "alloc")]
impl From<()> for ParseError {
fn from(_: ()) -> Self {
ParseError::Other(String::from("Error: ()"))
}
}
#[cfg(not(feature = "alloc"))]
impl From<()> for ParseError {
fn from(_: ()) -> Self {
ParseError::Other
}
}
#[doc(hidden)]
pub mod __private {
#[cfg(feature = "alloc")]
use super::{Box, ToString};
use crate::{ExtParseStr, Parse, ParseError};
#[doc(hidden)]
/// Not part of public api used to unwrap the result when parsing.
pub fn unwrap_parse<T>(result: Result<T, ParseError>) -> T {
match result {
Ok(x) => x,
Err(e) => panic!("Unable to parse input:\n\t{e}"),
}
}
#[doc(hidden)]
#[cfg(not(feature = "alloc"))]
pub fn try_parse_context<'a, T: Parse<'a>>(
item: &'a str,
_full_string: &'a str,
) -> Result<T, ParseError> {
item.lending_parse()
}
#[doc(hidden)]
#[cfg(not(feature = "alloc"))]
pub fn add_err_multi_context<T>(
result: Result<T, ParseError>,
_input: &str,
_failed_item: &str,
) -> Result<T, ParseError> {
result
}
#[doc(hidden)]
#[cfg(feature = "alloc")]
pub fn try_parse_context<'a, T: Parse<'a>>(
item: &'a str,
full_string: &'a str,
) -> Result<T, ParseError> {
item.lending_parse().map_err(|e| ParseError::Context {
full_string: full_string.to_string(),
failed_item: item.to_string(),
error: Box::new(e),
})
}
#[doc(hidden)]
#[cfg(feature = "alloc")]
pub fn add_err_multi_context<T>(
result: Result<T, ParseError>,
input: &str,
failed_item: &str,
) -> Result<T, ParseError> {
result.map_err(|e| ParseError::MultiContext {
multi_string: input.to_string(),
failed_string: failed_item.to_string(),
error: Box::new(e),
})
}
}
#[cfg(test)]
mod test {
use crate::ParseError;
#[test]
fn check_impl_traits() {
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}
is_send::<ParseError>();
is_sync::<ParseError>();
}
}