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        Century,
135        LastTwo,
136    }
137}
138
139to_tokens! {
140    pub(crate) enum YearRange {
141        Standard,
142        Extended,
143    }
144}
145
146to_tokens! {
147    pub(crate) struct Year {
148        pub(crate) padding: Padding,
149        pub(crate) repr: YearRepr,
150        pub(crate) range: YearRange,
151        pub(crate) iso_week_based: bool,
152        pub(crate) sign_is_mandatory: bool,
153    }
154}
155
156to_tokens! {
157    pub(crate) struct Hour {
158        pub(crate) padding: Padding,
159        pub(crate) is_12_hour_clock: bool,
160    }
161}
162
163to_tokens! {
164    pub(crate) struct Minute {
165        pub(crate) padding: Padding,
166    }
167}
168
169to_tokens! {
170    pub(crate) struct Period {
171        pub(crate) is_uppercase: bool,
172        pub(crate) case_sensitive: bool,
173    }
174}
175
176to_tokens! {
177    pub(crate) struct Second {
178        pub(crate) padding: Padding,
179    }
180}
181
182to_tokens! {
183    pub(crate) enum SubsecondDigits {
184        One,
185        Two,
186        Three,
187        Four,
188        Five,
189        Six,
190        Seven,
191        Eight,
192        Nine,
193        OneOrMore,
194    }
195}
196
197to_tokens! {
198    pub(crate) struct Subsecond {
199        pub(crate) digits: SubsecondDigits,
200    }
201}
202
203to_tokens! {
204    pub(crate) struct OffsetHour {
205        pub(crate) sign_is_mandatory: bool,
206        pub(crate) padding: Padding,
207    }
208}
209
210to_tokens! {
211    pub(crate) struct OffsetMinute {
212        pub(crate) padding: Padding,
213    }
214}
215
216to_tokens! {
217    pub(crate) struct OffsetSecond {
218        pub(crate) padding: Padding,
219    }
220}
221
222to_tokens! {
223    pub(crate) enum Padding {
224        Space,
225        Zero,
226        None,
227    }
228}
229
230pub(crate) struct Ignore {
231    pub(crate) count: NonZeroU16,
232}
233
234impl ToTokenTree for Ignore {
235    fn into_token_tree(self) -> TokenTree {
236        quote_group! {{
237            ::time::format_description::modifier::Ignore::count(#(self.count))
238        }}
239    }
240}
241
242to_tokens! {
243    pub(crate) enum UnixTimestampPrecision {
244        Second,
245        Millisecond,
246        Microsecond,
247        Nanosecond,
248    }
249}
250
251to_tokens! {
252    pub(crate) struct UnixTimestamp {
253        pub(crate) precision: UnixTimestampPrecision,
254        pub(crate) sign_is_mandatory: bool,
255    }
256}
257
258to_tokens! {
259    pub(crate) struct End {}
260}