summaryrefslogtreecommitdiffstats
path: root/third_party/rust/time-macros/src/format_description
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/time-macros/src/format_description
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/time-macros/src/format_description')
-rw-r--r--third_party/rust/time-macros/src/format_description/ast.rs253
-rw-r--r--third_party/rust/time-macros/src/format_description/format_item.rs442
-rw-r--r--third_party/rust/time-macros/src/format_description/lexer.rs248
-rw-r--r--third_party/rust/time-macros/src/format_description/mod.rs171
-rw-r--r--third_party/rust/time-macros/src/format_description/public/component.rs49
-rw-r--r--third_party/rust/time-macros/src/format_description/public/mod.rs54
-rw-r--r--third_party/rust/time-macros/src/format_description/public/modifier.rs247
7 files changed, 1464 insertions, 0 deletions
diff --git a/third_party/rust/time-macros/src/format_description/ast.rs b/third_party/rust/time-macros/src/format_description/ast.rs
new file mode 100644
index 0000000000..b75056bc2f
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/ast.rs
@@ -0,0 +1,253 @@
+use std::boxed::Box;
+use std::iter;
+
+use super::{lexer, unused, Error, Location, Spanned, SpannedValue, Unused};
+
+pub(super) enum Item<'a> {
+ Literal(Spanned<&'a [u8]>),
+ EscapedBracket {
+ _first: Unused<Location>,
+ _second: Unused<Location>,
+ },
+ Component {
+ _opening_bracket: Unused<Location>,
+ _leading_whitespace: Unused<Option<Spanned<&'a [u8]>>>,
+ name: Spanned<&'a [u8]>,
+ modifiers: Box<[Modifier<'a>]>,
+ _trailing_whitespace: Unused<Option<Spanned<&'a [u8]>>>,
+ _closing_bracket: Unused<Location>,
+ },
+ Optional {
+ opening_bracket: Location,
+ _leading_whitespace: Unused<Option<Spanned<&'a [u8]>>>,
+ _optional_kw: Unused<Spanned<&'a [u8]>>,
+ _whitespace: Unused<Spanned<&'a [u8]>>,
+ nested_format_description: NestedFormatDescription<'a>,
+ closing_bracket: Location,
+ },
+ First {
+ opening_bracket: Location,
+ _leading_whitespace: Unused<Option<Spanned<&'a [u8]>>>,
+ _first_kw: Unused<Spanned<&'a [u8]>>,
+ _whitespace: Unused<Spanned<&'a [u8]>>,
+ nested_format_descriptions: Box<[NestedFormatDescription<'a>]>,
+ closing_bracket: Location,
+ },
+}
+
+pub(super) struct NestedFormatDescription<'a> {
+ pub(super) _opening_bracket: Unused<Location>,
+ pub(super) items: Box<[Item<'a>]>,
+ pub(super) _closing_bracket: Unused<Location>,
+ pub(super) _trailing_whitespace: Unused<Option<Spanned<&'a [u8]>>>,
+}
+
+pub(super) struct Modifier<'a> {
+ pub(super) _leading_whitespace: Unused<Spanned<&'a [u8]>>,
+ pub(super) key: Spanned<&'a [u8]>,
+ pub(super) _colon: Unused<Location>,
+ pub(super) value: Spanned<&'a [u8]>,
+}
+
+pub(super) fn parse<
+ 'item: 'iter,
+ 'iter,
+ I: Iterator<Item = Result<lexer::Token<'item>, Error>>,
+ const VERSION: u8,
+>(
+ tokens: &'iter mut lexer::Lexed<I>,
+) -> impl Iterator<Item = Result<Item<'item>, Error>> + 'iter {
+ assert!(version!(1..=2));
+ parse_inner::<_, false, VERSION>(tokens)
+}
+
+fn parse_inner<
+ 'item,
+ I: Iterator<Item = Result<lexer::Token<'item>, Error>>,
+ const NESTED: bool,
+ const VERSION: u8,
+>(
+ tokens: &mut lexer::Lexed<I>,
+) -> impl Iterator<Item = Result<Item<'item>, Error>> + '_ {
+ iter::from_fn(move || {
+ if NESTED && tokens.peek_closing_bracket().is_some() {
+ return None;
+ }
+
+ let next = match tokens.next()? {
+ Ok(token) => token,
+ Err(err) => return Some(Err(err)),
+ };
+
+ Some(match next {
+ lexer::Token::Literal(Spanned { value: _, span: _ }) if NESTED => {
+ bug!("literal should not be present in nested description")
+ }
+ lexer::Token::Literal(value) => Ok(Item::Literal(value)),
+ lexer::Token::Bracket {
+ kind: lexer::BracketKind::Opening,
+ location,
+ } => {
+ if version!(..=1) {
+ if let Some(second_location) = tokens.next_if_opening_bracket() {
+ Ok(Item::EscapedBracket {
+ _first: unused(location),
+ _second: unused(second_location),
+ })
+ } else {
+ parse_component::<_, VERSION>(location, tokens)
+ }
+ } else {
+ parse_component::<_, VERSION>(location, tokens)
+ }
+ }
+ lexer::Token::Bracket {
+ kind: lexer::BracketKind::Closing,
+ location: _,
+ } if NESTED => {
+ bug!("closing bracket should be caught by the `if` statement")
+ }
+ lexer::Token::Bracket {
+ kind: lexer::BracketKind::Closing,
+ location: _,
+ } => {
+ bug!("closing bracket should have been consumed by `parse_component`")
+ }
+ lexer::Token::ComponentPart { kind: _, value } if NESTED => Ok(Item::Literal(value)),
+ lexer::Token::ComponentPart { kind: _, value: _ } => {
+ bug!("component part should have been consumed by `parse_component`")
+ }
+ })
+ })
+}
+
+fn parse_component<'a, I: Iterator<Item = Result<lexer::Token<'a>, Error>>, const VERSION: u8>(
+ opening_bracket: Location,
+ tokens: &mut lexer::Lexed<I>,
+) -> Result<Item<'a>, Error> {
+ let leading_whitespace = tokens.next_if_whitespace();
+
+ let Some(name) = tokens.next_if_not_whitespace() else {
+ let span = match leading_whitespace {
+ Some(Spanned { value: _, span }) => span,
+ None => opening_bracket.to(opening_bracket),
+ };
+ return Err(span.error("expected component name"));
+ };
+
+ if *name == b"optional" {
+ let Some(whitespace) = tokens.next_if_whitespace() else {
+ return Err(name.span.error("expected whitespace after `optional`"));
+ };
+
+ let nested = parse_nested::<_, VERSION>(whitespace.span.end, tokens)?;
+
+ let Some(closing_bracket) = tokens.next_if_closing_bracket() else {
+ return Err(opening_bracket.error("unclosed bracket"));
+ };
+
+ return Ok(Item::Optional {
+ opening_bracket,
+ _leading_whitespace: unused(leading_whitespace),
+ _optional_kw: unused(name),
+ _whitespace: unused(whitespace),
+ nested_format_description: nested,
+ closing_bracket,
+ });
+ }
+
+ if *name == b"first" {
+ let Some(whitespace) = tokens.next_if_whitespace() else {
+ return Err(name.span.error("expected whitespace after `first`"));
+ };
+
+ let mut nested_format_descriptions = Vec::new();
+ while let Ok(description) = parse_nested::<_, VERSION>(whitespace.span.end, tokens) {
+ nested_format_descriptions.push(description);
+ }
+
+ let Some(closing_bracket) = tokens.next_if_closing_bracket() else {
+ return Err(opening_bracket.error("unclosed bracket"));
+ };
+
+ return Ok(Item::First {
+ opening_bracket,
+ _leading_whitespace: unused(leading_whitespace),
+ _first_kw: unused(name),
+ _whitespace: unused(whitespace),
+ nested_format_descriptions: nested_format_descriptions.into_boxed_slice(),
+ closing_bracket,
+ });
+ }
+
+ let mut modifiers = Vec::new();
+ let trailing_whitespace = loop {
+ let Some(whitespace) = tokens.next_if_whitespace() else {
+ break None;
+ };
+
+ if let Some(location) = tokens.next_if_opening_bracket() {
+ return Err(location
+ .to(location)
+ .error("modifier must be of the form `key:value`"));
+ }
+
+ let Some(Spanned { value, span }) = tokens.next_if_not_whitespace() else {
+ break Some(whitespace);
+ };
+
+ let Some(colon_index) = value.iter().position(|&b| b == b':') else {
+ return Err(span.error("modifier must be of the form `key:value`"));
+ };
+ let key = &value[..colon_index];
+ let value = &value[colon_index + 1..];
+
+ if key.is_empty() {
+ return Err(span.shrink_to_start().error("expected modifier key"));
+ }
+ if value.is_empty() {
+ return Err(span.shrink_to_end().error("expected modifier value"));
+ }
+
+ modifiers.push(Modifier {
+ _leading_whitespace: unused(whitespace),
+ key: key.spanned(span.shrink_to_before(colon_index as _)),
+ _colon: unused(span.start.offset(colon_index as _)),
+ value: value.spanned(span.shrink_to_after(colon_index as _)),
+ });
+ };
+
+ let Some(closing_bracket) = tokens.next_if_closing_bracket() else {
+ return Err(opening_bracket.error("unclosed bracket"));
+ };
+
+ Ok(Item::Component {
+ _opening_bracket: unused(opening_bracket),
+ _leading_whitespace: unused(leading_whitespace),
+ name,
+ modifiers: modifiers.into_boxed_slice(),
+ _trailing_whitespace: unused(trailing_whitespace),
+ _closing_bracket: unused(closing_bracket),
+ })
+}
+
+fn parse_nested<'a, I: Iterator<Item = Result<lexer::Token<'a>, Error>>, const VERSION: u8>(
+ last_location: Location,
+ tokens: &mut lexer::Lexed<I>,
+) -> Result<NestedFormatDescription<'a>, Error> {
+ let Some(opening_bracket) = tokens.next_if_opening_bracket() else {
+ return Err(last_location.error("expected opening bracket"));
+ };
+ let items = parse_inner::<_, true, VERSION>(tokens).collect::<Result<_, _>>()?;
+ let Some(closing_bracket) = tokens.next_if_closing_bracket() else {
+ return Err(opening_bracket.error("unclosed bracket"));
+ };
+ let trailing_whitespace = tokens.next_if_whitespace();
+
+ Ok(NestedFormatDescription {
+ _opening_bracket: unused(opening_bracket),
+ items,
+ _closing_bracket: unused(closing_bracket),
+ _trailing_whitespace: unused(trailing_whitespace),
+ })
+}
diff --git a/third_party/rust/time-macros/src/format_description/format_item.rs b/third_party/rust/time-macros/src/format_description/format_item.rs
new file mode 100644
index 0000000000..6a8cf555ee
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/format_item.rs
@@ -0,0 +1,442 @@
+use std::boxed::Box;
+use std::num::NonZeroU16;
+use std::str::{self, FromStr};
+
+use super::{ast, unused, Error, Span, Spanned, Unused};
+
+pub(super) fn parse<'a>(
+ ast_items: impl Iterator<Item = Result<ast::Item<'a>, Error>>,
+) -> impl Iterator<Item = Result<Item<'a>, Error>> {
+ ast_items.map(|ast_item| ast_item.and_then(Item::from_ast))
+}
+
+pub(super) enum Item<'a> {
+ Literal(&'a [u8]),
+ Component(Component),
+ Optional {
+ value: Box<[Self]>,
+ _span: Unused<Span>,
+ },
+ First {
+ value: Box<[Box<[Self]>]>,
+ _span: Unused<Span>,
+ },
+}
+
+impl Item<'_> {
+ pub(super) fn from_ast(ast_item: ast::Item<'_>) -> Result<Item<'_>, Error> {
+ Ok(match ast_item {
+ ast::Item::Component {
+ _opening_bracket: _,
+ _leading_whitespace: _,
+ name,
+ modifiers,
+ _trailing_whitespace: _,
+ _closing_bracket: _,
+ } => Item::Component(component_from_ast(&name, &modifiers)?),
+ ast::Item::Literal(Spanned { value, span: _ }) => Item::Literal(value),
+ ast::Item::EscapedBracket {
+ _first: _,
+ _second: _,
+ } => Item::Literal(b"["),
+ ast::Item::Optional {
+ opening_bracket,
+ _leading_whitespace: _,
+ _optional_kw: _,
+ _whitespace: _,
+ nested_format_description,
+ closing_bracket,
+ } => {
+ let items = nested_format_description
+ .items
+ .into_vec()
+ .into_iter()
+ .map(Item::from_ast)
+ .collect::<Result<_, _>>()?;
+ Item::Optional {
+ value: items,
+ _span: unused(opening_bracket.to(closing_bracket)),
+ }
+ }
+ ast::Item::First {
+ opening_bracket,
+ _leading_whitespace: _,
+ _first_kw: _,
+ _whitespace: _,
+ nested_format_descriptions,
+ closing_bracket,
+ } => {
+ let items = nested_format_descriptions
+ .into_vec()
+ .into_iter()
+ .map(|nested_format_description| {
+ nested_format_description
+ .items
+ .into_vec()
+ .into_iter()
+ .map(Item::from_ast)
+ .collect()
+ })
+ .collect::<Result<_, _>>()?;
+ Item::First {
+ value: items,
+ _span: unused(opening_bracket.to(closing_bracket)),
+ }
+ }
+ })
+ }
+}
+
+impl From<Item<'_>> for crate::format_description::public::OwnedFormatItem {
+ fn from(item: Item<'_>) -> Self {
+ match item {
+ Item::Literal(literal) => Self::Literal(literal.to_vec().into_boxed_slice()),
+ Item::Component(component) => Self::Component(component.into()),
+ Item::Optional { value, _span: _ } => Self::Optional(Box::new(value.into())),
+ Item::First { value, _span: _ } => {
+ Self::First(value.into_vec().into_iter().map(Into::into).collect())
+ }
+ }
+ }
+}
+
+impl<'a> From<Box<[Item<'a>]>> for crate::format_description::public::OwnedFormatItem {
+ fn from(items: Box<[Item<'a>]>) -> Self {
+ let items = items.into_vec();
+ if items.len() == 1 {
+ if let Ok([item]) = <[_; 1]>::try_from(items) {
+ item.into()
+ } else {
+ bug!("the length was just checked to be 1")
+ }
+ } else {
+ Self::Compound(items.into_iter().map(Self::from).collect())
+ }
+ }
+}
+
+macro_rules! component_definition {
+ (@if_required required then { $($then:tt)* } $(else { $($else:tt)* })?) => { $($then)* };
+ (@if_required then { $($then:tt)* } $(else { $($else:tt)* })?) => { $($($else)*)? };
+ (@if_from_str from_str then { $($then:tt)* } $(else { $($else:tt)* })?) => { $($then)* };
+ (@if_from_str then { $($then:tt)* } $(else { $($else:tt)* })?) => { $($($else)*)? };
+
+ ($vis:vis enum $name:ident {
+ $($variant:ident = $parse_variant:literal {$(
+ $(#[$required:tt])?
+ $field:ident = $parse_field:literal:
+ Option<$(#[$from_str:tt])? $field_type:ty>
+ => $target_field:ident
+ ),* $(,)?}),* $(,)?
+ }) => {
+ $vis enum $name {
+ $($variant($variant),)*
+ }
+
+ $($vis struct $variant {
+ $($field: Option<$field_type>),*
+ })*
+
+ $(impl $variant {
+ fn with_modifiers(
+ modifiers: &[ast::Modifier<'_>],
+ _component_span: Span,
+ ) -> Result<Self, Error>
+ {
+ let mut this = Self {
+ $($field: None),*
+ };
+
+ for modifier in modifiers {
+ $(#[allow(clippy::string_lit_as_bytes)]
+ if modifier.key.eq_ignore_ascii_case($parse_field.as_bytes()) {
+ this.$field = component_definition!(@if_from_str $($from_str)?
+ then {
+ parse_from_modifier_value::<$field_type>(&modifier.value)?
+ } else {
+ <$field_type>::from_modifier_value(&modifier.value)?
+ });
+ continue;
+ })*
+ return Err(modifier.key.span.error("invalid modifier key"));
+ }
+
+ $(component_definition! { @if_required $($required)? then {
+ if this.$field.is_none() {
+ return Err(_component_span.error("missing required modifier"));
+ }
+ }})*
+
+ Ok(this)
+ }
+ })*
+
+ impl From<$name> for crate::format_description::public::Component {
+ fn from(component: $name) -> Self {
+ match component {$(
+ $name::$variant($variant { $($field),* }) => {
+ $crate::format_description::public::Component::$variant(
+ super::public::modifier::$variant {$(
+ $target_field: component_definition! { @if_required $($required)?
+ then {
+ match $field {
+ Some(value) => value.into(),
+ None => bug!("required modifier was not set"),
+ }
+ } else {
+ $field.unwrap_or_default().into()
+ }
+ }
+ ),*}
+ )
+ }
+ )*}
+ }
+ }
+
+ fn component_from_ast(
+ name: &Spanned<&[u8]>,
+ modifiers: &[ast::Modifier<'_>],
+ ) -> Result<Component, Error> {
+ $(#[allow(clippy::string_lit_as_bytes)]
+ if name.eq_ignore_ascii_case($parse_variant.as_bytes()) {
+ return Ok(Component::$variant($variant::with_modifiers(&modifiers, name.span)?));
+ })*
+ Err(name.span.error("invalid component"))
+ }
+ }
+}
+
+component_definition! {
+ pub(super) enum Component {
+ Day = "day" {
+ padding = "padding": Option<Padding> => padding,
+ },
+ Hour = "hour" {
+ padding = "padding": Option<Padding> => padding,
+ base = "repr": Option<HourBase> => is_12_hour_clock,
+ },
+ Ignore = "ignore" {
+ #[required]
+ count = "count": Option<#[from_str] NonZeroU16> => count,
+ },
+ Minute = "minute" {
+ padding = "padding": Option<Padding> => padding,
+ },
+ Month = "month" {
+ padding = "padding": Option<Padding> => padding,
+ repr = "repr": Option<MonthRepr> => repr,
+ case_sensitive = "case_sensitive": Option<MonthCaseSensitive> => case_sensitive,
+ },
+ OffsetHour = "offset_hour" {
+ sign_behavior = "sign": Option<SignBehavior> => sign_is_mandatory,
+ padding = "padding": Option<Padding> => padding,
+ },
+ OffsetMinute = "offset_minute" {
+ padding = "padding": Option<Padding> => padding,
+ },
+ OffsetSecond = "offset_second" {
+ padding = "padding": Option<Padding> => padding,
+ },
+ Ordinal = "ordinal" {
+ padding = "padding": Option<Padding> => padding,
+ },
+ Period = "period" {
+ case = "case": Option<PeriodCase> => is_uppercase,
+ case_sensitive = "case_sensitive": Option<PeriodCaseSensitive> => case_sensitive,
+ },
+ Second = "second" {
+ padding = "padding": Option<Padding> => padding,
+ },
+ Subsecond = "subsecond" {
+ digits = "digits": Option<SubsecondDigits> => digits,
+ },
+ UnixTimestamp = "unix_timestamp" {
+ precision = "precision": Option<UnixTimestampPrecision> => precision,
+ sign_behavior = "sign": Option<SignBehavior> => sign_is_mandatory,
+ },
+ Weekday = "weekday" {
+ repr = "repr": Option<WeekdayRepr> => repr,
+ one_indexed = "one_indexed": Option<WeekdayOneIndexed> => one_indexed,
+ case_sensitive = "case_sensitive": Option<WeekdayCaseSensitive> => case_sensitive,
+ },
+ WeekNumber = "week_number" {
+ padding = "padding": Option<Padding> => padding,
+ repr = "repr": Option<WeekNumberRepr> => repr,
+ },
+ Year = "year" {
+ padding = "padding": Option<Padding> => padding,
+ repr = "repr": Option<YearRepr> => repr,
+ base = "base": Option<YearBase> => iso_week_based,
+ sign_behavior = "sign": Option<SignBehavior> => sign_is_mandatory,
+ },
+ }
+}
+
+macro_rules! target_ty {
+ ($name:ident $type:ty) => {
+ $type
+ };
+ ($name:ident) => {
+ super::public::modifier::$name
+ };
+}
+
+/// Get the target value for a given enum.
+macro_rules! target_value {
+ ($name:ident $variant:ident $value:expr) => {
+ $value
+ };
+ ($name:ident $variant:ident) => {
+ super::public::modifier::$name::$variant
+ };
+}
+
+macro_rules! modifier {
+ ($(
+ enum $name:ident $(($target_ty:ty))? {
+ $(
+ $(#[$attr:meta])?
+ $variant:ident $(($target_value:expr))? = $parse_variant:literal
+ ),* $(,)?
+ }
+ )+) => {$(
+ #[derive(Default)]
+ enum $name {
+ $($(#[$attr])? $variant),*
+ }
+
+ impl $name {
+ /// Parse the modifier from its string representation.
+ fn from_modifier_value(value: &Spanned<&[u8]>) -> Result<Option<Self>, Error> {
+ $(if value.eq_ignore_ascii_case($parse_variant) {
+ return Ok(Some(Self::$variant));
+ })*
+ Err(value.span.error("invalid modifier value"))
+ }
+ }
+
+ impl From<$name> for target_ty!($name $($target_ty)?) {
+ fn from(modifier: $name) -> Self {
+ match modifier {
+ $($name::$variant => target_value!($name $variant $($target_value)?)),*
+ }
+ }
+ }
+ )+};
+}
+
+modifier! {
+ enum HourBase(bool) {
+ Twelve(true) = b"12",
+ #[default]
+ TwentyFour(false) = b"24",
+ }
+
+ enum MonthCaseSensitive(bool) {
+ False(false) = b"false",
+ #[default]
+ True(true) = b"true",
+ }
+
+ enum MonthRepr {
+ #[default]
+ Numerical = b"numerical",
+ Long = b"long",
+ Short = b"short",
+ }
+
+ enum Padding {
+ Space = b"space",
+ #[default]
+ Zero = b"zero",
+ None = b"none",
+ }
+
+ enum PeriodCase(bool) {
+ Lower(false) = b"lower",
+ #[default]
+ Upper(true) = b"upper",
+ }
+
+ enum PeriodCaseSensitive(bool) {
+ False(false) = b"false",
+ #[default]
+ True(true) = b"true",
+ }
+
+ enum SignBehavior(bool) {
+ #[default]
+ Automatic(false) = b"automatic",
+ Mandatory(true) = b"mandatory",
+ }
+
+ enum SubsecondDigits {
+ One = b"1",
+ Two = b"2",
+ Three = b"3",
+ Four = b"4",
+ Five = b"5",
+ Six = b"6",
+ Seven = b"7",
+ Eight = b"8",
+ Nine = b"9",
+ #[default]
+ OneOrMore = b"1+",
+ }
+
+ enum UnixTimestampPrecision {
+ #[default]
+ Second = b"second",
+ Millisecond = b"millisecond",
+ Microsecond = b"microsecond",
+ Nanosecond = b"nanosecond",
+ }
+
+ enum WeekNumberRepr {
+ #[default]
+ Iso = b"iso",
+ Sunday = b"sunday",
+ Monday = b"monday",
+ }
+
+ enum WeekdayCaseSensitive(bool) {
+ False(false) = b"false",
+ #[default]
+ True(true) = b"true",
+ }
+
+ enum WeekdayOneIndexed(bool) {
+ False(false) = b"false",
+ #[default]
+ True(true) = b"true",
+ }
+
+ enum WeekdayRepr {
+ Short = b"short",
+ #[default]
+ Long = b"long",
+ Sunday = b"sunday",
+ Monday = b"monday",
+ }
+
+ enum YearBase(bool) {
+ #[default]
+ Calendar(false) = b"calendar",
+ IsoWeek(true) = b"iso_week",
+ }
+
+ enum YearRepr {
+ #[default]
+ Full = b"full",
+ LastTwo = b"last_two",
+ }
+}
+
+fn parse_from_modifier_value<T: FromStr>(value: &Spanned<&[u8]>) -> Result<Option<T>, Error> {
+ str::from_utf8(value)
+ .ok()
+ .and_then(|val| val.parse::<T>().ok())
+ .map(|val| Some(val))
+ .ok_or_else(|| value.span.error("invalid modifier value"))
+}
diff --git a/third_party/rust/time-macros/src/format_description/lexer.rs b/third_party/rust/time-macros/src/format_description/lexer.rs
new file mode 100644
index 0000000000..2c927cb94d
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/lexer.rs
@@ -0,0 +1,248 @@
+use core::iter;
+
+use super::{Error, Location, Spanned, SpannedValue};
+
+pub(super) struct Lexed<I: Iterator> {
+ iter: core::iter::Peekable<I>,
+}
+
+impl<I: Iterator> Iterator for Lexed<I> {
+ type Item = I::Item;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ self.iter.next()
+ }
+}
+
+impl<'iter, 'token: 'iter, I: Iterator<Item = Result<Token<'token>, Error>> + 'iter> Lexed<I> {
+ pub(super) fn peek(&mut self) -> Option<&I::Item> {
+ self.iter.peek()
+ }
+
+ pub(super) fn next_if_whitespace(&mut self) -> Option<Spanned<&'token [u8]>> {
+ if let Some(&Ok(Token::ComponentPart {
+ kind: ComponentKind::Whitespace,
+ value,
+ })) = self.peek()
+ {
+ self.next(); // consume
+ Some(value)
+ } else {
+ None
+ }
+ }
+
+ pub(super) fn next_if_not_whitespace(&mut self) -> Option<Spanned<&'token [u8]>> {
+ if let Some(&Ok(Token::ComponentPart {
+ kind: ComponentKind::NotWhitespace,
+ value,
+ })) = self.peek()
+ {
+ self.next();
+ Some(value)
+ } else {
+ None
+ }
+ }
+
+ pub(super) fn next_if_opening_bracket(&mut self) -> Option<Location> {
+ if let Some(&Ok(Token::Bracket {
+ kind: BracketKind::Opening,
+ location,
+ })) = self.peek()
+ {
+ self.next();
+ Some(location)
+ } else {
+ None
+ }
+ }
+
+ pub(super) fn peek_closing_bracket(&'iter mut self) -> Option<&'iter Location> {
+ if let Some(Ok(Token::Bracket {
+ kind: BracketKind::Closing,
+ location,
+ })) = self.peek()
+ {
+ Some(location)
+ } else {
+ None
+ }
+ }
+
+ pub(super) fn next_if_closing_bracket(&mut self) -> Option<Location> {
+ if let Some(&Ok(Token::Bracket {
+ kind: BracketKind::Closing,
+ location,
+ })) = self.peek()
+ {
+ self.next();
+ Some(location)
+ } else {
+ None
+ }
+ }
+}
+
+pub(super) enum Token<'a> {
+ Literal(Spanned<&'a [u8]>),
+ Bracket {
+ kind: BracketKind,
+ location: Location,
+ },
+ ComponentPart {
+ kind: ComponentKind,
+ value: Spanned<&'a [u8]>,
+ },
+}
+
+pub(super) enum BracketKind {
+ Opening,
+ Closing,
+}
+
+pub(super) enum ComponentKind {
+ #[allow(clippy::missing_docs_in_private_items)]
+ Whitespace,
+ #[allow(clippy::missing_docs_in_private_items)]
+ NotWhitespace,
+}
+
+fn attach_location<'item>(
+ iter: impl Iterator<Item = &'item u8>,
+ proc_span: proc_macro::Span,
+) -> impl Iterator<Item = (&'item u8, Location)> {
+ let mut byte_pos = 0;
+
+ iter.map(move |byte| {
+ let location = Location {
+ byte: byte_pos,
+ proc_span,
+ };
+ byte_pos += 1;
+ (byte, location)
+ })
+}
+
+#[allow(clippy::unused_peekable)] // false positive
+pub(super) fn lex<const VERSION: u8>(
+ mut input: &[u8],
+ proc_span: proc_macro::Span,
+) -> Lexed<impl Iterator<Item = Result<Token<'_>, Error>>> {
+ assert!(version!(1..=2));
+
+ let mut depth: u8 = 0;
+ let mut iter = attach_location(input.iter(), proc_span).peekable();
+ let mut second_bracket_location = None;
+
+ let iter = iter::from_fn(move || {
+ if version!(..=1) {
+ if let Some(location) = second_bracket_location.take() {
+ return Some(Ok(Token::Bracket {
+ kind: BracketKind::Opening,
+ location,
+ }));
+ }
+ }
+
+ Some(Ok(match iter.next()? {
+ (b'\\', backslash_loc) if version!(2..) => match iter.next() {
+ Some((b'\\' | b'[' | b']', char_loc)) => {
+ let char = &input[1..2];
+ input = &input[2..];
+ if depth == 0 {
+ Token::Literal(char.spanned(backslash_loc.to(char_loc)))
+ } else {
+ Token::ComponentPart {
+ kind: ComponentKind::NotWhitespace,
+ value: char.spanned(backslash_loc.to(char_loc)),
+ }
+ }
+ }
+ Some((_, loc)) => {
+ return Some(Err(loc.error("invalid escape sequence")));
+ }
+ None => {
+ return Some(Err(backslash_loc.error("unexpected end of input")));
+ }
+ },
+ (b'[', location) if version!(..=1) => {
+ if let Some((_, second_location)) = iter.next_if(|&(&byte, _)| byte == b'[') {
+ second_bracket_location = Some(second_location);
+ input = &input[2..];
+ } else {
+ depth += 1;
+ input = &input[1..];
+ }
+
+ Token::Bracket {
+ kind: BracketKind::Opening,
+ location,
+ }
+ }
+ (b'[', location) => {
+ depth += 1;
+ input = &input[1..];
+
+ Token::Bracket {
+ kind: BracketKind::Opening,
+ location,
+ }
+ }
+ (b']', location) if depth > 0 => {
+ depth -= 1;
+ input = &input[1..];
+
+ Token::Bracket {
+ kind: BracketKind::Closing,
+ location,
+ }
+ }
+ (_, start_location) if depth == 0 => {
+ let mut bytes = 1;
+ let mut end_location = start_location;
+
+ while let Some((_, location)) =
+ iter.next_if(|&(&byte, _)| !((version!(2..) && byte == b'\\') || byte == b'['))
+ {
+ end_location = location;
+ bytes += 1;
+ }
+
+ let value = &input[..bytes];
+ input = &input[bytes..];
+
+ Token::Literal(value.spanned(start_location.to(end_location)))
+ }
+ (byte, start_location) => {
+ let mut bytes = 1;
+ let mut end_location = start_location;
+ let is_whitespace = byte.is_ascii_whitespace();
+
+ while let Some((_, location)) = iter.next_if(|&(byte, _)| {
+ !matches!(byte, b'\\' | b'[' | b']')
+ && is_whitespace == byte.is_ascii_whitespace()
+ }) {
+ end_location = location;
+ bytes += 1;
+ }
+
+ let value = &input[..bytes];
+ input = &input[bytes..];
+
+ Token::ComponentPart {
+ kind: if is_whitespace {
+ ComponentKind::Whitespace
+ } else {
+ ComponentKind::NotWhitespace
+ },
+ value: value.spanned(start_location.to(end_location)),
+ }
+ }
+ }))
+ });
+
+ Lexed {
+ iter: iter.peekable(),
+ }
+}
diff --git a/third_party/rust/time-macros/src/format_description/mod.rs b/third_party/rust/time-macros/src/format_description/mod.rs
new file mode 100644
index 0000000000..fde1272f6a
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/mod.rs
@@ -0,0 +1,171 @@
+//! Parser for format descriptions.
+
+use std::vec::Vec;
+
+macro_rules! version {
+ ($range:expr) => {
+ $range.contains(&VERSION)
+ };
+}
+
+mod ast;
+mod format_item;
+mod lexer;
+mod public;
+
+pub(crate) fn parse_with_version(
+ version: Option<crate::FormatDescriptionVersion>,
+ s: &[u8],
+ proc_span: proc_macro::Span,
+) -> Result<Vec<crate::format_description::public::OwnedFormatItem>, crate::Error> {
+ match version {
+ Some(crate::FormatDescriptionVersion::V1) | None => parse::<1>(s, proc_span),
+ Some(crate::FormatDescriptionVersion::V2) => parse::<2>(s, proc_span),
+ }
+}
+
+fn parse<const VERSION: u8>(
+ s: &[u8],
+ proc_span: proc_macro::Span,
+) -> Result<Vec<crate::format_description::public::OwnedFormatItem>, crate::Error> {
+ let mut lexed = lexer::lex::<VERSION>(s, proc_span);
+ let ast = ast::parse::<_, VERSION>(&mut lexed);
+ let format_items = format_item::parse(ast);
+ Ok(format_items
+ .map(|res| res.map(Into::into))
+ .collect::<Result<_, _>>()?)
+}
+
+#[derive(Clone, Copy)]
+struct Location {
+ byte: u32,
+ proc_span: proc_macro::Span,
+}
+
+impl Location {
+ fn to(self, end: Self) -> Span {
+ Span { start: self, end }
+ }
+
+ #[must_use = "this does not modify the original value"]
+ fn offset(&self, offset: u32) -> Self {
+ Self {
+ byte: self.byte + offset,
+ proc_span: self.proc_span,
+ }
+ }
+
+ fn error(self, message: &'static str) -> Error {
+ Error {
+ message,
+ _span: unused(Span {
+ start: self,
+ end: self,
+ }),
+ proc_span: self.proc_span,
+ }
+ }
+}
+
+#[derive(Clone, Copy)]
+struct Span {
+ #[allow(clippy::missing_docs_in_private_items)]
+ start: Location,
+ #[allow(clippy::missing_docs_in_private_items)]
+ end: Location,
+}
+
+impl Span {
+ #[must_use = "this does not modify the original value"]
+ const fn shrink_to_start(&self) -> Self {
+ Self {
+ start: self.start,
+ end: self.start,
+ }
+ }
+
+ #[must_use = "this does not modify the original value"]
+ const fn shrink_to_end(&self) -> Self {
+ Self {
+ start: self.end,
+ end: self.end,
+ }
+ }
+
+ #[must_use = "this does not modify the original value"]
+ const fn shrink_to_before(&self, pos: u32) -> Self {
+ Self {
+ start: self.start,
+ end: Location {
+ byte: self.start.byte + pos - 1,
+ proc_span: self.start.proc_span,
+ },
+ }
+ }
+
+ #[must_use = "this does not modify the original value"]
+ fn shrink_to_after(&self, pos: u32) -> Self {
+ Self {
+ start: Location {
+ byte: self.start.byte + pos + 1,
+ proc_span: self.start.proc_span,
+ },
+ end: self.end,
+ }
+ }
+
+ fn error(self, message: &'static str) -> Error {
+ Error {
+ message,
+ _span: unused(self),
+ proc_span: self.start.proc_span,
+ }
+ }
+}
+
+#[derive(Clone, Copy)]
+struct Spanned<T> {
+ value: T,
+ span: Span,
+}
+
+impl<T> core::ops::Deref for Spanned<T> {
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ &self.value
+ }
+}
+
+trait SpannedValue: Sized {
+ fn spanned(self, span: Span) -> Spanned<Self>;
+}
+
+impl<T> SpannedValue for T {
+ fn spanned(self, span: Span) -> Spanned<Self> {
+ Spanned { value: self, span }
+ }
+}
+
+struct Error {
+ message: &'static str,
+ _span: Unused<Span>,
+ proc_span: proc_macro::Span,
+}
+
+impl From<Error> for crate::Error {
+ fn from(error: Error) -> Self {
+ Self::Custom {
+ message: error.message.into(),
+ span_start: Some(error.proc_span),
+ span_end: Some(error.proc_span),
+ }
+ }
+}
+
+struct Unused<T>(core::marker::PhantomData<T>);
+
+#[allow(clippy::missing_const_for_fn)] // false positive
+fn unused<T>(_: T) -> Unused<T> {
+ Unused(core::marker::PhantomData)
+}
diff --git a/third_party/rust/time-macros/src/format_description/public/component.rs b/third_party/rust/time-macros/src/format_description/public/component.rs
new file mode 100644
index 0000000000..4737c6ce5c
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/public/component.rs
@@ -0,0 +1,49 @@
+use proc_macro::{Ident, Span, TokenStream};
+
+use super::modifier;
+use crate::to_tokens::ToTokenStream;
+
+macro_rules! declare_component {
+ ($($name:ident)*) => {
+ pub(crate) enum Component {$(
+ $name(modifier::$name),
+ )*}
+
+ impl ToTokenStream for Component {
+ fn append_to(self, ts: &mut TokenStream) {
+ let mut mts = TokenStream::new();
+
+ let component = match self {$(
+ Self::$name(modifier) => {
+ modifier.append_to(&mut mts);
+ stringify!($name)
+ }
+ )*};
+ let component = Ident::new(component, Span::mixed_site());
+
+ quote_append! { ts
+ ::time::format_description::Component::#(component)(#S(mts))
+ }
+ }
+ }
+ };
+}
+
+declare_component! {
+ Day
+ Month
+ Ordinal
+ Weekday
+ WeekNumber
+ Year
+ Hour
+ Minute
+ Period
+ Second
+ Subsecond
+ OffsetHour
+ OffsetMinute
+ OffsetSecond
+ Ignore
+ UnixTimestamp
+}
diff --git a/third_party/rust/time-macros/src/format_description/public/mod.rs b/third_party/rust/time-macros/src/format_description/public/mod.rs
new file mode 100644
index 0000000000..ccb0b6e2a3
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/public/mod.rs
@@ -0,0 +1,54 @@
+mod component;
+pub(super) mod modifier;
+
+use proc_macro::{Literal, TokenStream};
+
+pub(crate) use self::component::Component;
+use crate::to_tokens::ToTokenStream;
+
+#[allow(variant_size_differences)]
+pub(crate) enum OwnedFormatItem {
+ Literal(Box<[u8]>),
+ Component(Component),
+ Compound(Box<[Self]>),
+ Optional(Box<Self>),
+ First(Box<[Self]>),
+}
+
+impl ToTokenStream for OwnedFormatItem {
+ fn append_to(self, ts: &mut TokenStream) {
+ match self {
+ Self::Literal(bytes) => quote_append! { ts
+ ::time::format_description::FormatItem::Literal {
+ 0: #(Literal::byte_string(bytes.as_ref()))
+ }
+ },
+ Self::Component(component) => quote_append! { ts
+ ::time::format_description::FormatItem::Component { 0: #S(component) }
+ },
+ Self::Compound(items) => {
+ let items = items
+ .into_vec()
+ .into_iter()
+ .map(|item| quote! { #S(item), })
+ .collect::<TokenStream>();
+ quote_append! { ts
+ ::time::format_description::FormatItem::Compound { 0: &[#S(items)] }
+ }
+ }
+ Self::Optional(item) => quote_append! {ts
+ ::time::format_description::FormatItem::Optional { 0: &#S(*item) }
+ },
+ Self::First(items) => {
+ let items = items
+ .into_vec()
+ .into_iter()
+ .map(|item| quote! { #S(item), })
+ .collect::<TokenStream>();
+ quote_append! { ts
+ ::time::format_description::FormatItem::First { 0: &[#S(items)] }
+ }
+ }
+ }
+ }
+}
diff --git a/third_party/rust/time-macros/src/format_description/public/modifier.rs b/third_party/rust/time-macros/src/format_description/public/modifier.rs
new file mode 100644
index 0000000000..e39c6bf552
--- /dev/null
+++ b/third_party/rust/time-macros/src/format_description/public/modifier.rs
@@ -0,0 +1,247 @@
+use std::num::NonZeroU16;
+
+use proc_macro::{Ident, Span, TokenStream, TokenTree};
+
+use crate::to_tokens::{ToTokenStream, ToTokenTree};
+
+macro_rules! to_tokens {
+ (
+ $(#[$struct_attr:meta])*
+ $struct_vis:vis struct $struct_name:ident {$(
+ $(#[$field_attr:meta])*
+ $field_vis:vis $field_name:ident : $field_ty:ty
+ ),+ $(,)?}
+ ) => {
+ $(#[$struct_attr])*
+ $struct_vis struct $struct_name {$(
+ $(#[$field_attr])*
+ $field_vis $field_name: $field_ty
+ ),+}
+
+ impl ToTokenTree for $struct_name {
+ fn into_token_tree(self) -> TokenTree {
+ let mut tokens = TokenStream::new();
+ let Self {$($field_name),+} = self;
+
+ quote_append! { tokens
+ let mut value = ::time::format_description::modifier::$struct_name::default();
+ };
+ $(
+ quote_append!(tokens value.$field_name =);
+ $field_name.append_to(&mut tokens);
+ quote_append!(tokens ;);
+ )+
+ quote_append!(tokens value);
+
+ proc_macro::TokenTree::Group(proc_macro::Group::new(
+ proc_macro::Delimiter::Brace,
+ tokens,
+ ))
+ }
+ }
+ };
+
+ (
+ $(#[$enum_attr:meta])*
+ $enum_vis:vis enum $enum_name:ident {$(
+ $(#[$variant_attr:meta])*
+ $variant_name:ident
+ ),+ $(,)?}
+ ) => {
+ $(#[$enum_attr])*
+ $enum_vis enum $enum_name {$(
+ $(#[$variant_attr])*
+ $variant_name
+ ),+}
+
+ impl ToTokenStream for $enum_name {
+ fn append_to(self, ts: &mut TokenStream) {
+ quote_append! { ts
+ ::time::format_description::modifier::$enum_name::
+ };
+ let name = match self {
+ $(Self::$variant_name => stringify!($variant_name)),+
+ };
+ ts.extend([TokenTree::Ident(Ident::new(name, Span::mixed_site()))]);
+ }
+ }
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Day {
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) enum MonthRepr {
+ Numerical,
+ Long,
+ Short,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Month {
+ pub(crate) padding: Padding,
+ pub(crate) repr: MonthRepr,
+ pub(crate) case_sensitive: bool,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Ordinal {
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) enum WeekdayRepr {
+ Short,
+ Long,
+ Sunday,
+ Monday,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Weekday {
+ pub(crate) repr: WeekdayRepr,
+ pub(crate) one_indexed: bool,
+ pub(crate) case_sensitive: bool,
+ }
+}
+
+to_tokens! {
+ pub(crate) enum WeekNumberRepr {
+ Iso,
+ Sunday,
+ Monday,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct WeekNumber {
+ pub(crate) padding: Padding,
+ pub(crate) repr: WeekNumberRepr,
+ }
+}
+
+to_tokens! {
+ pub(crate) enum YearRepr {
+ Full,
+ LastTwo,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Year {
+ pub(crate) padding: Padding,
+ pub(crate) repr: YearRepr,
+ pub(crate) iso_week_based: bool,
+ pub(crate) sign_is_mandatory: bool,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Hour {
+ pub(crate) padding: Padding,
+ pub(crate) is_12_hour_clock: bool,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Minute {
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Period {
+ pub(crate) is_uppercase: bool,
+ pub(crate) case_sensitive: bool,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Second {
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) enum SubsecondDigits {
+ One,
+ Two,
+ Three,
+ Four,
+ Five,
+ Six,
+ Seven,
+ Eight,
+ Nine,
+ OneOrMore,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct Subsecond {
+ pub(crate) digits: SubsecondDigits,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct OffsetHour {
+ pub(crate) sign_is_mandatory: bool,
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct OffsetMinute {
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct OffsetSecond {
+ pub(crate) padding: Padding,
+ }
+}
+
+to_tokens! {
+ pub(crate) enum Padding {
+ Space,
+ Zero,
+ None,
+ }
+}
+
+pub(crate) struct Ignore {
+ pub(crate) count: NonZeroU16,
+}
+
+impl ToTokenTree for Ignore {
+ fn into_token_tree(self) -> TokenTree {
+ quote_group! {{
+ ::time::format_description::modifier::Ignore::count(#(self.count))
+ }}
+ }
+}
+
+to_tokens! {
+ pub(crate) enum UnixTimestampPrecision {
+ Second,
+ Millisecond,
+ Microsecond,
+ Nanosecond,
+ }
+}
+
+to_tokens! {
+ pub(crate) struct UnixTimestamp {
+ pub(crate) precision: UnixTimestampPrecision,
+ pub(crate) sign_is_mandatory: bool,
+ }
+}