deser-yaml 0.10.1

YAML support for deser
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! Resolution of scalars to atoms.
//!
//! In YAML the type of a plain (unquoted) scalar is determined by its
//! content: `true` is a boolean, `42` an integer and so on.  The rules for
//! this differ between YAML versions.  Quoted and block scalars are always
//! strings unless they have an explicit tag.
use std::borrow::Cow;

use deser_core::adapters::{Base64, BytesEncoding};
use deser_core::ext::{Date, Datetime, ExtValue, Offset, Time};
use deser_core::{Atom, Bytes, Implicit, ImplicitValue};

/// The YAML version that determines how plain scalars are resolved.
///
/// A document can declare its version with a `%YAML` directive.  The
/// version configured with
/// [`DeserializerConfig::set_version`](crate::DeserializerConfig::set_version) applies
/// to documents that do not declare a version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Version {
    /// YAML 1.1.
    ///
    /// In addition to what YAML 1.2 supports, `y`, `yes`, `on` (and their
    /// negations) are booleans, integers can be written in binary (`0b1010`),
    /// octal (`0777`) and base 60 (`1:30`) and numbers can contain `_` as
    /// digit separators.  Unlike YAML 1.2, `0o777` is a string.
    V1_1,
    /// YAML 1.2 with the core schema.
    #[default]
    V1_2,
}

const TAG_PREFIX: &str = "tag:yaml.org,2002:";

/// How a tag affects a scalar.
pub(crate) enum ScalarTag<'t> {
    /// The scalar is a string (the `!` tag or a quoted scalar).
    Str,
    /// A standard tag that determines the type.
    Standard(&'t str),
    /// A tag that is not known.  The value is a string, the tag is passed on.
    Custom,
}

/// Classifies a tag.
pub(crate) fn classify_tag(tag: &str) -> ScalarTag<'_> {
    if tag == "!" {
        return ScalarTag::Str;
    }
    match tag.strip_prefix(TAG_PREFIX) {
        Some(
            name @ ("str" | "int" | "float" | "bool" | "null" | "binary" | "timestamp" | "seq"
            | "map"),
        ) => ScalarTag::Standard(name),
        _ => ScalarTag::Custom,
    }
}

/// Returns `true` if the tag is a standard tag for a collection.
pub(crate) fn is_collection_tag(tag: &str, is_map: bool) -> Result<bool, &'static str> {
    match classify_tag(tag) {
        ScalarTag::Str => Ok(true),
        ScalarTag::Standard("seq") if !is_map => Ok(true),
        ScalarTag::Standard("map") if is_map => Ok(true),
        ScalarTag::Standard(_) => Err("tag does not apply to this kind of node"),
        _ => Ok(false),
    }
}

/// Resolves a plain scalar without tag.
#[inline]
pub(crate) fn resolve_plain(value: Cow<'_, str>, version: Version) -> Atom<'_> {
    match resolve_plain_str(&value, version) {
        Some(atom) => atom,
        None => Atom::Str(value.into()),
    }
}

/// Resolves a plain scalar without tag into the atom that is emitted.
///
/// The type of plain scalars is inferred from their text.  Scalars that
/// are not strings are emitted as [`Atom::Implicit`] so that types which
/// expect strings receive the text (`1.10` is `"1.10"` for a `String` and
/// `1.1` for an `f64`).  Integers which do not fit into 64 bits are emitted
/// as their value.
#[inline]
pub(crate) fn resolve_plain_implicit(value: Cow<'_, str>, version: Version) -> Atom<'_> {
    match resolve_plain_str(&value, version) {
        Some(atom) => match ImplicitValue::from_atom(&atom) {
            Some(resolved) => Atom::Implicit(Implicit::new(value, resolved)),
            None => atom,
        },
        None => Atom::Str(value.into()),
    }
}

/// Returns `true` if a plain scalar is a string in the given version.
pub(crate) fn is_plain_str(s: &str, version: Version) -> bool {
    resolve_plain_str(s, version).is_none()
}

/// Returns `true` if YAML 1.1 readers resolve a plain scalar implicitly to
/// something that the resolution of this crate does not cover.
///
/// Common YAML 1.1 readers (such as PyYAML) resolve timestamps without a
/// tag and treat `=` as the value key.  The syntax of timestamps is matched
/// without validating the date as these readers fail on invalid dates.
pub(crate) fn is_yaml11_implicit(s: &str) -> bool {
    s == "=" || is_yaml11_timestamp_syntax(s)
}

/// Matches the regular expression of YAML 1.1 timestamps.
fn is_yaml11_timestamp_syntax(s: &str) -> bool {
    let bytes = s.as_bytes();
    let mut pos = 0;
    let digits = |pos: &mut usize, min: usize, max: usize| -> bool {
        let len = bytes[*pos..]
            .iter()
            .take(max)
            .take_while(|x| x.is_ascii_digit())
            .count();
        *pos += len;
        len >= min
    };
    let expect = |pos: &mut usize, c: u8| -> bool {
        let rv = bytes.get(*pos) == Some(&c);
        *pos += rv as usize;
        rv
    };
    if !digits(&mut pos, 4, 4) || !expect(&mut pos, b'-') {
        return false;
    }
    // `YYYY-MM-DD` or `YYYY-M-D` followed by a time
    let date_start = pos;
    if !digits(&mut pos, 1, 2) || !expect(&mut pos, b'-') || !digits(&mut pos, 1, 2) {
        return false;
    }
    if pos == bytes.len() {
        return pos - date_start == 5;
    }
    match bytes[pos] {
        b'T' | b't' => pos += 1,
        b' ' | b'\t' => {
            while matches!(bytes.get(pos), Some(b' ' | b'\t')) {
                pos += 1;
            }
        }
        _ => return false,
    }
    if !digits(&mut pos, 1, 2)
        || !expect(&mut pos, b':')
        || !digits(&mut pos, 2, 2)
        || !expect(&mut pos, b':')
        || !digits(&mut pos, 2, 2)
    {
        return false;
    }
    if expect(&mut pos, b'.') {
        digits(&mut pos, 0, usize::MAX);
    }
    while matches!(bytes.get(pos), Some(b' ' | b'\t')) {
        pos += 1;
    }
    match bytes.get(pos) {
        None => true,
        Some(b'Z') => pos + 1 == bytes.len(),
        Some(b'+' | b'-') => {
            pos += 1;
            if !digits(&mut pos, 1, 2) {
                return false;
            }
            if expect(&mut pos, b':') && !digits(&mut pos, 2, 2) {
                return false;
            }
            pos == bytes.len()
        }
        _ => false,
    }
}

#[test]
fn test_yaml11_timestamp_syntax() {
    for s in [
        "2001-12-14",
        "2001-12-14t21:59:43.10-05:00",
        "2001-12-14 21:59:43.10 -5",
        "2001-12-15 2:59:43.10",
        "2002-12-14T21:59:43Z",
        "2001-02-30",
    ] {
        assert!(is_yaml11_timestamp_syntax(s), "{}", s);
    }
    for s in [
        "2001-12",
        "2001-1-1",
        "20011-12-14",
        "2001-12-14 foo",
        "hello",
    ] {
        assert!(!is_yaml11_timestamp_syntax(s), "{}", s);
    }
}

/// Returns `true` if the text of an implicit value can be written as plain
/// scalar.
///
/// This is the case if readers of all versions from `compat` on read the
/// text as the same value.  Empty text is not written as it's only null
/// in some places.
pub(crate) fn writes_as_plain(value: &Implicit, compat: Version) -> bool {
    let text = value.text().as_str();
    let reads_as = |version| {
        resolve_plain_str(text, version)
            .and_then(|atom| ImplicitValue::from_atom(&atom))
            .is_some_and(|resolved| resolved.is_same(value.value()))
    };
    !text.is_empty()
        && reads_as(Version::V1_2)
        && (compat == Version::V1_2 || reads_as(Version::V1_1))
}

fn resolve_plain_str(s: &str, version: Version) -> Option<Atom<'static>> {
    let first = match s.as_bytes().first() {
        Some(&first) => first,
        None => return Some(Atom::Null),
    };
    match first {
        b'0'..=b'9' | b'+' | b'-' | b'.' => match version {
            Version::V1_2 => parse_core_number(s),
            Version::V1_1 => parse_yaml11_int(s).or_else(|| parse_yaml11_float(s)),
        },
        b'~' if s.len() == 1 => Some(Atom::Null),
        b'n' | b'N' | b't' | b'T' | b'f' | b'F' | b'y' | b'Y' | b'o' | b'O' => {
            parse_null(s).or_else(|| parse_bool(s, version))
        }
        _ => None,
    }
}

/// Resolves a scalar with a standard tag.
pub(crate) fn resolve_standard<'x>(
    name: &str,
    value: Cow<'x, str>,
    version: Version,
) -> Result<Atom<'x>, &'static str> {
    let s = &*value;
    match name {
        "str" => Ok(Atom::Str(value.into())),
        "null" => parse_null(s).ok_or("invalid !!null value"),
        "bool" => parse_bool(s, version).ok_or("invalid !!bool value"),
        "int" => match version {
            Version::V1_2 => parse_core_int(s),
            Version::V1_1 => parse_yaml11_int(s),
        }
        .ok_or("invalid !!int value"),
        "float" => match version {
            Version::V1_2 => parse_core_float(s).or_else(|| parse_core_int(s).map(int_to_float)),
            Version::V1_1 => {
                parse_yaml11_float(s).or_else(|| parse_yaml11_int(s).map(int_to_float))
            }
        }
        .ok_or("invalid !!float value"),
        "binary" => decode_base64(s)
            .map(|bytes| Atom::Bytes(Bytes::new(bytes)))
            .ok_or("invalid !!binary value"),
        "timestamp" => parse_timestamp(s)
            .map(|value| Atom::Ext(ExtValue::owned(value)))
            .ok_or("invalid !!timestamp value"),
        _ => Err("tag does not apply to scalars"),
    }
}

/// Parses a YAML timestamp (<https://yaml.org/type/timestamp.html>).
///
/// Dates without time are local dates, timestamps without time zone are in
/// UTC.
pub(crate) fn parse_timestamp(s: &str) -> Option<Datetime> {
    let bytes = s.as_bytes();
    let mut pos = 0;
    // reads between `min` and `max` digits
    let number = |pos: &mut usize, min: usize, max: usize| -> Option<u32> {
        let len = bytes[*pos..]
            .iter()
            .take(max)
            .take_while(|x| x.is_ascii_digit())
            .count();
        if len < min {
            return None;
        }
        let rv = s[*pos..*pos + len].parse().ok()?;
        *pos += len;
        Some(rv)
    };

    let year = number(&mut pos, 4, 4)? as u16;
    let date_only = bytes.len() == 10;
    let expect =
        |pos: &mut usize, c: u8| -> Option<()> { (bytes.get(*pos) == Some(&c)).then(|| *pos += 1) };
    expect(&mut pos, b'-')?;
    let (min, max) = if date_only { (2, 2) } else { (1, 2) };
    let month = number(&mut pos, min, max)? as u8;
    expect(&mut pos, b'-')?;
    let day = number(&mut pos, min, max)? as u8;
    let date = Date { year, month, day };
    if !date.is_valid() {
        return None;
    }
    if date_only {
        return Some(Datetime::from(date));
    }

    match bytes.get(pos)? {
        b'T' | b't' => pos += 1,
        b' ' | b'\t' => {
            while let Some(b' ' | b'\t') = bytes.get(pos) {
                pos += 1;
            }
        }
        _ => return None,
    }
    let hour = number(&mut pos, 1, 2)? as u8;
    expect(&mut pos, b':')?;
    let minute = number(&mut pos, 2, 2)? as u8;
    expect(&mut pos, b':')?;
    let second = number(&mut pos, 2, 2)? as u8;
    let mut nanosecond = 0;
    if bytes.get(pos) == Some(&b'.') {
        pos += 1;
        let start = pos;
        while bytes.get(pos).is_some_and(u8::is_ascii_digit) {
            pos += 1;
        }
        // digits beyond nanoseconds are truncated
        let digits = &s[start..pos.min(start + 9)];
        if !digits.is_empty() {
            nanosecond = digits.parse::<u32>().ok()? * 10u32.pow(9 - digits.len() as u32);
        }
    }
    let time = Time {
        hour,
        minute,
        second,
        nanosecond,
    };
    if !time.is_valid() {
        return None;
    }

    while let Some(b' ' | b'\t') = bytes.get(pos) {
        pos += 1;
    }
    let offset = match bytes.get(pos) {
        // timestamps without time zone are in UTC
        None => Offset::Z,
        Some(b'Z') => {
            pos += 1;
            Offset::Z
        }
        Some(&sign @ (b'+' | b'-')) => {
            pos += 1;
            let hours = number(&mut pos, 1, 2)?;
            let minutes = if bytes.get(pos) == Some(&b':') {
                pos += 1;
                number(&mut pos, 2, 2)?
            } else {
                0
            };
            if hours > 23 || minutes > 59 {
                return None;
            }
            let minutes = (hours * 60 + minutes) as i16;
            Offset::Custom {
                minutes: if sign == b'-' { -minutes } else { minutes },
            }
        }
        _ => return None,
    };
    if pos != bytes.len() {
        return None;
    }
    Some(Datetime {
        date: Some(date),
        time: Some(time),
        offset: Some(offset),
    })
}

#[test]
fn test_parse_timestamp() {
    let ts = |s: &str| parse_timestamp(s).map(|x| x.to_string());
    assert_eq!(ts("2002-12-14").as_deref(), Some("2002-12-14"));
    assert_eq!(
        ts("2001-12-14t21:59:43.10-05:00").as_deref(),
        Some("2001-12-14T21:59:43.1-05:00")
    );
    assert_eq!(
        ts("2001-12-14 21:59:43.10 -5").as_deref(),
        Some("2001-12-14T21:59:43.1-05:00")
    );
    assert_eq!(
        ts("2001-12-15 2:59:43.10").as_deref(),
        Some("2001-12-15T02:59:43.1Z")
    );
    assert_eq!(
        ts("2001-12-15T02:59:43.1Z").as_deref(),
        Some("2001-12-15T02:59:43.1Z")
    );
    assert_eq!(
        ts("2001-1-5 02:59:43").as_deref(),
        Some("2001-01-05T02:59:43Z")
    );
    for invalid in [
        "",
        "2002-12-1",
        "2002-13-14",
        "2002-12-14 ",
        "2002-12-14T25:00:00",
        "2002-12-14T02:59",
        "2002-12-14T02:59:43X",
        "2002-12-14T02:59:43+24",
        "02002-12-14",
    ] {
        assert!(parse_timestamp(invalid).is_none(), "{}", invalid);
    }
}

fn int_to_float(atom: Atom) -> Atom<'static> {
    Atom::F64(match atom {
        Atom::U64(value) => value as f64,
        Atom::I64(value) => value as f64,
        Atom::F64(value) => value,
        Atom::Ext(ref ext) => match (ext.downcast_ref::<u128>(), ext.downcast_ref::<i128>()) {
            (Some(&value), _) => value as f64,
            (_, Some(&value)) => value as f64,
            _ => unreachable!(),
        },
        _ => unreachable!(),
    })
}

fn parse_null(s: &str) -> Option<Atom<'static>> {
    match s {
        "" | "~" | "null" | "Null" | "NULL" => Some(Atom::Null),
        _ => None,
    }
}

fn parse_bool(s: &str, version: Version) -> Option<Atom<'static>> {
    match s {
        "true" | "True" | "TRUE" => Some(Atom::Bool(true)),
        "false" | "False" | "FALSE" => Some(Atom::Bool(false)),
        "y" | "Y" | "yes" | "Yes" | "YES" | "on" | "On" | "ON" if version == Version::V1_1 => {
            Some(Atom::Bool(true))
        }
        "n" | "N" | "no" | "No" | "NO" | "off" | "Off" | "OFF" if version == Version::V1_1 => {
            Some(Atom::Bool(false))
        }
        _ => None,
    }
}

/// Splits off an optional sign.  Returns `true` for negative numbers.
fn split_sign(s: &str) -> (bool, &str) {
    match s.as_bytes().first() {
        Some(b'-') => (true, &s[1..]),
        Some(b'+') => (false, &s[1..]),
        _ => (false, s),
    }
}

/// Accumulates digits of a radix, skipping `_` if allowed.
///
/// Returns `None` if there are no digits or an invalid character.  The
/// value saturates to `None` in the magnitude if it overflows 128 bits, in
/// which case the approximate float value is returned as well.
fn accumulate(digits: &str, radix: u32, underscores: bool) -> Option<Magnitude> {
    let mut value = Some(0u128);
    let mut approx = 0f64;
    let mut seen = false;
    for c in digits.chars() {
        if c == '_' && underscores {
            continue;
        }
        let digit = c.to_digit(radix)?;
        seen = true;
        value = value
            .and_then(|v| v.checked_mul(radix as u128))
            .and_then(|v| v.checked_add(digit as u128));
        approx = approx * radix as f64 + digit as f64;
    }
    if value.is_none() && radix == 10 {
        // parsing the text rounds correctly
        approx = digits.replace('_', "").parse().unwrap_or(approx);
    }
    if seen {
        Some(Magnitude { value, approx })
    } else {
        None
    }
}

struct Magnitude {
    value: Option<u128>,
    approx: f64,
}

fn make_int(negative: bool, magnitude: Magnitude) -> Atom<'static> {
    match magnitude.value {
        Some(value) if !negative => match u64::try_from(value) {
            Ok(value) => Atom::U64(value),
            Err(_) => Atom::Ext(ExtValue::owned(value)),
        },
        Some(value) if value <= 1u128 << 63 => Atom::I64((value as i128).wrapping_neg() as i64),
        Some(value) if value <= 1u128 << 127 => {
            Atom::Ext(ExtValue::owned((value as i128).wrapping_neg()))
        }
        // integers that do not even fit into 128 bits are approximated
        _ => Atom::F64(if negative {
            -magnitude.approx
        } else {
            magnitude.approx
        }),
    }
}

/// YAML 1.2 core schema integers: `[-+]?[0-9]+`, `0o[0-7]+`, `0x[0-9a-fA-F]+`.
fn parse_core_int(s: &str) -> Option<Atom<'static>> {
    if let Some(rest) = s.strip_prefix("0o") {
        return accumulate(rest, 8, false).map(|m| make_int(false, m));
    }
    if let Some(rest) = s.strip_prefix("0x") {
        return accumulate(rest, 16, false).map(|m| make_int(false, m));
    }
    let (negative, digits) = split_sign(s);
    accumulate(digits, 10, false).map(|m| make_int(negative, m))
}

/// YAML 1.2 core schema integers and floats.
///
/// Decimal integers that fit into 64 bits are parsed in a single pass and
/// decimal floats skip the attempt to parse them as integer.  All other
/// numbers go through [`parse_core_int`] and [`parse_core_float`].
fn parse_core_number(s: &str) -> Option<Atom<'static>> {
    let bytes = s.as_bytes();
    let negative = bytes.first() == Some(&b'-');
    let digits_start = usize::from(matches!(bytes.first(), Some(b'-' | b'+')));
    let mut pos = digits_start;
    let mut value = Some(0u64);
    while let Some(&b @ b'0'..=b'9') = bytes.get(pos) {
        value = value
            .and_then(|v| v.checked_mul(10))
            .and_then(|v| v.checked_add(u64::from(b - b'0')));
        pos += 1;
    }
    match (bytes.get(pos), value) {
        (None, Some(value)) if pos > digits_start => Some(make_int(
            negative,
            Magnitude {
                value: Some(value.into()),
                approx: value as f64,
            },
        )),
        (Some(b'.' | b'e' | b'E'), _) => parse_core_float(s),
        _ => parse_core_int(s).or_else(|| parse_core_float(s)),
    }
}

/// YAML 1.1 integers: binary, octal, decimal, hexadecimal and base 60, all
/// with an optional sign and `_` separators.
fn parse_yaml11_int(s: &str) -> Option<Atom<'static>> {
    let (negative, rest) = split_sign(s);
    let magnitude = if let Some(digits) = rest.strip_prefix("0b") {
        accumulate(digits, 2, true)?
    } else if let Some(digits) = rest.strip_prefix("0x") {
        accumulate(digits, 16, true)?
    } else if rest == "0" {
        accumulate(rest, 10, false)?
    } else if let Some(digits) = rest.strip_prefix('0') {
        accumulate(digits, 8, true)?
    } else if rest
        .as_bytes()
        .first()
        .is_some_and(|b| (b'1'..=b'9').contains(b))
    {
        if rest.contains(':') {
            parse_base60(rest)?
        } else {
            accumulate(rest, 10, true)?
        }
    } else {
        return None;
    };
    Some(make_int(negative, magnitude))
}

/// Parses the base 60 integer `[1-9][0-9_]*(:[0-5]?[0-9])+`.
fn parse_base60(s: &str) -> Option<Magnitude> {
    let mut parts = s.split(':');
    let mut rv = accumulate(parts.next()?, 10, true)?;
    for part in parts {
        if !is_base60_digit(part) {
            return None;
        }
        let digit = part.parse::<u8>().ok()?;
        rv.value = rv
            .value
            .and_then(|v| v.checked_mul(60))
            .and_then(|v| v.checked_add(digit as u128));
        rv.approx = rv.approx * 60.0 + digit as f64;
    }
    Some(rv)
}

fn is_base60_digit(s: &str) -> bool {
    matches!(s.as_bytes(), [b'0'..=b'9'] | [b'0'..=b'5', b'0'..=b'9'])
}

fn parse_special_float(s: &str) -> Option<Atom<'static>> {
    match s {
        ".nan" | ".NaN" | ".NAN" => return Some(Atom::F64(f64::NAN)),
        _ => {}
    }
    let (negative, rest) = split_sign(s);
    match rest {
        ".inf" | ".Inf" | ".INF" => Some(Atom::F64(if negative {
            f64::NEG_INFINITY
        } else {
            f64::INFINITY
        })),
        _ => None,
    }
}

/// Skips ASCII digits (and `_` if allowed) and returns the number of
/// digits.
fn skip_digits(bytes: &[u8], pos: &mut usize, underscores: bool) -> usize {
    let mut count = 0;
    while let Some(&b) = bytes.get(*pos) {
        if b.is_ascii_digit() {
            count += 1;
        } else if !(b == b'_' && underscores) {
            break;
        }
        *pos += 1;
    }
    count
}

/// Skips an exponent (`[eE][-+]?[0-9]+`) if there is one.  Returns `false`
/// if the exponent is malformed.
fn skip_exponent(bytes: &[u8], pos: &mut usize, sign_required: bool) -> bool {
    if !matches!(bytes.get(*pos), Some(b'e' | b'E')) {
        return true;
    }
    *pos += 1;
    if matches!(bytes.get(*pos), Some(b'-' | b'+')) {
        *pos += 1;
    } else if sign_required {
        return false;
    }
    skip_digits(bytes, pos, false) > 0
}

fn parse_float_text(s: &str, underscores: bool) -> Option<Atom<'static>> {
    let value = if underscores && s.contains('_') {
        s.replace('_', "").parse()
    } else {
        s.parse()
    };
    value.ok().map(Atom::F64)
}

/// YAML 1.2 core schema floats:
/// `[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?` and the special
/// values.
fn parse_core_float(s: &str) -> Option<Atom<'static>> {
    if let Some(atom) = parse_special_float(s) {
        return Some(atom);
    }
    let bytes = s.as_bytes();
    let mut pos = usize::from(matches!(bytes.first(), Some(b'-' | b'+')));
    let int_digits = skip_digits(bytes, &mut pos, false);
    let mut frac_digits = 0;
    if bytes.get(pos) == Some(&b'.') {
        pos += 1;
        frac_digits = skip_digits(bytes, &mut pos, false);
    } else if int_digits == 0 {
        return None;
    }
    if int_digits == 0 && frac_digits == 0 {
        return None;
    }
    if !skip_exponent(bytes, &mut pos, false) || pos != bytes.len() {
        return None;
    }
    parse_float_text(s, false)
}

/// YAML 1.1 floats: `[-+]?[0-9][0-9_]*\.[0-9_]*([eE][-+][0-9]+)?`,
/// `[-+]?\.[0-9][0-9_]*([eE][-+][0-9]+)?`, base 60 floats and the special
/// values.
fn parse_yaml11_float(s: &str) -> Option<Atom<'static>> {
    if let Some(atom) = parse_special_float(s) {
        return Some(atom);
    }
    let (negative, rest) = split_sign(s);
    let bytes = rest.as_bytes();
    let mut pos = 0;
    match bytes.first() {
        Some(b'0'..=b'9') => {
            skip_digits(bytes, &mut pos, true);
            if bytes.get(pos) == Some(&b':') {
                return parse_base60_float(negative, rest);
            }
            if bytes.get(pos) != Some(&b'.') {
                return None;
            }
            pos += 1;
            skip_digits(bytes, &mut pos, true);
        }
        Some(b'.') if matches!(bytes.get(1), Some(b'0'..=b'9')) => {
            pos += 1;
            skip_digits(bytes, &mut pos, true);
        }
        _ => return None,
    }
    if !skip_exponent(bytes, &mut pos, true) || pos != bytes.len() {
        return None;
    }
    parse_float_text(s, true)
}

/// Parses `[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*`.
fn parse_base60_float(negative: bool, s: &str) -> Option<Atom<'static>> {
    let (int_part, frac_part) = s.split_once('.')?;
    let mut parts = int_part.split(':');
    let mut value = accumulate(parts.next()?, 10, true)?.approx;
    let mut count = 0;
    for part in parts {
        if !is_base60_digit(part) {
            return None;
        }
        value = value * 60.0 + part.parse::<u8>().ok()? as f64;
        count += 1;
    }
    if count == 0 || !frac_part.bytes().all(|b| b.is_ascii_digit() || b == b'_') {
        return None;
    }
    let frac: f64 = format!("0.{}", frac_part.replace('_', "")).parse().ok()?;
    value += frac;
    Some(Atom::F64(if negative { -value } else { value }))
}

/// Decodes base64 as used by `!!binary`.  Whitespace (the line breaks of
/// block scalars) is ignored, otherwise it decodes like other bytes
/// (leniently, see [`deser::adapters::Base64`](deser_core::adapters::Base64)).
fn decode_base64(s: &str) -> Option<Vec<u8>> {
    if s.bytes().any(|b| b.is_ascii_whitespace()) {
        let s: String = s.chars().filter(|c| !c.is_ascii_whitespace()).collect();
        Base64::decode(&s).ok()
    } else {
        Base64::decode(s).ok()
    }
}

#[test]
fn test_base64() {
    assert_eq!(decode_base64("").unwrap(), b"");
    assert_eq!(decode_base64("Zg==").unwrap(), b"f");
    assert_eq!(decode_base64("Zm8=").unwrap(), b"fo");
    assert_eq!(decode_base64("Zm9v").unwrap(), b"foo");
    assert_eq!(decode_base64("Zm9v\n YmFy").unwrap(), b"foobar");
    assert_eq!(decode_base64("Zm9v\n YmE=\n").unwrap(), b"fooba");
    // lenient like other bytes
    assert_eq!(decode_base64("Zm8").unwrap(), b"fo");
    assert_eq!(decode_base64("-_8=").unwrap(), b"\xfb\xff");
    assert_eq!(decode_base64("Zm9"), None);
    assert_eq!(decode_base64("Z=9v"), None);
    assert_eq!(decode_base64("Zm9!"), None);
    assert_eq!(decode_base64("Zm8=="), None);
    // unused bits have to be zero
    assert_eq!(decode_base64("Zh=="), None);
}

#[test]
fn test_big_ints() {
    assert_eq!(
        parse_core_int("18446744073709551616"),
        Some(Atom::Ext(ExtValue::owned(18446744073709551616u128)))
    );
    assert_eq!(
        parse_core_int("-9223372036854775808"),
        Some(Atom::I64(i64::MIN))
    );
    assert_eq!(
        parse_core_int("-9223372036854775809"),
        Some(Atom::Ext(ExtValue::owned(-9223372036854775809i128)))
    );
    assert_eq!(
        parse_core_int("-170141183460469231731687303715884105728"),
        Some(Atom::Ext(ExtValue::owned(i128::MIN)))
    );
    assert_eq!(
        parse_core_int("1000000000000000000000000000000000000000000"),
        Some(Atom::F64(1e42))
    );
}

#[test]
fn test_core_number_fast_path() {
    let tokens = [
        "0",
        "1",
        "+1",
        "-1",
        "-0",
        "007",
        "123456789",
        "9223372036854775807",
        "9223372036854775808",
        "-9223372036854775808",
        "-9223372036854775809",
        "18446744073709551615",
        "18446744073709551616",
        "-18446744073709551616",
        "340282366920938463463374607431768211456",
        "1.5",
        "-1.5",
        "+1.5",
        "1.",
        ".5",
        "-.5",
        "1e5",
        "1E+5",
        "1e-5",
        "1.5e-5",
        "1e",
        "1.e5",
        "1e400",
        "0x1f",
        "0o17",
        "-0x1f",
        ".inf",
        "-.inf",
        ".nan",
        "1_000",
        "1-2",
        "-",
        "+",
        "12a",
        "1:30",
    ];
    for token in tokens {
        let expected = parse_core_int(token).or_else(|| parse_core_float(token));
        assert_eq!(
            format!("{:?}", parse_core_number(token)),
            format!("{:?}", expected),
            "{}",
            token
        );
    }
}