Skip to main content

time_macros/format_description/public/
component.rs

1use proc_macro::{Ident, Span, TokenStream};
2
3use super::modifier;
4use crate::to_tokens::ToTokenStream;
5
6macro_rules! declare_component {
7    ($($(#[cfg($cfg_inner:meta)])* $name:ident)*) => {
8        #[derive(Clone, Copy)]
9        pub(crate) enum Component {$(
10            $(#[cfg($cfg_inner)])*
11            $name(modifier::$name),
12        )*}
13
14        impl ToTokenStream for Component {
15            fn into_token_stream(self) -> TokenStream {
16                self.into_token_stream_with_enum_name("Component")
17            }
18        }
19
20        impl Component {
21            pub(crate) fn into_token_stream_with_enum_name(self, enum_name: &str) -> TokenStream {
22                let (component, modifier) = match self {$(
23                    $(#[cfg($cfg_inner)])*
24                    Self::$name(modifier) => {
25                        (stringify!($name), modifier.into_token_stream())
26                    }
27                )*};
28                let enum_name = Ident::new(enum_name, Span::mixed_site());
29                let component = Ident::new(component, Span::mixed_site());
30
31                quote! {
32                    #(enum_name)::#(component)(#S(modifier))
33                }
34            }
35        }
36    };
37}
38
39declare_component! {
40    Day
41    MonthShort
42    MonthLong
43    MonthNumerical
44    Ordinal
45    WeekdayShort
46    WeekdayLong
47    WeekdaySunday
48    WeekdayMonday
49    WeekNumberIso
50    WeekNumberSunday
51    WeekNumberMonday
52    #[cfg(feature = "large-dates")]
53    CalendarYearFullExtendedRange
54    CalendarYearFullStandardRange
55    #[cfg(feature = "large-dates")]
56    IsoYearFullExtendedRange
57    IsoYearFullStandardRange
58    #[cfg(feature = "large-dates")]
59    CalendarYearCenturyExtendedRange
60    CalendarYearCenturyStandardRange
61    #[cfg(feature = "large-dates")]
62    IsoYearCenturyExtendedRange
63    IsoYearCenturyStandardRange
64    CalendarYearLastTwo
65    IsoYearLastTwo
66    Hour12
67    Hour24
68    Minute
69    Period
70    Second
71    Subsecond
72    OffsetHour
73    OffsetMinute
74    OffsetSecond
75    Ignore
76    UnixTimestampSecond
77    UnixTimestampMillisecond
78    UnixTimestampMicrosecond
79    UnixTimestampNanosecond
80    End
81}