time_macros/format_description/public/
modifier.rs1use std::num::NonZero;
2
3use proc_macro::{Delimiter, Group, 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 = $default:pat
13 ),* $(,)?}
14 ) => {
15 $(#[$struct_attr])*
16 #[derive(Clone, Copy)]
17 $struct_vis struct $struct_name {$(
18 $(#[$field_attr])*
19 $field_vis $field_name: $field_ty
20 ),*}
21
22 impl ToTokenTree for $struct_name {
23 fn into_token_tree(self) -> TokenTree {
24 let Self {$($field_name),*} = self;
25
26 #[allow(clippy::redundant_pattern_matching)]
27 if matches!(($(&$field_name,)*), ($($default,)*)) {
28 return TokenTree::Group(Group::new(
29 Delimiter::None,
30 quote_! { $struct_name::default() }
31 ));
32 }
33
34 let mut tokens = quote_! {
35 $struct_name::default()
36 };
37 $(
38 #[allow(clippy::redundant_pattern_matching)]
39 if !matches!($field_name, $default) {
40 let method_name = Ident::new(concat!("with_", stringify!($field_name)), Span::mixed_site());
41 quote_append!(tokens .#(method_name)(#S($field_name)));
42 }
43 )*
44
45 TokenTree::Group(Group::new(
46 Delimiter::Brace,
47 tokens,
48 ))
49 }
50 }
51 };
52
53 (
54 $(#[$enum_attr:meta])*
55 $enum_vis:vis enum $enum_name:ident {$(
56 $(#[$variant_attr:meta])*
57 $variant_name:ident
58 ),+ $(,)?}
59 ) => {
60 $(#[$enum_attr])*
61 #[derive(Clone, Copy)]
62 $enum_vis enum $enum_name {$(
63 $(#[$variant_attr])*
64 $variant_name
65 ),+}
66
67 impl ToTokenStream for $enum_name {
68 fn append_to(self, ts: &mut TokenStream) {
69 quote_append! { ts
70 $enum_name::
71 };
72 let name = match self {
73 $(Self::$variant_name => stringify!($variant_name)),+
74 };
75 ts.extend([TokenTree::Ident(Ident::new(name, Span::mixed_site()))]);
76 }
77 }
78 }
79}
80
81to_tokens! {
82 pub(crate) struct Day {
83 pub(crate) padding: Padding = Padding::Zero,
84 }
85}
86
87to_tokens! {
88 pub(crate) struct MonthShort {
89 pub(crate) case_sensitive: bool = true,
90 }
91}
92
93to_tokens! {
94 pub(crate) struct MonthLong {
95 pub(crate) case_sensitive: bool = true,
96 }
97}
98
99to_tokens! {
100 pub(crate) struct MonthNumerical {
101 pub(crate) padding: Padding = Padding::Zero,
102 }
103}
104
105to_tokens! {
106 pub(crate) struct Ordinal {
107 pub(crate) padding: Padding = Padding::Zero,
108 }
109}
110
111to_tokens! {
112 pub(crate) struct WeekdayShort {
113 pub(crate) case_sensitive: bool = true,
114 }
115}
116
117to_tokens! {
118 pub(crate) struct WeekdayLong {
119 pub(crate) case_sensitive: bool = true,
120 }
121}
122
123to_tokens! {
124 pub(crate) struct WeekdaySunday {
125 pub(crate) one_indexed: bool = true,
126 }
127}
128
129to_tokens! {
130 pub(crate) struct WeekdayMonday {
131 pub(crate) one_indexed: bool = true,
132 }
133}
134
135to_tokens! {
136 pub(crate) struct WeekNumberIso {
137 pub(crate) padding: Padding = Padding::Zero,
138 }
139}
140
141to_tokens! {
142 pub(crate) struct WeekNumberSunday {
143 pub(crate) padding: Padding = Padding::Zero,
144 }
145}
146
147to_tokens! {
148 pub(crate) struct WeekNumberMonday {
149 pub(crate) padding: Padding = Padding::Zero,
150 }
151}
152
153to_tokens! {
154 pub(crate) struct CalendarYearFullStandardRange {
155 pub(crate) padding: Padding = Padding::Zero,
156 pub(crate) sign_is_mandatory: bool = false,
157 }
158}
159
160#[cfg(feature = "large-dates")]
161to_tokens! {
162 pub(crate) struct CalendarYearFullExtendedRange {
163 pub(crate) padding: Padding = Padding::Zero,
164 pub(crate) sign_is_mandatory: bool = false,
165 }
166}
167
168to_tokens! {
169 pub(crate) struct CalendarYearCenturyStandardRange {
170 pub(crate) padding: Padding = Padding::Zero,
171 pub(crate) sign_is_mandatory: bool = false,
172 }
173}
174
175#[cfg(feature = "large-dates")]
176to_tokens! {
177 pub(crate) struct CalendarYearCenturyExtendedRange {
178 pub(crate) padding: Padding = Padding::Zero,
179 pub(crate) sign_is_mandatory: bool = false,
180 }
181}
182
183to_tokens! {
184 pub(crate) struct IsoYearFullStandardRange {
185 pub(crate) padding: Padding = Padding::Zero,
186 pub(crate) sign_is_mandatory: bool = false,
187 }
188}
189
190#[cfg(feature = "large-dates")]
191to_tokens! {
192 pub(crate) struct IsoYearFullExtendedRange {
193 pub(crate) padding: Padding = Padding::Zero,
194 pub(crate) sign_is_mandatory: bool = false,
195 }
196}
197
198to_tokens! {
199 pub(crate) struct IsoYearCenturyStandardRange {
200 pub(crate) padding: Padding = Padding::Zero,
201 pub(crate) sign_is_mandatory: bool = false,
202 }
203}
204
205#[cfg(feature = "large-dates")]
206to_tokens! {
207 pub(crate) struct IsoYearCenturyExtendedRange {
208 pub(crate) padding: Padding = Padding::Zero,
209 pub(crate) sign_is_mandatory: bool = false,
210 }
211}
212
213to_tokens! {
214 pub(crate) struct CalendarYearLastTwo {
215 pub(crate) padding: Padding = Padding::Zero,
216 }
217}
218
219to_tokens! {
220 pub(crate) struct IsoYearLastTwo {
221 pub(crate) padding: Padding = Padding::Zero,
222 }
223}
224
225to_tokens! {
226 pub(crate) struct Hour12 {
227 pub(crate) padding: Padding = Padding::Zero,
228 }
229}
230
231to_tokens! {
232 pub(crate) struct Hour24 {
233 pub(crate) padding: Padding = Padding::Zero,
234 }
235}
236
237to_tokens! {
238 pub(crate) struct Minute {
239 pub(crate) padding: Padding = Padding::Zero,
240 }
241}
242
243to_tokens! {
244 pub(crate) struct Period {
245 pub(crate) is_uppercase: bool = true,
246 pub(crate) case_sensitive: bool = true,
247 }
248}
249
250to_tokens! {
251 pub(crate) struct Second {
252 pub(crate) padding: Padding = Padding::Zero,
253 }
254}
255
256to_tokens! {
257 pub(crate) enum SubsecondDigits {
258 One,
259 Two,
260 Three,
261 Four,
262 Five,
263 Six,
264 Seven,
265 Eight,
266 Nine,
267 OneOrMore,
268 }
269}
270
271to_tokens! {
272 pub(crate) struct Subsecond {
273 pub(crate) digits: SubsecondDigits = SubsecondDigits::OneOrMore,
274 }
275}
276
277to_tokens! {
278 pub(crate) struct OffsetHour {
279 pub(crate) sign_is_mandatory: bool = false,
280 pub(crate) padding: Padding = Padding::Zero,
281 }
282}
283
284to_tokens! {
285 pub(crate) struct OffsetMinute {
286 pub(crate) padding: Padding = Padding::Zero,
287 }
288}
289
290to_tokens! {
291 pub(crate) struct OffsetSecond {
292 pub(crate) padding: Padding = Padding::Zero,
293 }
294}
295
296to_tokens! {
297 pub(crate) enum Padding {
298 Space,
299 Zero,
300 None,
301 }
302}
303
304#[derive(Clone, Copy)]
305pub(crate) struct Ignore {
306 pub(crate) count: NonZero<u16>,
307}
308
309impl ToTokenTree for Ignore {
310 fn into_token_tree(self) -> TokenTree {
311 quote_group! {{
312 Ignore::count(#(self.count))
313 }}
314 }
315}
316
317to_tokens! {
318 pub(crate) struct UnixTimestampSecond {
319 pub(crate) sign_is_mandatory: bool = false,
320 }
321}
322
323to_tokens! {
324 pub(crate) struct UnixTimestampMillisecond {
325 pub(crate) sign_is_mandatory: bool = false,
326 }
327}
328
329to_tokens! {
330 pub(crate) struct UnixTimestampMicrosecond {
331 pub(crate) sign_is_mandatory: bool = false,
332 }
333}
334
335to_tokens! {
336 pub(crate) struct UnixTimestampNanosecond {
337 pub(crate) sign_is_mandatory: bool = false,
338 }
339}
340
341to_tokens! {
342 pub(crate) enum TrailingInput {
343 Prohibit,
344 Discard,
345 }
346}
347
348to_tokens! {
349 pub(crate) struct End {
350 pub(crate) trailing_input: TrailingInput = TrailingInput::Prohibit,
351 }
352}