time_macros/format_description/public/
modifier.rs

1use std::num::NonZeroU16;
2
3use proc_macro::{Ident, Span, TokenStream, TokenTree};
4
5use crate::to_tokens::{ToTokenStream, ToTokenTree};
6
7macro_rules! to_tokens {
8    (
9        $(#[$struct_attr:meta])*
10        $struct_vis:vis struct $struct_name:ident {$(
11            $(#[$field_attr:meta])*
12            $field_vis:vis $field_name:ident : $field_ty:ty
13        ),* $(,)?}
14    ) => {
15        $(#[$struct_attr])*
16        $struct_vis struct $struct_name {$(
17            $(#[$field_attr])*
18            $field_vis $field_name: $field_ty
19        ),*}
20
21        impl ToTokenTree for $struct_name {
22            fn into_token_tree(self) -> TokenTree {
23                let mut tokens = TokenStream::new();
24                let Self {$($field_name),*} = self;
25
26                quote_append! { tokens
27                    let mut value = ::time::format_description::modifier::$struct_name::default();
28                };
29                $(
30                    quote_append!(tokens value.$field_name =);
31                    $field_name.append_to(&mut tokens);
32                    quote_append!(tokens ;);
33                )*
34                quote_append!(tokens value);
35
36                proc_macro::TokenTree::Group(proc_macro::Group::new(
37                    proc_macro::Delimiter::Brace,
38                    tokens,
39                ))
40            }
41        }
42    };
43
44    (
45        $(#[$enum_attr:meta])*
46        $enum_vis:vis enum $enum_name:ident {$(
47            $(#[$variant_attr:meta])*
48            $variant_name:ident
49        ),+ $(,)?}
50    ) => {
51        $(#[$enum_attr])*
52        $enum_vis enum $enum_name {$(
53            $(#[$variant_attr])*
54            $variant_name
55        ),+}
56
57        impl ToTokenStream for $enum_name {
58            fn append_to(self, ts: &mut TokenStream) {
59                quote_append! { ts
60                    ::time::format_description::modifier::$enum_name::
61                };
62                let name = match self {
63                    $(Self::$variant_name => stringify!($variant_name)),+
64                };
65                ts.extend([TokenTree::Ident(Ident::new(name, Span::mixed_site()))]);
66            }
67        }
68    }
69}
70
71to_tokens! {
72    pub(crate) struct Day {
73        pub(crate) padding: Padding,
74    }
75}
76
77to_tokens! {
78    pub(crate) enum MonthRepr {
79        Numerical,
80        Long,
81        Short,
82    }
83}
84
85to_tokens! {
86    pub(crate) struct Month {
87        pub(crate) padding: Padding,
88        pub(crate) repr: MonthRepr,
89        pub(crate) case_sensitive: bool,
90    }
91}
92
93to_tokens! {
94    pub(crate) struct Ordinal {
95        pub(crate) padding: Padding,
96    }
97}
98
99to_tokens! {
100    pub(crate) enum WeekdayRepr {
101        Short,
102        Long,
103        Sunday,
104        Monday,
105    }
106}
107
108to_tokens! {
109    pub(crate) struct Weekday {
110        pub(crate) repr: WeekdayRepr,
111        pub(crate) one_indexed: bool,
112        pub(crate) case_sensitive: bool,
113    }
114}
115
116to_tokens! {
117    pub(crate) enum WeekNumberRepr {
118        Iso,
119        Sunday,
120        Monday,
121    }
122}
123
124to_tokens! {
125    pub(crate) struct WeekNumber {
126        pub(crate) padding: Padding,
127        pub(crate) repr: WeekNumberRepr,
128    }
129}
130
131to_tokens! {
132    pub(crate) enum YearRepr {
133        Full,
134        LastTwo,
135    }
136}
137
138to_tokens! {
139    pub(crate) struct Year {
140        pub(crate) padding: Padding,
141        pub(crate) repr: YearRepr,
142        pub(crate) iso_week_based: bool,
143        pub(crate) sign_is_mandatory: bool,
144    }
145}
146
147to_tokens! {
148    pub(crate) struct Hour {
149        pub(crate) padding: Padding,
150        pub(crate) is_12_hour_clock: bool,
151    }
152}
153
154to_tokens! {
155    pub(crate) struct Minute {
156        pub(crate) padding: Padding,
157    }
158}
159
160to_tokens! {
161    pub(crate) struct Period {
162        pub(crate) is_uppercase: bool,
163        pub(crate) case_sensitive: bool,
164    }
165}
166
167to_tokens! {
168    pub(crate) struct Second {
169        pub(crate) padding: Padding,
170    }
171}
172
173to_tokens! {
174    pub(crate) enum SubsecondDigits {
175        One,
176        Two,
177        Three,
178        Four,
179        Five,
180        Six,
181        Seven,
182        Eight,
183        Nine,
184        OneOrMore,
185    }
186}
187
188to_tokens! {
189    pub(crate) struct Subsecond {
190        pub(crate) digits: SubsecondDigits,
191    }
192}
193
194to_tokens! {
195    pub(crate) struct OffsetHour {
196        pub(crate) sign_is_mandatory: bool,
197        pub(crate) padding: Padding,
198    }
199}
200
201to_tokens! {
202    pub(crate) struct OffsetMinute {
203        pub(crate) padding: Padding,
204    }
205}
206
207to_tokens! {
208    pub(crate) struct OffsetSecond {
209        pub(crate) padding: Padding,
210    }
211}
212
213to_tokens! {
214    pub(crate) enum Padding {
215        Space,
216        Zero,
217        None,
218    }
219}
220
221pub(crate) struct Ignore {
222    pub(crate) count: NonZeroU16,
223}
224
225impl ToTokenTree for Ignore {
226    fn into_token_tree(self) -> TokenTree {
227        quote_group! {{
228            ::time::format_description::modifier::Ignore::count(#(self.count))
229        }}
230    }
231}
232
233to_tokens! {
234    pub(crate) enum UnixTimestampPrecision {
235        Second,
236        Millisecond,
237        Microsecond,
238        Nanosecond,
239    }
240}
241
242to_tokens! {
243    pub(crate) struct UnixTimestamp {
244        pub(crate) precision: UnixTimestampPrecision,
245        pub(crate) sign_is_mandatory: bool,
246    }
247}
248
249to_tokens! {
250    pub(crate) struct End {}
251}