sscanf 0.5.0

A sscanf (inverse of format!()) macro with near unlimited parsing capabilities
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
use std::num::*;

use crate::{
    advanced::{format_options::*, *},
    *,
};

// float syntax: https://doc.rust-lang.org/std/primitive.f32.html#grammar
//
// Float  ::= Sign? ( 'inf' | 'infinity' | 'nan' | Number )
// Number ::= ( Digit+ | Digit+ '.' Digit* | Digit* '.' Digit+ ) Exp?
// Exp    ::= 'e' Sign? Digit+
// Sign   ::= [+-]
// Digit  ::= [0-9]
// Float  ::= Sign? ( 'inf' | 'infinity' | 'nan' | Number )
#[rustfmt::skip]
const FLOAT: &str = r"[+-]?(?i:inf|infinity|nan|(?:[0-9]+|[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:e[+-]?[0-9]+)?)";
//                    \__/                                                              \______________/ Exp
//                    Sign                       \______________________________________________________/ Number

macro_rules! doc_concat {
    ($target: item, $($doc: expr),+) => {
        $(#[doc = $doc])+
        $target
    };
}

trait PrimitiveNumber: Sized {
    /// The number of bits in the type.
    const BITS: u32;
    /// Whether the type is signed or not.
    const SIGNED: bool;
    /// Parses a number from a string in the given radix.
    ///
    /// Effectively `from_str_radix`, but calling it that would create naming conflicts.
    fn parse_radix(s: &str, radix: u32) -> Option<Self>;

    /// The unsigned counterpart of the type (or the type itself if it is unsigned).
    type Unsigned: PrimitiveNumber;
    /// Converts an unsigned number to a negative signed number, if possible.
    fn negative_from_unsigned(n: Self::Unsigned) -> Option<Self>;
}

fn primitive_get_matcher<T: PrimitiveNumber>(options: &FormatOptions) -> Matcher {
    let options = &options.number;
    let radix = options.to_number();

    use regex_syntax::hir::*;
    fn optional(inner: Hir) -> Hir {
        // regex_syntax uses a repetition for optional groups
        Hir::repetition(Repetition {
            min: 0,
            max: Some(1),
            greedy: true,
            sub: Box::new(inner),
        })
    }
    fn cl(c: char) -> ClassUnicodeRange {
        ClassUnicodeRange::new(c, c)
    }

    // The final regex is:
    // sign prefix ( [ possible_chars ] { 1, num_digits } )
    // - "sign" matches an optional sign, either '+' or '-'
    // - "prefix" matches the prefix, if any
    // - "(...)" creates a capture group for the number itself
    //   - "[possible_chars]" matches one of the possible characters for the given radix
    //   - "{1,num_digits}" means repeat the previous character class one to `num_digits` times
    let mut regex = vec![];

    regex.push(optional(if T::SIGNED {
        Hir::class(Class::Unicode(ClassUnicode::new([cl('-'), cl('+')]))) // "[-+]?"
    } else {
        Hir::literal(b"+".as_slice()) // "\\+?"
    }));

    fn make_prefix(lower: char) -> Hir {
        let zero = Hir::literal(b"0".as_slice());

        let upper = lower.to_ascii_uppercase();
        let specifier = Hir::class(Class::Unicode(ClassUnicode::new([cl(lower), cl(upper)])));

        Hir::concat(vec![zero, specifier])
    }
    fn add_prefix(regex: &mut Vec<Hir>, options: NumberFormatOption) {
        let (lower, policy) = match options {
            NumberFormatOption::Binary(number_prefix_policy) => ('b', number_prefix_policy),
            NumberFormatOption::Octal(number_prefix_policy) => ('o', number_prefix_policy),
            NumberFormatOption::Decimal => return,
            NumberFormatOption::Hexadecimal(number_prefix_policy) => ('x', number_prefix_policy),
            NumberFormatOption::Other(_) => return,
        };
        match policy {
            NumberPrefixPolicy::Forbidden => {}
            NumberPrefixPolicy::Optional => regex.push(optional(make_prefix(lower))),
            NumberPrefixPolicy::Required => regex.push(make_prefix(lower)),
        }
    }
    add_prefix(&mut regex, *options);

    // possible characters for digits
    let possible_chars = if radix <= 10 {
        // "0-{radix - 1}"
        let range = ClassUnicodeRange::new('0', (b'0' + radix as u8 - 1) as char);
        Hir::class(Class::Unicode(ClassUnicode::new([range])))
    } else {
        // "0-9a-{'a' + radix - 1 - 10}"
        let number_range = ClassUnicodeRange::new('0', '9');
        let last_letter_offset = radix as u8 - 1 - 10;
        let letter_range = ClassUnicodeRange::new('a', (b'a' + last_letter_offset) as char);
        let upper_letter_range = ClassUnicodeRange::new('A', (b'A' + last_letter_offset) as char);
        Hir::class(Class::Unicode(ClassUnicode::new([
            number_range,
            letter_range,
            upper_letter_range,
        ])))
    };

    let num_digits = if radix == 2 {
        T::BITS
    } else {
        // digit conversion:   num_digits_in_base_a = num_digits_in_base_b * log(b) / log(a)
        // where log can be any type of logarithm. Since binary is base 2 and log_2(2) = 1,
        // we can use log_2 to simplify the math
        f32::ceil(T::BITS as f32 / f32::log2(radix as f32)) as u32
    };

    let digit_matcher = Hir::repetition(Repetition {
        min: 1,
        max: Some(num_digits),
        greedy: true,
        sub: Box::new(possible_chars),
    });
    let digit_matcher = Hir::capture(Capture {
        index: 0, // will be overwritten by matcher compilation
        name: None,
        sub: Box::new(digit_matcher),
    });
    regex.push(digit_matcher);

    Matcher::from_raw(Hir::concat(regex))
}

fn generic_number_parse<T: PrimitiveNumber>(
    options: &FormatOptions,
    is_negative: bool,
    number: &str,
) -> Option<T> {
    if is_negative {
        // negative numbers have a different range from positive numbers (e.g. i8::MIN is -128 while i8::MAX is 127).
        // in order to avoid an overflow when trying to parse number like -128i8, we need to parse the number as its
        // unsigned counterpart, e.g. u8::parse_radix("128", 10). This is better than having to manually check for
        // the overflowing value or constructing a new string with a leading minus sign.
        let raw_num = T::Unsigned::parse_radix(number, options.number.to_number())?;
        T::negative_from_unsigned(raw_num)
    } else {
        T::parse_radix(number, options.number.to_number())
    }
}

fn primitive_from_match_tree<T: PrimitiveNumber>(
    matches: Match<'_, '_>,
    options: &FormatOptions,
) -> Option<T> {
    let is_negative = matches.text().starts_with('-');
    let matches = matches.as_regex_matches();
    let number = matches[0].unwrap(); // We created a capture in primitive_get_matcher
    generic_number_parse(options, is_negative, number)
}

fn primitive_from_regex_override<T: PrimitiveNumber>(
    input: &str,
    options: &FormatOptions,
) -> Option<T> {
    let mut rest = input;
    let is_negative = rest.starts_with('-');
    rest = rest.strip_prefix(['+', '-']).unwrap_or(rest);

    if let Some(prefix) = options.number.prefix() {
        let prefix_upper = prefix.to_ascii_uppercase();
        if options.number.prefix_policy() == NumberPrefixPolicy::Required {
            rest = rest
                .strip_prefix(prefix)
                .or_else(|| rest.strip_prefix(&prefix_upper))?;
        } else {
            // optional
            rest = rest
                .strip_prefix(prefix)
                .or_else(|| rest.strip_prefix(&prefix_upper))
                .unwrap_or(rest);
        }
    }

    generic_number_parse::<T>(options, is_negative, rest)
}

macro_rules! impl_int {
    ($( $unsigned:ty | $signed:ty : ( $digits_2:literal , $digits_10:literal , $digits_16:literal ) ),+) => {
        $(
            impl PrimitiveNumber for $unsigned {
                const BITS: u32 = <$unsigned>::BITS;
                const SIGNED: bool = false;
                fn parse_radix(s: &str, radix: u32) -> Option<Self> {
                    <$unsigned>::from_str_radix(s, radix).ok()
                }

                type Unsigned = $unsigned;
                fn negative_from_unsigned(_: Self::Unsigned) -> Option<Self> {
                    None // unsigned types cannot be negative, so this is always None
                }
            }

            doc_concat! {
                impl FromScanf<'_> for $unsigned {
                    fn get_matcher(options: &FormatOptions) -> Matcher {
                        primitive_get_matcher::<$unsigned>(options)
                    }

                    fn from_match(matches: Match<'_, '_>, options: &FormatOptions) -> Option<Self> {
                        primitive_from_match_tree::<$unsigned>(matches, options)
                    }
                },
                "Matches an unsigned integer type with ", stringify!($bits), " bits.",
                "",
                "Note that this matches purely based on the number of digits in the given radix, so larger numbers",
                "will still be matched, but parsing them will fail.",
                "",
                "# Example",
                "```rust",
                "# use sscanf::{*, advanced::*};",
                concat!("let re = ", stringify!($signed), "::get_matcher(&Default::default()).debug_to_regex();"),
                concat!(r#"assert_eq!(re, r"((?:[\+\-]?([0-9]{1,"#, $digits_10, r#"})))");"#),
                "",
                "let hex_options = FormatOptions::builder().hex().with_prefix().build();",
                concat!("let re = ", stringify!($unsigned), "::get_matcher(&hex_options).debug_to_regex();"),
                concat!(r#"assert_eq!(re, r"((?:\+?0[Xx]([0-9A-Fa-f]{1,"#, $digits_16, r#"})))");"#),
                "```"
            }

            impl AcceptsRegexOverride<'_> for $unsigned {
                fn from_regex_match(input: &str, options: &FormatOptions) -> Option<Self> {
                    primitive_from_regex_override::<$unsigned>(input, options)
                }
            }

            impl PrimitiveNumber for $signed {
                const BITS: u32 = <$signed>::BITS;
                const SIGNED: bool = true;
                fn parse_radix(s: &str, radix: u32) -> Option<Self> {
                    <$signed>::from_str_radix(s, radix).ok()
                }

                type Unsigned = $unsigned;
                fn negative_from_unsigned(n: Self::Unsigned) -> Option<Self> {
                    // The simplest way to convert an unsigned number to its signed negative counterpart is to
                    // calculate `0 - n` with the method below.
                    <$signed>::checked_sub_unsigned(0, n)
                }
            }

            doc_concat! {
                impl FromScanf<'_> for $signed {
                    fn get_matcher(options: &FormatOptions) -> Matcher {
                        primitive_get_matcher::<$signed>(options)
                    }

                    fn from_match(matches: Match<'_, '_>, options: &FormatOptions) -> Option<Self> {
                        primitive_from_match_tree::<$signed>(matches, options)
                    }
                },
                concat!("Matches a signed number with ", $digits_2, " bits in the respective radix."),
                "",
                "```",
                "# use sscanf::*; use sscanf::advanced::*;",
                concat!("let re = ", stringify!($signed), "::get_matcher(&Default::default()).debug_to_regex();"),
                concat!(r#"assert_eq!(re, r"((?:[\+\-]?([0-9]{1,"#, $digits_10, r#"})))");"#),
                "",
                "let hex_options = FormatOptions::builder().hex().with_prefix().build();",
                concat!("let re = ", stringify!($signed), "::get_matcher(&hex_options).debug_to_regex();"),
                concat!(r#"assert_eq!(re, r"((?:[\+\-]?0[Xx]([0-9A-Fa-f]{1,"#, $digits_16, r#"})))");"#),
                "```"
            }

            impl AcceptsRegexOverride<'_> for $signed {
                fn from_regex_match(input: &str, options: &FormatOptions) -> Option<Self> {
                    primitive_from_regex_override::<$signed>(input, options)
                }
            }
        )+
    };
}
impl_int!(u8|i8: (8, 3, 2), u16|i16: (16, 5, 4), u32|i32: (32, 10, 8), u64|i64: (64, 20, 16), u128|i128: (128, 39, 32));

impl PrimitiveNumber for usize {
    const BITS: u32 = <usize>::BITS;
    const SIGNED: bool = false;
    fn parse_radix(s: &str, radix: u32) -> Option<Self> {
        usize::from_str_radix(s, radix).ok()
    }

    type Unsigned = usize;
    fn negative_from_unsigned(_: Self::Unsigned) -> Option<Self> {
        None // unsigned types cannot be negative, so this is always None
    }
}

/// Matches an unsigned number with a platform-specific number of bits in the respective radix.
///
/// ```
/// # use sscanf::*; use sscanf::advanced::*;
/// #[cfg(target_pointer_width = "64")]
/// {
///     let re = usize::get_matcher(&Default::default()).debug_to_regex();
///     assert_eq!(re, r"((?:\+?([0-9]{1,20})))");
///
///     let hex_options = FormatOptions::builder().hex().with_prefix().build();
///     let re = usize::get_matcher(&hex_options).debug_to_regex();
///     assert_eq!(re, r"((?:\+?0[Xx]([0-9A-Fa-f]{1,16})))");
/// }
/// #[cfg(target_pointer_width = "32")]
/// {
///     let re = usize::get_matcher(&Default::default()).debug_to_regex();
///     assert_eq!(re, r"((?:\+?([0-9]{1,10})))");
///
///     let hex_options = FormatOptions::builder().hex().with_prefix().build();
///     let re = usize::get_matcher(&hex_options).debug_to_regex();
///     assert_eq!(re, r"((?:\+?0[Xx]([0-9A-Fa-f]{1,8})))");
/// }
/// ```
impl FromScanf<'_> for usize {
    fn get_matcher(options: &FormatOptions) -> Matcher {
        primitive_get_matcher::<usize>(options)
    }

    fn from_match(matches: Match<'_, '_>, options: &FormatOptions) -> Option<Self> {
        primitive_from_match_tree::<usize>(matches, options)
    }
}

impl AcceptsRegexOverride<'_> for usize {
    fn from_regex_match(input: &str, options: &FormatOptions) -> Option<Self> {
        primitive_from_regex_override::<usize>(input, options)
    }
}

impl PrimitiveNumber for isize {
    const BITS: u32 = <isize>::BITS;
    const SIGNED: bool = true;
    fn parse_radix(s: &str, radix: u32) -> Option<Self> {
        isize::from_str_radix(s, radix).ok()
    }

    type Unsigned = usize;
    fn negative_from_unsigned(n: Self::Unsigned) -> Option<Self> {
        // The simplest way to convert an unsigned number to its signed negative counterpart is to
        // calculate `0 - n` with the method below.
        isize::checked_sub_unsigned(0, n)
    }
}

/// Matches a signed number with a platform-specific number of bits in the respective radix.
///
/// ```
/// # use sscanf::*; use sscanf::advanced::*;
/// #[cfg(target_pointer_width = "64")]
/// {
///     let re = isize::get_matcher(&Default::default()).debug_to_regex();
///     assert_eq!(re, r"((?:[\+\-]?([0-9]{1,20})))");
///
///     let hex_options = FormatOptions::builder().hex().with_prefix().build();
///     let re = isize::get_matcher(&hex_options).debug_to_regex();
///     assert_eq!(re, r"((?:[\+\-]?0[Xx]([0-9A-Fa-f]{1,16})))");
/// }
/// #[cfg(target_pointer_width = "32")]
/// {
///     let re = isize::get_matcher(&Default::default()).debug_to_regex();
///     assert_eq!(re, r"((?:[\+\-]?([0-9]{1,10})))");
///
///     let hex_options = FormatOptions::builder().hex().with_prefix().build();
///     let re = isize::get_matcher(&hex_options).debug_to_regex();
///     assert_eq!(re, r"((?:[\+\-]?0[Xx]([0-9A-Fa-f]{1,8})))");
/// }
/// ```
impl FromScanf<'_> for isize {
    fn get_matcher(options: &FormatOptions) -> Matcher {
        primitive_get_matcher::<isize>(options)
    }

    fn from_match(matches: Match<'_, '_>, options: &FormatOptions) -> Option<Self> {
        primitive_from_match_tree::<isize>(matches, options)
    }
}

impl AcceptsRegexOverride<'_> for isize {
    fn from_regex_match(input: &str, options: &FormatOptions) -> Option<Self> {
        primitive_from_regex_override::<isize>(input, options)
    }
}

macro_rules! impl_non_zero {
    ($( $ty:ty : $base:ty ),+) => {
        $(
            impl PrimitiveNumber for $ty {
                const BITS: u32 = <$ty>::BITS;
                const SIGNED: bool = <$base>::SIGNED;
                fn parse_radix(s: &str, radix: u32) -> Option<Self> {
                    <$base>::from_str_radix(s, radix).ok().and_then(Self::new)
                }

                type Unsigned = <$base as PrimitiveNumber>::Unsigned;
                fn negative_from_unsigned(n: Self::Unsigned) -> Option<Self> {
                    <$base>::negative_from_unsigned(n).and_then(Self::new)
                }
            }

            doc_concat! {
                impl FromScanf<'_> for $ty {
                    fn get_matcher(options: &FormatOptions) -> Matcher {
                        primitive_get_matcher::<$base>(options)
                    }

                    fn from_match(matches: Match<'_, '_>, options: &FormatOptions) -> Option<Self> {
                        primitive_from_match_tree::<$base>(matches, options).and_then(Self::new)
                    }
                },
                concat!("Matches a non-zero [", stringify!($base), "](trait.FromScanf.html#impl-FromScanf<'_>-for-", stringify!($base), ").")
            }

            impl AcceptsRegexOverride<'_> for $ty {
                fn from_regex_match(input: &str, options: &FormatOptions) -> Option<Self> {
                    primitive_from_regex_override::<$base>(input, options).and_then(Self::new)
                }
            }
        )+
    };
}
impl_non_zero!(NonZeroU8:u8, NonZeroU16:u16, NonZeroU32:u32, NonZeroU64:u64, NonZeroU128:u128, NonZeroUsize:usize);
impl_non_zero!(NonZeroI8:i8, NonZeroI16:i16, NonZeroI32:i32, NonZeroI64:i64, NonZeroI128:i128, NonZeroIsize:isize);

macro_rules! impl_float {
    ($($ty: ty),+) => {$(
        /// Matches a floating point number.
        ///
        /// See <https://doc.rust-lang.org/std/primitive.f32.html#grammar> for the syntax.
        impl FromScanf<'_> for $ty {
            fn get_matcher(_: &FormatOptions) -> Matcher {
                Matcher::from_regex(FLOAT).unwrap()
            }

            fn from_match(matches: Match<'_, '_>, _: &FormatOptions) -> Option<Self> {
                matches.text().parse().ok()
            }
        }

        impl AcceptsRegexOverride<'_> for $ty {
            fn from_regex_match(input: &str, _: &FormatOptions) -> Option<Self> {
                input.parse().ok()
            }
        }
    )+};
}
impl_float!(f32, f64);

#[cfg(test)]
mod tests {
    use super::*;

    fn to_string_radix(digit: i32) -> char {
        "0123456789abcdefghijklmnopqrstuvwxyz".as_bytes()[digit as usize] as char
    }
    /// Adds one to a string representation of a number in the given radix.
    ///
    /// Has to be done like this because we want to exceed the maximum value of all types, including u128 and i128.
    fn str_add_one(mut s: &str, radix: u32) -> String {
        let negative = if let Some(stripped) = s.strip_prefix('-') {
            s = stripped;
            true
        } else {
            false
        };
        let bytes = s.as_bytes();
        let mut carry = 1;
        let mut result = Vec::with_capacity(bytes.len());
        for &byte in bytes.iter().rev() {
            let mut digit = (byte as char).to_digit(radix).unwrap() as i32;
            digit += carry;
            carry = 0;
            if digit >= radix as i32 {
                digit -= radix as i32;
                carry = 1;
            } else if digit < 0 {
                digit += radix as i32;
                carry = -1;
            }
            result.push(to_string_radix(digit));
        }
        if carry > 0 {
            result.push(to_string_radix(carry));
        }
        if negative {
            result.push('-');
        }
        result.reverse();
        result.into_iter().collect()
    }

    #[track_caller]
    fn assert_parse<T>(value: T, value_str: &str, options: NumberFormatOption)
    where
        T: ToString + PartialEq + std::fmt::Debug + for<'input> FromScanf<'input>,
    {
        let name = std::any::type_name::<T>();

        let options = FormatOptions {
            number: options,
            ..Default::default()
        };
        let mut parser = Parser::<T>::with_options(options);
        let output = parser.parse(value_str);

        let Some(parsed_value) = output else {
            panic!("Parser does not match {value_str} for {name}:\n{parser:?}");
        };

        assert_eq!(
            parsed_value, value,
            "Parser for {name} did not parse {value_str} correctly",
        );
    }

    #[track_caller]
    fn assert_parse_fails<T>(value_str: &str, options: NumberFormatOption)
    where
        T: ToString + PartialEq + std::fmt::Debug + for<'input> FromScanf<'input>,
    {
        let name = std::any::type_name::<T>();

        let options = FormatOptions {
            number: options,
            ..Default::default()
        };
        let mut parser = Parser::<T>::with_options(options);
        let result = parser.parse(value_str);

        assert!(
            result.is_none(),
            "Parser for {name} should have failed to parse {value_str}, but it succeeded with {result:?}",
        );
    }

    macro_rules! create_type_limit_tests {
        ($ty:ty) => {
            use NumberFormatOption::*;
            use NumberPrefixPolicy::*;
            let max_str = <$ty>::MAX.to_string();
            let min_str = <$ty>::MIN.to_string();
            assert_parse::<$ty>(<$ty>::MAX, &max_str, Default::default());
            assert_parse::<$ty>(<$ty>::MIN, &min_str, Default::default());
            assert_parse::<$ty>(0, "0", Default::default());
            assert_parse::<$ty>(35, "35", Default::default());
            assert_parse::<$ty>(5, "5", Hexadecimal(Forbidden));
            assert_parse::<$ty>(10, "a", Hexadecimal(Forbidden));
            assert_parse::<$ty>(10, "A", Hexadecimal(Forbidden));
            assert_parse::<$ty>(15, "f", Hexadecimal(Forbidden));

            assert_parse::<$ty>(15, "f", Hexadecimal(Forbidden));
            assert_parse_fails::<$ty>("0xf", Hexadecimal(Forbidden));
            assert_parse_fails::<$ty>("0Xf", Hexadecimal(Forbidden));

            assert_parse::<$ty>(15, "f", Hexadecimal(Optional));
            assert_parse::<$ty>(15, "0xf", Hexadecimal(Optional));
            assert_parse::<$ty>(15, "0Xf", Hexadecimal(Optional));

            assert_parse_fails::<$ty>("f", Hexadecimal(Required));
            assert_parse::<$ty>(15, "0xf", Hexadecimal(Required));
            assert_parse::<$ty>(15, "0Xf", Hexadecimal(Required));

            let too_much = str_add_one(&max_str, 10);
            assert_parse_fails::<$ty>(&too_much, Default::default());

            let max_hex = format!("{:x}", <$ty>::MAX);
            let max_hex_prefixed = format!("0x{max_hex}");
            assert_parse::<$ty>(<$ty>::MAX, &max_hex, Hexadecimal(Forbidden));
            assert_parse::<$ty>(<$ty>::MAX, &max_hex, Hexadecimal(Optional));
            assert_parse_fails::<$ty>(&max_hex, Hexadecimal(Required));

            assert_parse_fails::<$ty>(&max_hex_prefixed, Hexadecimal(Forbidden));
            assert_parse::<$ty>(<$ty>::MAX, &max_hex_prefixed, Hexadecimal(Optional));
            assert_parse::<$ty>(<$ty>::MAX, &max_hex_prefixed, Hexadecimal(Required));

            let too_much_hex = str_add_one(&max_hex, 16);
            assert_parse_fails::<$ty>(&too_much_hex, Hexadecimal(Forbidden));
        };
    }

    macro_rules! create_signed_type_limit_tests {
        ($ty:ty) => {
            create_type_limit_tests!($ty);

            let min_str = <$ty>::MIN.to_string();
            let too_little = str_add_one(&min_str, 10);
            assert_parse_fails::<$ty>(&too_little, Default::default());

            let raw_min_hex = format!("{:x}", <$ty>::MIN.abs_diff(0));
            let min_hex = format!("-{raw_min_hex}");
            assert_parse::<$ty>(<$ty>::MIN, &min_hex, Hexadecimal(Forbidden));
            assert_parse::<$ty>(<$ty>::MIN, &min_hex, Hexadecimal(Optional));
            assert_parse_fails::<$ty>(&min_hex, Hexadecimal(Required));

            let min_hex_prefixed = format!("-0x{raw_min_hex}");
            assert_parse_fails::<$ty>(&min_hex_prefixed, Hexadecimal(Forbidden));
            assert_parse::<$ty>(<$ty>::MIN, &min_hex_prefixed, Hexadecimal(Optional));
            assert_parse::<$ty>(<$ty>::MIN, &min_hex_prefixed, Hexadecimal(Required));

            let min_hex_prefixed = format!("-0X{raw_min_hex}");
            assert_parse_fails::<$ty>(&min_hex_prefixed, Hexadecimal(Forbidden));
            assert_parse::<$ty>(<$ty>::MIN, &min_hex_prefixed, Hexadecimal(Optional));
            assert_parse::<$ty>(<$ty>::MIN, &min_hex_prefixed, Hexadecimal(Required));

            let too_little_hex = str_add_one(&min_hex, 16);
            assert_parse_fails::<$ty>(&too_little_hex, Hexadecimal(Forbidden));
        };
    }

    #[rustfmt::skip]
    mod test_spam {
        use super::*;
        // test functions need to have unique names, but we can't create identifiers with macros yet, so
        // we have to manually write these out.
        #[test] fn test_type_parser_limits_u8() { create_type_limit_tests!(u8); }
        #[test] fn test_type_parser_limits_u16() { create_type_limit_tests!(u16); }
        #[test] fn test_type_parser_limits_u32() { create_type_limit_tests!(u32); }
        #[test] fn test_type_parser_limits_u64() { create_type_limit_tests!(u64); }
        #[test] fn test_type_parser_limits_u128() { create_type_limit_tests!(u128); }
        #[test] fn test_type_parser_limits_usize() { create_type_limit_tests!(usize); }
        #[test] fn test_type_parser_limits_i8() { create_signed_type_limit_tests!(i8); }
        #[test] fn test_type_parser_limits_i16() { create_signed_type_limit_tests!(i16); }
        #[test] fn test_type_parser_limits_i32() { create_signed_type_limit_tests!(i32); }
        #[test] fn test_type_parser_limits_i64() { create_signed_type_limit_tests!(i64); }
        #[test] fn test_type_parser_limits_i128() { create_signed_type_limit_tests!(i128); }
        #[test] fn test_type_parser_limits_isize() { create_signed_type_limit_tests!(isize); }
    }

    macro_rules! create_float_parser_test {
        ($ty:ty, $ident:ident) => {
            assert_parse::<$ty>(1.0, "1.0", Default::default());
            assert_parse::<$ty>(-1.0, "-1.0", Default::default());
            assert_parse::<$ty>(
                std::$ident::consts::PI,
                &std::$ident::consts::PI.to_string(),
                Default::default(),
            );
            assert_parse::<$ty>(
                -std::$ident::consts::PI,
                &(-std::$ident::consts::PI).to_string(),
                Default::default(),
            );
            assert_parse::<$ty>(10_000_000_000.0, "1e10", Default::default());
            assert_parse::<$ty>(-10_000_000_000.0, "-1e10", Default::default());
            assert_parse::<$ty>(0.000_000_000_1, "1e-10", Default::default());
            assert_parse::<$ty>(-0.000_000_000_1, "-1e-10", Default::default());

            // Infinity and NaN
            assert_parse::<$ty>(<$ty>::INFINITY, "inf", Default::default());
            assert_parse::<$ty>(<$ty>::INFINITY, "infinity", Default::default());
            // not checking NaN here, because the assert_eq would fail

            // Case insensitivity
            assert_parse::<$ty>(<$ty>::INFINITY, "INF", Default::default());
            assert_parse::<$ty>(<$ty>::INFINITY, "INFINITY", Default::default());
            assert_parse::<$ty>(<$ty>::INFINITY, "iNfiNIty", Default::default());
        };
    }
    #[test]
    fn test_float_parser_f32() {
        create_float_parser_test!(f32, f32);
    }
    #[test]
    fn test_float_parser_f64() {
        create_float_parser_test!(f64, f64);
    }
}