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 append_to(self, ts: &mut TokenStream) {
16                let mut mts = TokenStream::new();
17
18                let component = match self {$(
19                    $(#[cfg($cfg_inner)])*
20                    Self::$name(modifier) => {
21                        modifier.append_to(&mut mts);
22                        stringify!($name)
23                    }
24                )*};
25                let component = Ident::new(component, Span::mixed_site());
26
27                quote_append! { ts
28                    Component::#(component)(#S(mts))
29                }
30            }
31        }
32    };
33}
34
35declare_component! {
36    Day
37    MonthShort
38    MonthLong
39    MonthNumerical
40    Ordinal
41    WeekdayShort
42    WeekdayLong
43    WeekdaySunday
44    WeekdayMonday
45    WeekNumberIso
46    WeekNumberSunday
47    WeekNumberMonday
48    #[cfg(feature = "large-dates")]
49    CalendarYearFullExtendedRange
50    CalendarYearFullStandardRange
51    #[cfg(feature = "large-dates")]
52    IsoYearFullExtendedRange
53    IsoYearFullStandardRange
54    #[cfg(feature = "large-dates")]
55    CalendarYearCenturyExtendedRange
56    CalendarYearCenturyStandardRange
57    #[cfg(feature = "large-dates")]
58    IsoYearCenturyExtendedRange
59    IsoYearCenturyStandardRange
60    CalendarYearLastTwo
61    IsoYearLastTwo
62    Hour12
63    Hour24
64    Minute
65    Period
66    Second
67    Subsecond
68    OffsetHour
69    OffsetMinute
70    OffsetSecond
71    Ignore
72    UnixTimestampSecond
73    UnixTimestampMillisecond
74    UnixTimestampMicrosecond
75    UnixTimestampNanosecond
76    End
77}