summaryrefslogtreecommitdiffstats
path: root/vendor/time-macros/src/format_description
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/time-macros/src/format_description')
-rw-r--r--vendor/time-macros/src/format_description/component.rs168
-rw-r--r--vendor/time-macros/src/format_description/error.rs29
-rw-r--r--vendor/time-macros/src/format_description/mod.rs40
-rw-r--r--vendor/time-macros/src/format_description/modifier.rs417
-rw-r--r--vendor/time-macros/src/format_description/parse.rs84
5 files changed, 738 insertions, 0 deletions
diff --git a/vendor/time-macros/src/format_description/component.rs b/vendor/time-macros/src/format_description/component.rs
new file mode 100644
index 000000000..850da91d2
--- /dev/null
+++ b/vendor/time-macros/src/format_description/component.rs
@@ -0,0 +1,168 @@
+use proc_macro::{Ident, Span, TokenStream};
+
+use crate::format_description::error::InvalidFormatDescription;
+use crate::format_description::modifier;
+use crate::format_description::modifier::Modifiers;
+use crate::to_tokens::ToTokenStream;
+
+pub(crate) enum Component {
+ Day(modifier::Day),
+ Month(modifier::Month),
+ Ordinal(modifier::Ordinal),
+ Weekday(modifier::Weekday),
+ WeekNumber(modifier::WeekNumber),
+ Year(modifier::Year),
+ Hour(modifier::Hour),
+ Minute(modifier::Minute),
+ Period(modifier::Period),
+ Second(modifier::Second),
+ Subsecond(modifier::Subsecond),
+ OffsetHour(modifier::OffsetHour),
+ OffsetMinute(modifier::OffsetMinute),
+ OffsetSecond(modifier::OffsetSecond),
+}
+
+impl ToTokenStream for Component {
+ fn append_to(self, ts: &mut TokenStream) {
+ let mut mts = TokenStream::new();
+
+ macro_rules! component_name_and_append {
+ ($($name:ident)*) => {
+ match self {
+ $(Self::$name(modifier) => {
+ modifier.append_to(&mut mts);
+ stringify!($name)
+ })*
+ }
+ };
+ }
+
+ let component = component_name_and_append![
+ Day
+ Month
+ Ordinal
+ Weekday
+ WeekNumber
+ Year
+ Hour
+ Minute
+ Period
+ Second
+ Subsecond
+ OffsetHour
+ OffsetMinute
+ OffsetSecond
+ ];
+ let component = Ident::new(component, Span::mixed_site());
+
+ quote_append! { ts
+ ::time::format_description::Component::#(component)(#S(mts))
+ }
+ }
+}
+
+pub(crate) enum NakedComponent {
+ Day,
+ Month,
+ Ordinal,
+ Weekday,
+ WeekNumber,
+ Year,
+ Hour,
+ Minute,
+ Period,
+ Second,
+ Subsecond,
+ OffsetHour,
+ OffsetMinute,
+ OffsetSecond,
+}
+
+impl NakedComponent {
+ pub(crate) fn parse(
+ component_name: &[u8],
+ component_index: usize,
+ ) -> Result<Self, InvalidFormatDescription> {
+ match component_name {
+ b"day" => Ok(Self::Day),
+ b"month" => Ok(Self::Month),
+ b"ordinal" => Ok(Self::Ordinal),
+ b"weekday" => Ok(Self::Weekday),
+ b"week_number" => Ok(Self::WeekNumber),
+ b"year" => Ok(Self::Year),
+ b"hour" => Ok(Self::Hour),
+ b"minute" => Ok(Self::Minute),
+ b"period" => Ok(Self::Period),
+ b"second" => Ok(Self::Second),
+ b"subsecond" => Ok(Self::Subsecond),
+ b"offset_hour" => Ok(Self::OffsetHour),
+ b"offset_minute" => Ok(Self::OffsetMinute),
+ b"offset_second" => Ok(Self::OffsetSecond),
+ b"" => Err(InvalidFormatDescription::MissingComponentName {
+ index: component_index,
+ }),
+ _ => Err(InvalidFormatDescription::InvalidComponentName {
+ name: String::from_utf8_lossy(component_name).into_owned(),
+ index: component_index,
+ }),
+ }
+ }
+
+ pub(crate) fn attach_modifiers(self, modifiers: Modifiers) -> Component {
+ match self {
+ Self::Day => Component::Day(modifier::Day {
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ Self::Month => Component::Month(modifier::Month {
+ padding: modifiers.padding.unwrap_or_default(),
+ repr: modifiers.month_repr.unwrap_or_default(),
+ case_sensitive: modifiers.case_sensitive.unwrap_or(true),
+ }),
+ Self::Ordinal => Component::Ordinal(modifier::Ordinal {
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ Self::Weekday => Component::Weekday(modifier::Weekday {
+ repr: modifiers.weekday_repr.unwrap_or_default(),
+ one_indexed: modifiers.weekday_is_one_indexed.unwrap_or(true),
+ case_sensitive: modifiers.case_sensitive.unwrap_or(true),
+ }),
+ Self::WeekNumber => Component::WeekNumber(modifier::WeekNumber {
+ padding: modifiers.padding.unwrap_or_default(),
+ repr: modifiers.week_number_repr.unwrap_or_default(),
+ }),
+ Self::Year => Component::Year(modifier::Year {
+ padding: modifiers.padding.unwrap_or_default(),
+ repr: modifiers.year_repr.unwrap_or_default(),
+ iso_week_based: modifiers.year_is_iso_week_based.unwrap_or_default(),
+ sign_is_mandatory: modifiers.sign_is_mandatory.unwrap_or_default(),
+ }),
+ Self::Hour => Component::Hour(modifier::Hour {
+ padding: modifiers.padding.unwrap_or_default(),
+ is_12_hour_clock: modifiers.hour_is_12_hour_clock.unwrap_or_default(),
+ }),
+ Self::Minute => Component::Minute(modifier::Minute {
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ Self::Period => Component::Period(modifier::Period {
+ is_uppercase: modifiers.period_is_uppercase.unwrap_or(true),
+ case_sensitive: modifiers.case_sensitive.unwrap_or(true),
+ }),
+ Self::Second => Component::Second(modifier::Second {
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ Self::Subsecond => Component::Subsecond(modifier::Subsecond {
+ digits: modifiers.subsecond_digits.unwrap_or_default(),
+ }),
+ Self::OffsetHour => Component::OffsetHour(modifier::OffsetHour {
+ sign_is_mandatory: modifiers.sign_is_mandatory.unwrap_or_default(),
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ Self::OffsetMinute => Component::OffsetMinute(modifier::OffsetMinute {
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ Self::OffsetSecond => Component::OffsetSecond(modifier::OffsetSecond {
+ padding: modifiers.padding.unwrap_or_default(),
+ }),
+ }
+ }
+}
diff --git a/vendor/time-macros/src/format_description/error.rs b/vendor/time-macros/src/format_description/error.rs
new file mode 100644
index 000000000..9aacd7dc9
--- /dev/null
+++ b/vendor/time-macros/src/format_description/error.rs
@@ -0,0 +1,29 @@
+use std::fmt;
+
+pub(crate) enum InvalidFormatDescription {
+ UnclosedOpeningBracket { index: usize },
+ InvalidComponentName { name: String, index: usize },
+ InvalidModifier { value: String, index: usize },
+ MissingComponentName { index: usize },
+}
+
+impl fmt::Display for InvalidFormatDescription {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ #[allow(clippy::enum_glob_use)]
+ use InvalidFormatDescription::*;
+ match self {
+ UnclosedOpeningBracket { index } => {
+ write!(f, "unclosed opening bracket at byte index {index}")
+ }
+ InvalidComponentName { name, index } => {
+ write!(f, "invalid component name `{name}` at byte index {index}",)
+ }
+ InvalidModifier { value, index } => {
+ write!(f, "invalid modifier `{value}` at byte index {index}")
+ }
+ MissingComponentName { index } => {
+ write!(f, "missing component name at byte index {index}")
+ }
+ }
+ }
+}
diff --git a/vendor/time-macros/src/format_description/mod.rs b/vendor/time-macros/src/format_description/mod.rs
new file mode 100644
index 000000000..dd32db74d
--- /dev/null
+++ b/vendor/time-macros/src/format_description/mod.rs
@@ -0,0 +1,40 @@
+mod component;
+pub(crate) mod error;
+pub(crate) mod modifier;
+pub(crate) mod parse;
+
+use proc_macro::{Literal, TokenStream};
+
+pub(crate) use self::component::Component;
+pub(crate) use self::parse::parse;
+use crate::to_tokens::ToTokenStream;
+
+mod helper {
+ #[must_use = "This does not modify the original slice."]
+ pub(crate) fn consume_whitespace<'a>(bytes: &'a [u8], index: &mut usize) -> &'a [u8] {
+ let first_non_whitespace = bytes
+ .iter()
+ .position(|c| !c.is_ascii_whitespace())
+ .unwrap_or(bytes.len());
+ *index += first_non_whitespace;
+ &bytes[first_non_whitespace..]
+ }
+}
+
+#[allow(single_use_lifetimes)] // false positive
+#[allow(variant_size_differences)]
+pub(crate) enum FormatItem<'a> {
+ Literal(&'a [u8]),
+ Component(Component),
+}
+
+impl ToTokenStream for FormatItem<'_> {
+ fn append_to(self, ts: &mut TokenStream) {
+ quote_append! { ts
+ ::time::format_description::FormatItem::#S(match self {
+ FormatItem::Literal(bytes) => quote! { Literal(#(Literal::byte_string(bytes))) },
+ FormatItem::Component(component) => quote! { Component(#S(component)) },
+ })
+ }
+ }
+}
diff --git a/vendor/time-macros/src/format_description/modifier.rs b/vendor/time-macros/src/format_description/modifier.rs
new file mode 100644
index 000000000..f4e641a7b
--- /dev/null
+++ b/vendor/time-macros/src/format_description/modifier.rs
@@ -0,0 +1,417 @@
+use core::mem;
+
+use proc_macro::{Ident, Span, TokenStream, TokenTree};
+
+use crate::format_description::error::InvalidFormatDescription;
+use crate::format_description::helper;
+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,
+ }
+}
+
+macro_rules! impl_default {
+ ($($type:ty => $default:expr;)*) => {$(
+ impl Default for $type {
+ fn default() -> Self {
+ $default
+ }
+ }
+ )*};
+}
+
+impl_default! {
+ Day => Self { padding: Padding::default() };
+ MonthRepr => Self::Numerical;
+ Month => Self {
+ padding: Padding::default(),
+ repr: MonthRepr::default(),
+ case_sensitive: true,
+ };
+ Ordinal => Self { padding: Padding::default() };
+ WeekdayRepr => Self::Long;
+ Weekday => Self {
+ repr: WeekdayRepr::default(),
+ one_indexed: true,
+ case_sensitive: true,
+ };
+ WeekNumberRepr => Self::Iso;
+ WeekNumber => Self {
+ padding: Padding::default(),
+ repr: WeekNumberRepr::default(),
+ };
+ YearRepr => Self::Full;
+ Year => Self {
+ padding: Padding::default(),
+ repr: YearRepr::default(),
+ iso_week_based: false,
+ sign_is_mandatory: false,
+ };
+ Hour => Self {
+ padding: Padding::default(),
+ is_12_hour_clock: false,
+ };
+ Minute => Self { padding: Padding::default() };
+ Period => Self { is_uppercase: true, case_sensitive: true };
+ Second => Self { padding: Padding::default() };
+ SubsecondDigits => Self::OneOrMore;
+ Subsecond => Self { digits: SubsecondDigits::default() };
+ OffsetHour => Self {
+ sign_is_mandatory: true,
+ padding: Padding::default(),
+ };
+ OffsetMinute => Self { padding: Padding::default() };
+ OffsetSecond => Self { padding: Padding::default() };
+ Padding => Self::Zero;
+}
+
+#[derive(Default)]
+pub(crate) struct Modifiers {
+ pub(crate) padding: Option<Padding>,
+ pub(crate) hour_is_12_hour_clock: Option<bool>,
+ pub(crate) period_is_uppercase: Option<bool>,
+ pub(crate) month_repr: Option<MonthRepr>,
+ pub(crate) subsecond_digits: Option<SubsecondDigits>,
+ pub(crate) weekday_repr: Option<WeekdayRepr>,
+ pub(crate) weekday_is_one_indexed: Option<bool>,
+ pub(crate) week_number_repr: Option<WeekNumberRepr>,
+ pub(crate) year_repr: Option<YearRepr>,
+ pub(crate) year_is_iso_week_based: Option<bool>,
+ pub(crate) sign_is_mandatory: Option<bool>,
+ pub(crate) case_sensitive: Option<bool>,
+}
+
+impl Modifiers {
+ #[allow(clippy::too_many_lines)]
+ pub(crate) fn parse(
+ component_name: &[u8],
+ mut bytes: &[u8],
+ index: &mut usize,
+ ) -> Result<Self, InvalidFormatDescription> {
+ let mut modifiers = Self::default();
+
+ while !bytes.is_empty() {
+ // Trim any whitespace between modifiers.
+ bytes = helper::consume_whitespace(bytes, index);
+
+ let modifier;
+ if let Some(whitespace_loc) = bytes.iter().position(u8::is_ascii_whitespace) {
+ *index += whitespace_loc;
+ modifier = &bytes[..whitespace_loc];
+ bytes = &bytes[whitespace_loc..];
+ } else {
+ modifier = mem::take(&mut bytes);
+ }
+
+ if modifier.is_empty() {
+ break;
+ }
+
+ match (component_name, modifier) {
+ (
+ b"day" | b"hour" | b"minute" | b"month" | b"offset_hour" | b"offset_minute"
+ | b"offset_second" | b"ordinal" | b"second" | b"week_number" | b"year",
+ b"padding:space",
+ ) => modifiers.padding = Some(Padding::Space),
+ (
+ b"day" | b"hour" | b"minute" | b"month" | b"offset_hour" | b"offset_minute"
+ | b"offset_second" | b"ordinal" | b"second" | b"week_number" | b"year",
+ b"padding:zero",
+ ) => modifiers.padding = Some(Padding::Zero),
+ (
+ b"day" | b"hour" | b"minute" | b"month" | b"offset_hour" | b"offset_minute"
+ | b"offset_second" | b"ordinal" | b"second" | b"week_number" | b"year",
+ b"padding:none",
+ ) => modifiers.padding = Some(Padding::None),
+ (b"hour", b"repr:24") => modifiers.hour_is_12_hour_clock = Some(false),
+ (b"hour", b"repr:12") => modifiers.hour_is_12_hour_clock = Some(true),
+ (b"month" | b"period" | b"weekday", b"case_sensitive:true") => {
+ modifiers.case_sensitive = Some(true)
+ }
+ (b"month" | b"period" | b"weekday", b"case_sensitive:false") => {
+ modifiers.case_sensitive = Some(false)
+ }
+ (b"month", b"repr:numerical") => modifiers.month_repr = Some(MonthRepr::Numerical),
+ (b"month", b"repr:long") => modifiers.month_repr = Some(MonthRepr::Long),
+ (b"month", b"repr:short") => modifiers.month_repr = Some(MonthRepr::Short),
+ (b"offset_hour" | b"year", b"sign:automatic") => {
+ modifiers.sign_is_mandatory = Some(false);
+ }
+ (b"offset_hour" | b"year", b"sign:mandatory") => {
+ modifiers.sign_is_mandatory = Some(true);
+ }
+ (b"period", b"case:upper") => modifiers.period_is_uppercase = Some(true),
+ (b"period", b"case:lower") => modifiers.period_is_uppercase = Some(false),
+ (b"subsecond", b"digits:1") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::One);
+ }
+ (b"subsecond", b"digits:2") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Two);
+ }
+ (b"subsecond", b"digits:3") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Three);
+ }
+ (b"subsecond", b"digits:4") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Four);
+ }
+ (b"subsecond", b"digits:5") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Five);
+ }
+ (b"subsecond", b"digits:6") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Six);
+ }
+ (b"subsecond", b"digits:7") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Seven);
+ }
+ (b"subsecond", b"digits:8") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Eight);
+ }
+ (b"subsecond", b"digits:9") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::Nine);
+ }
+ (b"subsecond", b"digits:1+") => {
+ modifiers.subsecond_digits = Some(SubsecondDigits::OneOrMore);
+ }
+ (b"weekday", b"repr:short") => modifiers.weekday_repr = Some(WeekdayRepr::Short),
+ (b"weekday", b"repr:long") => modifiers.weekday_repr = Some(WeekdayRepr::Long),
+ (b"weekday", b"repr:sunday") => modifiers.weekday_repr = Some(WeekdayRepr::Sunday),
+ (b"weekday", b"repr:monday") => modifiers.weekday_repr = Some(WeekdayRepr::Monday),
+ (b"weekday", b"one_indexed:true") => modifiers.weekday_is_one_indexed = Some(true),
+ (b"weekday", b"one_indexed:false") => {
+ modifiers.weekday_is_one_indexed = Some(false);
+ }
+ (b"week_number", b"repr:iso") => {
+ modifiers.week_number_repr = Some(WeekNumberRepr::Iso);
+ }
+ (b"week_number", b"repr:sunday") => {
+ modifiers.week_number_repr = Some(WeekNumberRepr::Sunday);
+ }
+ (b"week_number", b"repr:monday") => {
+ modifiers.week_number_repr = Some(WeekNumberRepr::Monday);
+ }
+ (b"year", b"repr:full") => modifiers.year_repr = Some(YearRepr::Full),
+ (b"year", b"repr:last_two") => modifiers.year_repr = Some(YearRepr::LastTwo),
+ (b"year", b"base:calendar") => modifiers.year_is_iso_week_based = Some(false),
+ (b"year", b"base:iso_week") => modifiers.year_is_iso_week_based = Some(true),
+ _ => {
+ return Err(InvalidFormatDescription::InvalidModifier {
+ value: String::from_utf8_lossy(modifier).into_owned(),
+ index: *index,
+ });
+ }
+ }
+ }
+
+ Ok(modifiers)
+ }
+}
diff --git a/vendor/time-macros/src/format_description/parse.rs b/vendor/time-macros/src/format_description/parse.rs
new file mode 100644
index 000000000..19c7bf608
--- /dev/null
+++ b/vendor/time-macros/src/format_description/parse.rs
@@ -0,0 +1,84 @@
+use proc_macro::Span;
+
+use crate::format_description::component::{Component, NakedComponent};
+use crate::format_description::error::InvalidFormatDescription;
+use crate::format_description::{helper, modifier, FormatItem};
+use crate::Error;
+
+struct ParsedItem<'a> {
+ item: FormatItem<'a>,
+ remaining: &'a [u8],
+}
+
+fn parse_component(mut s: &[u8], index: &mut usize) -> Result<Component, InvalidFormatDescription> {
+ s = helper::consume_whitespace(s, index);
+
+ let component_index = *index;
+ let whitespace_loc = s
+ .iter()
+ .position(u8::is_ascii_whitespace)
+ .unwrap_or(s.len());
+ *index += whitespace_loc;
+ let component_name = &s[..whitespace_loc];
+ s = &s[whitespace_loc..];
+ s = helper::consume_whitespace(s, index);
+
+ Ok(NakedComponent::parse(component_name, component_index)?
+ .attach_modifiers(modifier::Modifiers::parse(component_name, s, index)?))
+}
+
+fn parse_literal<'a>(s: &'a [u8], index: &mut usize) -> ParsedItem<'a> {
+ let loc = s.iter().position(|&c| c == b'[').unwrap_or(s.len());
+ *index += loc;
+ ParsedItem {
+ item: FormatItem::Literal(&s[..loc]),
+ remaining: &s[loc..],
+ }
+}
+
+fn parse_item<'a>(
+ s: &'a [u8],
+ index: &mut usize,
+) -> Result<ParsedItem<'a>, InvalidFormatDescription> {
+ if let [b'[', b'[', remaining @ ..] = s {
+ *index += 2;
+ return Ok(ParsedItem {
+ item: FormatItem::Literal(b"["),
+ remaining,
+ });
+ };
+
+ if s.starts_with(b"[") {
+ if let Some(bracket_index) = s.iter().position(|&c| c == b']') {
+ *index += 1; // opening bracket
+ let ret_val = ParsedItem {
+ item: FormatItem::Component(parse_component(&s[1..bracket_index], index)?),
+ remaining: &s[bracket_index + 1..],
+ };
+ *index += 1; // closing bracket
+ Ok(ret_val)
+ } else {
+ Err(InvalidFormatDescription::UnclosedOpeningBracket { index: *index })
+ }
+ } else {
+ Ok(parse_literal(s, index))
+ }
+}
+
+pub(crate) fn parse(mut s: &[u8], span: Span) -> Result<Vec<FormatItem<'_>>, Error> {
+ let mut compound = Vec::new();
+ let mut loc = 0;
+
+ while !s.is_empty() {
+ let ParsedItem { item, remaining } =
+ parse_item(s, &mut loc).map_err(|error| Error::InvalidFormatDescription {
+ error,
+ span_start: Some(span),
+ span_end: Some(span),
+ })?;
+ s = remaining;
+ compound.push(item);
+ }
+
+ Ok(compound)
+}