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
use core::{fmt, mem};
use static_assertions::const_assert;
#[cfg(feature = "std")]
use std::error;
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Error {
Overflow(usize),
Underflow(usize),
InvalidDigit(usize),
Empty(usize),
EmptyMantissa(usize),
EmptyExponent(usize),
EmptyInteger(usize),
EmptyFraction(usize),
InvalidPositiveMantissaSign(usize),
MissingMantissaSign(usize),
InvalidExponent(usize),
InvalidPositiveExponentSign(usize),
MissingExponentSign(usize),
ExponentWithoutFraction(usize),
InvalidLeadingZeros(usize),
MissingExponent(usize),
MissingSign(usize),
InvalidPositiveSign(usize),
InvalidNegativeSign(usize),
InvalidMantissaRadix,
InvalidExponentBase,
InvalidExponentRadix,
InvalidDigitSeparator,
InvalidDecimalPoint,
InvalidExponentSymbol,
InvalidBasePrefix,
InvalidBaseSuffix,
InvalidPunctuation,
InvalidExponentFlags,
InvalidMantissaSign,
InvalidExponentSign,
InvalidSpecial,
InvalidConsecutiveIntegerDigitSeparator,
InvalidConsecutiveFractionDigitSeparator,
InvalidConsecutiveExponentDigitSeparator,
InvalidFlags,
InvalidNanString,
NanStringTooLong,
InvalidInfString,
InfStringTooLong,
InvalidInfinityString,
InfinityStringTooLong,
InfinityStringTooShort,
InvalidFloatParseAlgorithm,
InvalidRadix,
InvalidFloatPrecision,
InvalidNegativeExponentBreak,
InvalidPositiveExponentBreak,
Success,
}
const_assert!(mem::size_of::<Error>() <= 2 * mem::size_of::<usize>());
macro_rules! is_error_type {
($name:ident, $type:ident$($t:tt)*) => (
pub const fn $name(&self) -> bool {
if let Self::$type$($t)* = self {
true
} else {
false
}
}
);
}
impl Error {
pub fn index(&self) -> Option<&usize> {
match self {
Self::Overflow(index) => Some(index),
Self::Underflow(index) => Some(index),
Self::InvalidDigit(index) => Some(index),
Self::Empty(index) => Some(index),
Self::EmptyMantissa(index) => Some(index),
Self::EmptyExponent(index) => Some(index),
Self::EmptyInteger(index) => Some(index),
Self::EmptyFraction(index) => Some(index),
Self::InvalidPositiveMantissaSign(index) => Some(index),
Self::MissingMantissaSign(index) => Some(index),
Self::InvalidExponent(index) => Some(index),
Self::InvalidPositiveExponentSign(index) => Some(index),
Self::MissingExponentSign(index) => Some(index),
Self::ExponentWithoutFraction(index) => Some(index),
Self::InvalidLeadingZeros(index) => Some(index),
Self::MissingExponent(index) => Some(index),
Self::MissingSign(index) => Some(index),
Self::InvalidPositiveSign(index) => Some(index),
Self::InvalidNegativeSign(index) => Some(index),
Self::InvalidMantissaRadix => None,
Self::InvalidExponentBase => None,
Self::InvalidExponentRadix => None,
Self::InvalidDigitSeparator => None,
Self::InvalidDecimalPoint => None,
Self::InvalidExponentSymbol => None,
Self::InvalidBasePrefix => None,
Self::InvalidBaseSuffix => None,
Self::InvalidPunctuation => None,
Self::InvalidExponentFlags => None,
Self::InvalidMantissaSign => None,
Self::InvalidExponentSign => None,
Self::InvalidSpecial => None,
Self::InvalidConsecutiveIntegerDigitSeparator => None,
Self::InvalidConsecutiveFractionDigitSeparator => None,
Self::InvalidConsecutiveExponentDigitSeparator => None,
Self::InvalidFlags => None,
Self::InvalidNanString => None,
Self::NanStringTooLong => None,
Self::InvalidInfString => None,
Self::InfStringTooLong => None,
Self::InvalidInfinityString => None,
Self::InfinityStringTooLong => None,
Self::InfinityStringTooShort => None,
Self::InvalidFloatParseAlgorithm => None,
Self::InvalidRadix => None,
Self::InvalidFloatPrecision => None,
Self::InvalidNegativeExponentBreak => None,
Self::InvalidPositiveExponentBreak => None,
Self::Success => None,
}
}
is_error_type!(is_overflow, Overflow(_));
is_error_type!(is_underflow, Underflow(_));
is_error_type!(is_invalid_digit, InvalidDigit(_));
is_error_type!(is_empty, Empty(_));
is_error_type!(is_empty_mantissa, EmptyMantissa(_));
is_error_type!(is_empty_exponent, EmptyExponent(_));
is_error_type!(is_empty_integer, EmptyInteger(_));
is_error_type!(is_empty_fraction, EmptyFraction(_));
is_error_type!(is_invalid_positive_mantissa_sign, InvalidPositiveMantissaSign(_));
is_error_type!(is_missing_mantissa_sign, MissingMantissaSign(_));
is_error_type!(is_invalid_exponent, InvalidExponent(_));
is_error_type!(is_invalid_positive_exponent_sign, InvalidPositiveExponentSign(_));
is_error_type!(is_missing_exponent_sign, MissingExponentSign(_));
is_error_type!(is_exponent_without_fraction, ExponentWithoutFraction(_));
is_error_type!(is_invalid_leading_zeros, InvalidLeadingZeros(_));
is_error_type!(is_missing_exponent, MissingExponent(_));
is_error_type!(is_missing_sign, MissingSign(_));
is_error_type!(is_invalid_positive_sign, InvalidPositiveSign(_));
is_error_type!(is_invalid_negative_sign, InvalidNegativeSign(_));
is_error_type!(is_invalid_mantissa_radix, InvalidMantissaRadix);
is_error_type!(is_invalid_exponent_base, InvalidExponentBase);
is_error_type!(is_invalid_exponent_radix, InvalidExponentRadix);
is_error_type!(is_invalid_digit_separator, InvalidDigitSeparator);
is_error_type!(is_invalid_decimal_point, InvalidDecimalPoint);
is_error_type!(is_invalid_exponent_symbol, InvalidExponentSymbol);
is_error_type!(is_invalid_base_prefix, InvalidBasePrefix);
is_error_type!(is_invalid_base_suffix, InvalidBaseSuffix);
is_error_type!(is_invalid_punctuation, InvalidPunctuation);
is_error_type!(is_invalid_exponent_flags, InvalidExponentFlags);
is_error_type!(is_invalid_mantissa_sign, InvalidMantissaSign);
is_error_type!(is_invalid_exponent_sign, InvalidExponentSign);
is_error_type!(is_invalid_special, InvalidSpecial);
is_error_type!(
is_invalid_consecutive_integer_digit_separator,
InvalidConsecutiveIntegerDigitSeparator
);
is_error_type!(
is_invalid_consecutive_fraction_digit_separator,
InvalidConsecutiveFractionDigitSeparator
);
is_error_type!(
is_invalid_consecutive_exponent_digit_separator,
InvalidConsecutiveExponentDigitSeparator
);
is_error_type!(is_invalid_flags, InvalidFlags);
is_error_type!(is_invalid_nan_string, InvalidNanString);
is_error_type!(is_nan_string_too_long, NanStringTooLong);
is_error_type!(is_invalid_inf_string, InvalidInfString);
is_error_type!(is_inf_string_too_long, InfStringTooLong);
is_error_type!(is_invalid_infinity_string, InvalidInfinityString);
is_error_type!(is_infinity_string_too_long, InfinityStringTooLong);
is_error_type!(is_infinity_string_too_short, InfinityStringTooShort);
is_error_type!(is_invalid_float_parse_algorithm, InvalidFloatParseAlgorithm);
is_error_type!(is_invalid_radix, InvalidRadix);
is_error_type!(is_invalid_float_precision, InvalidFloatPrecision);
is_error_type!(is_invalid_negative_exponent_break, InvalidNegativeExponentBreak);
is_error_type!(is_invalid_positive_exponent_break, InvalidPositiveExponentBreak);
is_error_type!(is_success, Success);
}
macro_rules! write_parse_error {
($formatter:ident, $message:literal, $index:ident) => {
write!($formatter, "lexical parse error: {} at index {}", $message, $index)
};
}
macro_rules! format_message {
($formatter:ident, $message:literal) => {
write!($formatter, "lexical number format error: {}", $message)
};
}
macro_rules! options_message {
($formatter:ident, $message:literal) => {
write!($formatter, "lexical options error: {}", $message)
};
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Overflow(index) => write_parse_error!(formatter, "'numeric overflow occurred'", index),
Self::Underflow(index) => write_parse_error!(formatter, "'numeric underflow occurred'", index),
Self::InvalidDigit(index) => write_parse_error!(formatter, "'invalid digit found'", index),
Self::Empty(index) => write_parse_error!(formatter, "'the string to parse was empty'", index),
Self::EmptyMantissa(index) => write_parse_error!(formatter, "'no significant digits found'", index),
Self::EmptyExponent(index) => write_parse_error!(formatter, "'exponent notation found without an exponent'", index),
Self::EmptyInteger(index) => write_parse_error!(formatter, "'invalid float with no integer digits'", index),
Self::EmptyFraction(index) => write_parse_error!(formatter, "'invalid float with no fraction digits'", index),
Self::InvalidPositiveMantissaSign(index) => write_parse_error!(formatter, "'invalid `+` sign before significant digits'", index),
Self::MissingMantissaSign(index) => write_parse_error!(formatter, "'missing required `+/-` sign for significant digits'", index),
Self::InvalidExponent(index) => write_parse_error!(formatter, "'exponent found but not allowed'", index),
Self::InvalidPositiveExponentSign(index) => write_parse_error!(formatter, "'invalid `+` sign in exponent'", index),
Self::MissingExponentSign(index) => write_parse_error!(formatter, "'missing required `+/-` sign for exponent'", index),
Self::ExponentWithoutFraction(index) => write_parse_error!(formatter, "'invalid float containing exponent without fraction'", index),
Self::InvalidLeadingZeros(index) => write_parse_error!(formatter, "'invalid number with leading zeros before digits'", index),
Self::MissingExponent(index) => write_parse_error!(formatter, "'missing required exponent'", index),
Self::MissingSign(index) => write_parse_error!(formatter, "'missing required `+/-` sign for integer'", index),
Self::InvalidPositiveSign(index) => write_parse_error!(formatter, "'invalid `+` sign for an integer was found'", index),
Self::InvalidNegativeSign(index) => write_parse_error!(formatter, "'invalid `-` sign for an unsigned type was found'", index),
Self::InvalidMantissaRadix => format_message!(formatter, "'invalid radix for mantissa digits'"),
Self::InvalidExponentBase => format_message!(formatter, "'invalid exponent base'"),
Self::InvalidExponentRadix => format_message!(formatter, "'invalid radix for exponent digits'"),
Self::InvalidDigitSeparator => format_message!(formatter, "'invalid digit separator: must be ASCII and not a digit or a `+/-` sign'"),
Self::InvalidDecimalPoint => format_message!(formatter, "'invalid decimal point: must be ASCII and not a digit or a `+/-` sign'"),
Self::InvalidExponentSymbol => format_message!(formatter, "'invalid exponent symbol: must be ASCII and not a digit or a `+/-` sign'"),
Self::InvalidBasePrefix => format_message!(formatter, "'invalid base prefix character'"),
Self::InvalidBaseSuffix => format_message!(formatter, "'invalid base suffix character'"),
Self::InvalidPunctuation => format_message!(formatter, "'invalid punctuation: multiple characters overlap'"),
Self::InvalidExponentFlags => format_message!(formatter, "'exponent flags set while disabling exponent notation'"),
Self::InvalidMantissaSign => format_message!(formatter, "'disabled the `+` sign while requiring a sign for significant digits'"),
Self::InvalidExponentSign => format_message!(formatter, "'disabled the `+` sign while requiring a sign for exponent digits'"),
Self::InvalidSpecial => format_message!(formatter, "'special flags set while disabling special floats'"),
Self::InvalidConsecutiveIntegerDigitSeparator => format_message!(formatter, "'enabled consecutive digit separators in the integer without setting a valid location'"),
Self::InvalidConsecutiveFractionDigitSeparator => format_message!(formatter, "'enabled consecutive digit separators in the fraction without setting a valid location'"),
Self::InvalidConsecutiveExponentDigitSeparator => format_message!(formatter, "'enabled consecutive digit separators in the exponent without setting a valid location'"),
Self::InvalidFlags => format_message!(formatter, "'invalid flags enabled without the format feature'"),
Self::InvalidNanString => options_message!(formatter, "'NaN string must started with `n`'"),
Self::NanStringTooLong => options_message!(formatter, "'NaN string is too long'"),
Self::InvalidInfString => options_message!(formatter, "'short infinity string must started with `i`'"),
Self::InfStringTooLong => options_message!(formatter, "'short infinity string is too long'"),
Self::InvalidInfinityString => options_message!(formatter, "'long infinity string must started with `i`'"),
Self::InfinityStringTooLong => options_message!(formatter, "'long infinity string is too long'"),
Self::InfinityStringTooShort => options_message!(formatter, "'long infinity string is too short'"),
Self::InvalidFloatParseAlgorithm => options_message!(formatter, "'invalid combination of float parse algorithms'"),
Self::InvalidRadix => options_message!(formatter, "'invalid radix for significant digits'"),
Self::InvalidFloatPrecision => options_message!(formatter, "'invalid float precision: min digits is larger than max digits'"),
Self::InvalidNegativeExponentBreak => options_message!(formatter, "'invalid negative exponent break: value is above 0'"),
Self::InvalidPositiveExponentBreak => options_message!(formatter, "'invalid positive exponent break: value is below 0'"),
Self::Success => write!(formatter, "'not actually an error'"),
}
}
}
#[cfg(feature = "std")]
impl error::Error for Error {
}