time 0.3.48

Date and time library. Fully interoperable with the standard library. Mostly compatible with #![no_std].
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Lexer for parsing format descriptions.

use alloc::borrow::ToOwned as _;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;

use super::format_item::{
    AstComponent, component_from_ast, ident_eq, parse_optional_format_modifier,
};
use super::{
    Error, Location, Span, Spanned, SpannedValue, WithLocation, WithLocationValue as _, unused,
};
use crate::error::InvalidFormatDescription;
use crate::format_description::__private::FormatDescriptionV3Inner;
use crate::format_description::{BorrowedFormatItem, FormatDescriptionV3, OwnedFormatItem};
use crate::hint;
use crate::internal_macros::try_likely_ok;

#[must_use]
enum Context {
    Component,
    Literal,
}

impl Context {
    #[inline]
    const fn is_component(&self) -> bool {
        matches!(self, Self::Component)
    }

    #[inline]
    const fn is_literal(&self) -> bool {
        matches!(self, Self::Literal)
    }
}

enum NextModifier<'a> {
    Modifier(Modifier<'a>),
    TrailingWhitespace(Spanned<&'a str>),
    None,
}

type ParseItemWithLiteralLifetime<'input, const VERSION: u8, const OWNED: bool> =
    <() as ParseTarget<'input, VERSION, OWNED>>::ItemWithLiteralLifetime;
type ParseOutput<'input, const VERSION: u8, const OWNED: bool> =
    <() as ParseTarget<'input, VERSION, OWNED>>::Output;

pub(super) trait ParseTarget<'input, const VERSION: u8, const OWNED: bool> {
    type ItemWithLiteralLifetime;
    type ItemWithStaticLifetime;
    type Component: TryFrom<AstComponent, Error: Into<Error>>;
    type Output;

    fn literal(value: &'input str) -> Self::ItemWithLiteralLifetime;
    fn component(component: Self::Component) -> Result<Self::ItemWithLiteralLifetime, Error>;
    fn optional(
        value: Vec<Self::ItemWithLiteralLifetime>,
        format: bool,
        span: Span,
    ) -> Result<Self::ItemWithLiteralLifetime, Error>;
    fn first(
        value: Vec<Vec<Self::ItemWithLiteralLifetime>>,
        span: Span,
    ) -> Result<Self::ItemWithLiteralLifetime, Error>;
    fn parse(s: &'input str) -> Result<Self::Output, Error>;
}

pub(super) fn parse_generic<'input, const VERSION: u8, const OWNED: bool>(
    s: &'input str,
) -> Result<ParseOutput<'input, VERSION, OWNED>, Error>
where
    (): ParseTarget<'input, VERSION, OWNED>,
{
    <() as ParseTarget<'input, VERSION, OWNED>>::parse(s)
}

macro_rules! v1_v2_parse_target {
    ($($version:literal)+) => {$(
        impl<'input> ParseTarget<'input, $version, false> for () {
            type ItemWithLiteralLifetime = BorrowedFormatItem<'input>;
            type ItemWithStaticLifetime = BorrowedFormatItem<'static>;
            type Component = AstComponent;
            type Output = Vec<BorrowedFormatItem<'input>>;

            #[inline]
            fn literal(value: &'input str) -> Self::ItemWithLiteralLifetime {
                BorrowedFormatItem::StringLiteral(value)
            }

            #[inline]
            fn component(component: Self::Component) -> Result<Self::ItemWithStaticLifetime, Error>
            {
                Ok(BorrowedFormatItem::Component(try_likely_ok!(
                    component.try_into()
                )))
            }

            #[inline]
            fn optional(
                _value: Vec<Self::ItemWithLiteralLifetime>,
                _format: bool,
                span: Span,
            ) -> Result<Self::ItemWithLiteralLifetime, Error> {
                hint::cold_path();
                Err(Error {
                    _inner: unused(span.error(
                        "optional items are not supported in runtime-parsed format descriptions",
                    )),
                    public: InvalidFormatDescription::NotSupported {
                        what: "optional item",
                        context: "runtime-parsed format descriptions",
                        index: span.start.byte as usize,
                    },
                })
            }

            #[inline]
            fn first(_value: Vec<Vec<Self::ItemWithLiteralLifetime>>, span: Span)
                -> Result<Self::ItemWithLiteralLifetime, Error>
            {
                hint::cold_path();
                Err(Error {
                    _inner: unused(span.error(
                        "'first' items are not supported in runtime-parsed format descriptions",
                    )),
                    public: InvalidFormatDescription::NotSupported {
                        what: "'first' item",
                        context: "runtime-parsed format descriptions",
                        index: span.start.byte as usize,
                    },
                })
            }

            #[inline]
            fn parse(s: &'input str) -> Result<ParseOutput<'input, $version, false>, Error> {
                let mut items = Vec::with_capacity(16);
                let mut lexer = Lexer::<$version, false>::new(s);
                while !lexer.input.is_empty() {
                    items.push(try_likely_ok!(lexer.parse_next_item()));
                }
                Ok(items)
            }
        }

        impl<'input> ParseTarget<'input, $version, true> for () {
            type ItemWithLiteralLifetime = OwnedFormatItem;
            type ItemWithStaticLifetime = OwnedFormatItem;
            type Component = AstComponent;
            type Output = OwnedFormatItem;

            #[inline]
            fn literal(value: &'input str) -> Self::ItemWithLiteralLifetime {
                OwnedFormatItem::StringLiteral(value.to_owned().into_boxed_str())
            }

            #[inline]
            fn component(component: Self::Component) -> Result<Self::ItemWithStaticLifetime, Error>
            {
                Ok(OwnedFormatItem::Component(try_likely_ok!(
                    component.try_into()
                )))
            }

            #[inline]
            fn optional(
                value: Vec<Self::ItemWithLiteralLifetime>,
                format: bool,
                span: Span,
            ) -> Result<Self::ItemWithLiteralLifetime, Error> {
                if !format {
                    hint::cold_path();
                    return Err(Error {
                        _inner: unused(span.error(
                            "v1 and v2 format descriptions do not support optional items that are \
                             not formatted",
                        )),
                        public: InvalidFormatDescription::NotSupported {
                            what: "optional item with `format:false`",
                            context: "v1 and v2 format descriptions",
                            index: span.start.byte as usize,
                        },
                    });
                }

                Ok(OwnedFormatItem::Optional(Box::new(
                    items_to_owned_format_item(value),
                )))
            }

            #[inline]
            fn first(value: Vec<Vec<Self::ItemWithLiteralLifetime>>, _span: Span)
                -> Result<Self::ItemWithLiteralLifetime, Error>
            {
                Ok(OwnedFormatItem::First(
                    value.into_iter().map(items_to_owned_format_item).collect(),
                ))
            }

            #[inline]
            fn parse(s: &'input str) -> Result<ParseOutput<'input, $version, true>, Error> {
                let mut items = Vec::with_capacity(16);
                let mut lexer = Lexer::<$version, true>::new(s);
                while !lexer.input.is_empty() {
                    items.push(try_likely_ok!(lexer.parse_next_item()));
                }
                Ok(items_to_owned_format_item(items))
            }
        }
    )+};
}

macro_rules! v3_parse_target {
    ($owned:tt, $output_lt:lifetime, $literal:expr, $items_to_v3:expr) => {
        impl<'input> ParseTarget<'input, 3, $owned> for () {
            type ItemWithLiteralLifetime = FormatDescriptionV3Inner<$output_lt>;
            type ItemWithStaticLifetime = FormatDescriptionV3Inner<'static>;
            type Component = FormatDescriptionV3Inner<'static>;
            type Output = FormatDescriptionV3<$output_lt>;

            #[inline]
            fn literal(value: &'input str) -> Self::ItemWithLiteralLifetime {
                $literal(value.into())
            }

            #[inline]
            fn component(
                component: Self::Component,
            ) -> Result<Self::ItemWithStaticLifetime, Error> {
                Ok(component)
            }

            #[inline]
            fn optional(
                value: Vec<Self::ItemWithLiteralLifetime>,
                format: bool,
                _span: Span,
            ) -> Result<Self::ItemWithLiteralLifetime, Error> {
                Ok(FormatDescriptionV3Inner::OwnedOptional {
                    format,
                    item: Box::new($items_to_v3(value)),
                })
            }

            #[inline]
            fn first(
                value: Vec<Vec<Self::ItemWithLiteralLifetime>>,
                _span: Span,
            ) -> Result<Self::ItemWithLiteralLifetime, Error> {
                Ok(FormatDescriptionV3Inner::OwnedFirst(
                    value.into_iter().map($items_to_v3).collect(),
                ))
            }

            #[inline]
            fn parse(s: &'input str) -> Result<Self::Output, Error> {
                let mut items = Vec::with_capacity(16);
                let mut lexer = Lexer::<3, false>::new(s);

                while let Some(&byte) = lexer.input.first() {
                    let location = Location {
                        byte: lexer.byte_pos,
                    };
                    let token = match byte {
                        b'[' => lexer.consume_component(location),
                        b']' => {
                            hint::cold_path();
                            return Err(Error {
                                _inner: unused(location.error("right brackets must be escaped")),
                                public: InvalidFormatDescription::Expected {
                                    what: "right bracket to be escaped",
                                    index: location.byte as usize,
                                },
                            });
                        }
                        b'\\' => lexer
                            .consume_backslash_escape_sequence(location)
                            .map(<() as ParseTarget<'input, 3, $owned>>::literal),
                        _ => Ok(<() as ParseTarget<'input, 3, $owned>>::literal(
                            lexer.consume_literal().into(),
                        )),
                    };

                    items.push(try_likely_ok!(token));
                }

                Ok($items_to_v3(items).into_opaque())
            }
        }
    };
}

v1_v2_parse_target!(1 2);
v3_parse_target!(false, 'input, FormatDescriptionV3Inner::BorrowedLiteral, items_to_v3_borrowed);
v3_parse_target!(true, 'static, FormatDescriptionV3Inner::OwnedLiteral, items_to_v3_owned);

fn items_to_owned_format_item(items: Vec<OwnedFormatItem>) -> OwnedFormatItem {
    match <[_; 1]>::try_from(items) {
        Ok([item]) => item,
        Err(items) => OwnedFormatItem::Compound(items.into_boxed_slice()),
    }
}

fn items_to_v3_borrowed<'input>(
    items: Vec<FormatDescriptionV3Inner<'input>>,
) -> FormatDescriptionV3Inner<'input> {
    match <[_; 1]>::try_from(items) {
        Ok([item]) => item,
        Err(items) => FormatDescriptionV3Inner::OwnedCompound(items.into_boxed_slice()),
    }
}

fn items_to_v3_owned(
    items: Vec<FormatDescriptionV3Inner<'_>>,
) -> FormatDescriptionV3Inner<'static> {
    match <[_; 1]>::try_from(items) {
        Ok([item]) => item.into_owned(),
        Err(items) => FormatDescriptionV3Inner::OwnedCompound(
            items
                .into_iter()
                .map(FormatDescriptionV3Inner::into_owned)
                .collect(),
        ),
    }
}

/// An iterator over the lexed tokens.
pub(super) struct Lexer<'input, const VERSION: u8, const OWNED: bool> {
    input: &'input [u8],
    depth: u8,
    byte_pos: u32,
}

impl<'input, const VERSION: u8, const OWNED: bool> Lexer<'input, VERSION, OWNED> {
    /// Parse the string into a series of [`Token`]s.
    ///
    /// `VERSION` controls the version of the format description that is being parsed.
    ///
    /// - When `VERSION` is 1, `[[` is the only escape sequence, resulting in a literal `[`. For the
    ///   start of a nested format description, a single `[` is used and is _never_ part of the
    ///   escape sequence. For example, `[optional [[day]]]` will lex successfully, ultimately
    ///   resulting in a component named `optional` with the nested component `day`.
    /// - When `VERSION` is 2 or 3, all escape sequences begin with `\`. The only characters that
    ///   may currently follow are `\`, `[`, and `]`, all of which result in the literal character.
    ///   All other characters result in a lex error.
    #[inline]
    pub(super) const fn new(input: &'input str) -> Self {
        Self {
            input: input.as_bytes(),
            depth: 0,
            byte_pos: 0,
        }
    }

    /// Advance the input by the given number of bytes.
    #[inline]
    fn advance(&mut self, bytes: u32) {
        self.input = &self.input[bytes as usize..];
        self.byte_pos += bytes;
    }

    /// Whether the lexer is currently parsing a component or a literal.
    #[inline]
    const fn context(&self) -> Context {
        if self.depth.is_multiple_of(2) {
            Context::Literal
        } else {
            Context::Component
        }
    }

    /// Consume the next token if it is a component item that is whitespace.
    #[inline]
    fn consume_whitespace(&mut self) -> Option<Spanned<&'input str>> {
        debug_assert!(self.context().is_component());

        let bytes = self
            .input
            .iter()
            .take_while(|byte| byte.is_ascii_whitespace())
            .count() as u32;

        if bytes == 0 {
            return None;
        }

        let start_loc = Location {
            byte: self.byte_pos,
        };
        let end_loc = Location {
            byte: self.byte_pos + bytes,
        };

        // Safety: Runtime format descriptions always originate with a string passed as a parameter
        // and we have only consumed full codepoints, ensuring that a valid string remains.
        let value = unsafe { str::from_utf8_unchecked(&self.input[..bytes as usize]) };
        self.advance(bytes);

        Some(value.spanned(start_loc.to(end_loc)))
    }

    /// Consume the next token if it is a component item that is not whitespace.
    #[inline]
    fn consume_component_part(&mut self) -> Option<Spanned<&'input str>> {
        debug_assert!(self.context().is_component());

        let bytes = self
            .input
            .iter()
            .take_while(|byte| !byte.is_ascii_whitespace() && !matches!(byte, b'\\' | b'[' | b']'))
            .count() as u32;

        if bytes == 0 {
            hint::cold_path();
            return None;
        }

        let start_loc = Location {
            byte: self.byte_pos,
        };
        let end_loc = Location {
            byte: self.byte_pos + bytes,
        };

        // Safety: Runtime format descriptions always originate with a string passed as a parameter
        // and we have only consumed full codepoints, ensuring that a valid string remains.
        let value = unsafe { str::from_utf8_unchecked(&self.input[..bytes as usize]) };
        self.advance(bytes);

        Some(value.spanned(start_loc.to(end_loc)))
    }

    /// Consume the next token if it is a closing bracket.
    #[inline]
    fn consume_closing_bracket(&mut self) -> Option<Location> {
        if self.input.first() != Some(&b']') {
            hint::cold_path();
            return None;
        }

        self.depth -= 1;

        let location = Location {
            byte: self.byte_pos,
        };
        self.advance(1);
        Some(location)
    }

    /// Consume the next token if it is a component name. The caller is expected to be inside a
    /// component header.
    #[inline]
    fn consume_component_name(
        &mut self,
        opening_bracket: Location,
    ) -> Result<Spanned<&'input str>, Error> {
        let leading_whitespace = self.consume_whitespace().is_some();

        let Some(name) = self.consume_component_part() else {
            hint::cold_path();
            let location = if leading_whitespace {
                opening_bracket.offset(1)
            } else {
                opening_bracket
            };
            return Err(Error {
                _inner: unused(location.error("expected component name")),
                public: InvalidFormatDescription::MissingComponentName {
                    index: location.byte as usize,
                },
            });
        };

        Ok(name)
    }

    #[inline]
    fn consume_modifier(&mut self) -> Result<NextModifier<'input>, Error> {
        let Some(whitespace) = self.consume_whitespace() else {
            hint::cold_path();
            return Ok(NextModifier::None);
        };

        let Some(token) = self.consume_component_part() else {
            hint::cold_path();
            return Ok(NextModifier::TrailingWhitespace(whitespace));
        };

        let modifier = try_likely_ok!(self.modifier_from_token(token));
        Ok(NextModifier::Modifier(modifier))
    }

    /// Parse a component.
    #[inline]
    fn consume_component(
        &mut self,
        opening_bracket: Location,
    ) -> Result<ParseItemWithLiteralLifetime<'input, VERSION, OWNED>, Error>
    where
        (): ParseTarget<'input, VERSION, OWNED>,
    {
        match self.depth.checked_add(1) {
            Some(depth) => self.depth = depth,
            None => {
                hint::cold_path();
                return Err(Error {
                    _inner: unused(opening_bracket.error("too much nesting")),
                    public: InvalidFormatDescription::NotSupported {
                        what: "highly-nested format description",
                        context: "",
                        index: opening_bracket.byte as usize,
                    },
                });
            }
        };
        // consume the opening bracket, which was checked prior to calling this method
        self.advance(1);

        let name = try_likely_ok!(self.consume_component_name(opening_bracket));
        let modifiers = try_likely_ok!(Modifiers::parse::<VERSION, OWNED>(self));

        let mut nested_format_descriptions = Vec::new();
        while self.is_nested_description_start()
            && let Ok(description) = self.consume_nested(modifiers.end_location())
        {
            nested_format_descriptions.push(description);
        }

        if modifiers.trailing_whitespace.is_some()
            && let Some(first_nested) = nested_format_descriptions.first_mut()
        {
            first_nested.leading_whitespace = modifiers.trailing_whitespace;
        }

        if modifiers.trailing_whitespace.is_none() || !nested_format_descriptions.is_empty() {
            self.consume_whitespace();
        }

        let Some(closing_bracket) = self.consume_closing_bracket() else {
            hint::cold_path();
            return Err(Error {
                _inner: unused(opening_bracket.error("unclosed bracket")),
                public: InvalidFormatDescription::UnclosedOpeningBracket {
                    index: opening_bracket.byte as usize,
                },
            });
        };

        if let Some(first_nested_fd) = nested_format_descriptions.first()
            && first_nested_fd.leading_whitespace.is_none()
        {
            hint::cold_path();
            return Err(Error {
                _inner: unused(
                    opening_bracket
                        .to(closing_bracket)
                        .error("missing leading whitespace before nested format description"),
                ),
                public: InvalidFormatDescription::Expected {
                    what: "whitespace before nested format description",
                    index: first_nested_fd.opening_bracket.byte as usize,
                },
            });
        }

        if ident_eq::<VERSION>(*name, "optional") {
            hint::cold_path();

            let format = try_likely_ok!(parse_optional_format_modifier::<VERSION>(
                &modifiers.modifiers,
            ));

            let nested_format_description = match <[_; 1]>::try_from(nested_format_descriptions) {
                Ok([nested_format_description]) => nested_format_description,
                Err(e) => {
                    hint::cold_path();
                    if let Some((second_fd, last_fd)) = e.first().zip(e.last()) {
                        return Err(Error {
                            _inner: unused(
                                second_fd.opening_bracket.to(last_fd.closing_bracket).error(
                                    "the `optional` component only allows a single nested format \
                                     description",
                                ),
                            ),
                            public: InvalidFormatDescription::NotSupported {
                                what: "more than one nested format description",
                                context: "`optional` components",
                                index: second_fd.opening_bracket.byte as usize,
                            },
                        });
                    } else {
                        return Err(Error {
                            _inner: unused(opening_bracket.to(closing_bracket).error(
                                "missing nested format description for `optional` component",
                            )),
                            public: InvalidFormatDescription::Expected {
                                what: "nested format description",
                                index: closing_bracket.byte as usize,
                            },
                        });
                    }
                }
            };

            return <() as ParseTarget<'input, VERSION, OWNED>>::optional(
                nested_format_description.items,
                *format,
                opening_bracket.to(closing_bracket),
            );
        }

        if ident_eq::<VERSION>(*name, "first") {
            hint::cold_path();
            if !modifiers.modifiers.is_empty() {
                hint::cold_path();
                let modifier = &modifiers.modifiers[0];
                return Err(Error {
                    _inner: unused(modifier.key_span().error("invalid modifier key")),
                    public: InvalidFormatDescription::InvalidModifier {
                        value: (*modifier.key).to_owned(),
                        index: modifier.key.location.byte as usize,
                    },
                });
            }

            if version!(3..) && nested_format_descriptions.is_empty() {
                hint::cold_path();
                return Err(Error {
                    _inner: unused(opening_bracket.to(closing_bracket).error(
                        "the `first` component requires at least one nested format description",
                    )),
                    public: InvalidFormatDescription::Expected {
                        what: "at least one nested format description",
                        index: closing_bracket.byte as usize,
                    },
                });
            }

            let items = nested_format_descriptions
                .into_iter()
                .map(|nested_format_description| nested_format_description.items)
                .collect();

            return <() as ParseTarget<'input, VERSION, OWNED>>::first(
                items,
                opening_bracket.to(closing_bracket),
            );
        }

        if !nested_format_descriptions.is_empty() {
            hint::cold_path();
            return Err(Error {
                _inner: unused(
                    opening_bracket
                        .to(closing_bracket)
                        .error("this component does not support nested format descriptions"),
                ),
                public: InvalidFormatDescription::NotSupported {
                    what: "nested format descriptions",
                    context: "on this component",
                    index: opening_bracket.byte as usize,
                },
            });
        }

        let component = try_likely_ok!(component_from_ast::<VERSION>(&name, &modifiers.modifiers));
        <() as ParseTarget<'input, VERSION, OWNED>>::component(try_likely_ok!(component.try_into()))
    }

    /// Parse a nested format description. The location provided is the most recent one consumed.
    #[inline]
    fn consume_nested(
        &mut self,
        last_location: Location,
    ) -> Result<
        NestedFormatDescription<'input, ParseItemWithLiteralLifetime<'input, VERSION, OWNED>>,
        Error,
    >
    where
        (): ParseTarget<'input, VERSION, OWNED>,
    {
        let leading_whitespace = self.consume_whitespace();

        let opening_bracket = {
            match self.depth.checked_add(1) {
                Some(depth) => self.depth = depth,
                None => {
                    hint::cold_path();
                    return Err(Error {
                        _inner: unused(last_location.error("too much nesting")),
                        public: InvalidFormatDescription::NotSupported {
                            what: "highly-nested format description",
                            context: "",
                            index: last_location.byte as usize,
                        },
                    });
                }
            }
            let location = Location {
                byte: self.byte_pos,
            };
            self.advance(1);
            location
        };

        let mut items = Vec::new();
        while !self.input.is_empty() {
            // If we're in a literal context and the next byte is a closing bracket, stop so that we
            // can consume it.
            if self.context().is_literal() && self.input.first() == Some(&b']') {
                break;
            }

            items.push(try_likely_ok!(self.parse_next_item()));
        }

        let Some(closing_bracket) = self.consume_closing_bracket() else {
            hint::cold_path();
            return Err(Error {
                _inner: unused(opening_bracket.error("unclosed bracket")),
                public: InvalidFormatDescription::UnclosedOpeningBracket {
                    index: opening_bracket.byte as usize,
                },
            });
        };

        Ok(NestedFormatDescription {
            leading_whitespace,
            opening_bracket,
            items,
            closing_bracket,
        })
    }

    #[inline]
    fn modifier_from_token(&self, token: Spanned<&'input str>) -> Result<Modifier<'input>, Error> {
        let Some(colon_index) = token.bytes().position(|b| b == b':') else {
            hint::cold_path();
            return Err(Error {
                _inner: unused(token.span.error("modifier must be of the form `key:value`")),
                public: InvalidFormatDescription::InvalidModifier {
                    value: (*token).to_owned(),
                    index: token.span.start.byte as usize,
                },
            });
        };
        let key = &token[..colon_index];
        let value = &token[colon_index + 1..];

        if key.is_empty() {
            hint::cold_path();
            return Err(Error {
                _inner: unused(token.span.shrink_to_start().error("expected modifier key")),
                public: InvalidFormatDescription::InvalidModifier {
                    value: String::new(),
                    index: token.span.start.byte as usize,
                },
            });
        }
        if value.is_empty() {
            hint::cold_path();
            return Err(Error {
                _inner: unused(token.span.shrink_to_end().error("expected modifier value")),
                public: InvalidFormatDescription::InvalidModifier {
                    value: String::new(),
                    index: token.span.start.byte as usize + colon_index,
                },
            });
        }

        Ok(Modifier {
            key: key.with_location(token.span.start),
            value,
        })
    }

    /// Check whether the next tokens start a nested format description. Does not consume any
    /// input.
    ///
    /// Note that this call is strictly an optimization, as checking the error path on
    /// `parse_nested` is sufficient for knowing if a nested format description is present. This
    /// method avoids the overhead of constructing an error only to throw it away.
    #[inline]
    fn is_nested_description_start(&self) -> bool {
        debug_assert!(self.context().is_component());

        let Some(index) = self
            .input
            .iter()
            .position(|&byte| !byte.is_ascii_whitespace())
        else {
            return false;
        };

        self.input[index] == b'['
            && (version!(2..)
                || self.context().is_component()
                || self.input.get(index + 1) != Some(&b'['))
    }

    #[inline]
    fn consume_literal(&mut self) -> &'input str {
        let bytes = self
            .input
            .iter()
            .take_while(|&&byte| byte != b'[' && byte != b']' && (version!(1) || byte != b'\\'))
            .count() as u32;

        // Safety: A string was passed to this function, and only UTF-8 has been consumed,
        // leaving behind a string known to begin at a character boundary.
        let value = unsafe { str::from_utf8_unchecked(&self.input[..bytes as usize]) };
        self.advance(bytes);

        value
    }

    #[inline]
    fn consume_backslash_escape_sequence(
        &mut self,
        location: Location,
    ) -> Result<&'input str, Error> {
        let backslash_loc = location;

        Ok(match self.input.get(1) {
            Some(b'\\' | b'[' | b']') => {
                // The escaped character is emitted as-is.
                // Safety: We know that this is either a left bracket, right bracket, or
                // backslash.
                let char = unsafe { str::from_utf8_unchecked(&self.input[1..2]) };
                self.advance(2);
                char
            }
            Some(_) => {
                hint::cold_path();
                let loc = Location {
                    byte: self.byte_pos + 1,
                };
                return Err(Error {
                    _inner: unused(loc.error("invalid escape sequence")),
                    public: InvalidFormatDescription::Expected {
                        what: "valid escape sequence",
                        index: loc.byte as usize,
                    },
                });
            }
            None => {
                hint::cold_path();
                return Err(Error {
                    _inner: unused(backslash_loc.error("unexpected end of input")),
                    public: InvalidFormatDescription::Expected {
                        what: "valid escape sequence",
                        index: backslash_loc.byte as usize,
                    },
                });
            }
        })
    }
}

impl<'input, const VERSION: u8, const OWNED: bool> Lexer<'input, VERSION, OWNED> {
    #[inline(always)]
    fn parse_next_item(
        &mut self,
    ) -> Result<ParseItemWithLiteralLifetime<'input, VERSION, OWNED>, Error>
    where
        (): ParseTarget<'input, VERSION, OWNED>,
    {
        let byte = self.input[0];
        let location = Location {
            byte: self.byte_pos,
        };

        Ok(match byte {
            b'[' if version!(1) && self.input.get(1) == Some(&b'[') => {
                self.advance(2);
                <() as ParseTarget<'input, VERSION, OWNED>>::literal("[")
            }
            b'[' => return self.consume_component(location),
            b']' if version!(3..) => {
                hint::cold_path();
                return Err(Error {
                    _inner: unused(location.error("right brackets must be escaped")),
                    public: InvalidFormatDescription::Expected {
                        what: "right bracket to be escaped",
                        index: location.byte as usize,
                    },
                });
            }
            b']' if version!(1..=2) => {
                self.advance(1);
                <() as ParseTarget<'input, VERSION, OWNED>>::literal("]")
            }
            b'\\' if version!(2..) => {
                return self
                    .consume_backslash_escape_sequence(location)
                    .map(<() as ParseTarget<'input, VERSION, OWNED>>::literal);
            }
            _ => <() as ParseTarget<'input, VERSION, OWNED>>::literal(self.consume_literal()),
        })
    }
}

/// A format description that is nested within another format description.
pub(super) struct NestedFormatDescription<'a, Item> {
    /// Whitespace between the end of the previous item and the opening bracket.
    pub(super) leading_whitespace: Option<Spanned<&'a str>>,
    /// Where the opening bracket was in the format string.
    pub(super) opening_bracket: Location,
    /// The items within the nested format description.
    pub(super) items: Vec<Item>,
    /// Where the closing bracket was in the format string.
    pub(super) closing_bracket: Location,
}

/// A modifier for a component.
pub(super) struct Modifier<'a> {
    /// The key of the modifier.
    pub(super) key: WithLocation<&'a str>,
    /// The value of the modifier.
    pub(super) value: &'a str,
}

impl Modifier<'_> {
    #[inline]
    pub(super) fn key_value_span(&self) -> Span {
        self.key
            .location
            .with_length(self.key.len() + self.value.len() + 1)
    }

    #[inline]
    pub(super) fn key_span(&self) -> Span {
        self.key.location.with_length(self.key.len())
    }

    #[inline]
    pub(super) fn value_span(&self) -> Span {
        self.key
            .location
            .offset(self.key.len() as u32 + 1)
            .with_length(self.value.len())
    }
}

pub(super) struct Modifiers<'a> {
    pub(super) modifiers: Vec<Modifier<'a>>,
    pub(super) trailing_whitespace: Option<Spanned<&'a str>>,
}

impl<'a> Modifiers<'a> {
    /// Parse modifiers until there are none left. Returns the modifiers along with any trailing
    /// whitespace after the last modifier.
    #[inline]
    pub(super) fn parse<const VERSION: u8, const OWNED: bool>(
        tokens: &mut Lexer<'a, VERSION, OWNED>,
    ) -> Result<Self, Error> {
        let mut modifiers = Vec::new();
        loop {
            match try_likely_ok!(tokens.consume_modifier()) {
                NextModifier::Modifier(modifier) => modifiers.push(modifier),
                NextModifier::TrailingWhitespace(whitespace) => {
                    return Ok(Self {
                        modifiers,
                        trailing_whitespace: Some(whitespace),
                    });
                }
                NextModifier::None => {
                    return Ok(Self {
                        modifiers,
                        trailing_whitespace: None,
                    });
                }
            }
        }
    }

    #[inline]
    pub(super) fn end_location(&self) -> Location {
        match &*self.modifiers {
            [] => Location::DUMMY,
            [.., modifier] => modifier.value_span().end,
        }
    }
}