Skip to main content

time_macros/
lib.rs

1#![allow(
2    clippy::missing_const_for_fn,
3    clippy::std_instead_of_core,
4    clippy::std_instead_of_alloc,
5    clippy::alloc_instead_of_core,
6    reason = "irrelevant for proc macros"
7)]
8#![allow(
9    clippy::missing_docs_in_private_items,
10    missing_docs,
11    reason = "may be removed eventually"
12)]
13
14#[allow(
15    unused_macros,
16    reason = "may not be used for all feature flag combinations"
17)]
18macro_rules! bug {
19    () => { compile_error!("provide an error message to help fix a possible bug") };
20    ($descr:literal $($rest:tt)?) => {
21        unreachable!(concat!("internal error: ", $descr) $($rest)?)
22    }
23}
24
25#[macro_use]
26mod quote;
27
28mod date;
29mod datetime;
30mod error;
31#[cfg(any(feature = "formatting", feature = "parsing"))]
32mod format_description;
33mod helpers;
34mod offset;
35#[cfg(all(feature = "serde", any(feature = "formatting", feature = "parsing")))]
36mod serde_format_description;
37mod time;
38mod timestamp;
39mod to_tokens;
40mod utc_datetime;
41
42#[cfg(any(feature = "formatting", feature = "parsing"))]
43use std::iter::Peekable;
44
45#[cfg(all(feature = "serde", any(feature = "formatting", feature = "parsing")))]
46use proc_macro::Delimiter;
47use proc_macro::TokenStream;
48#[cfg(any(feature = "formatting", feature = "parsing"))]
49use proc_macro::TokenTree;
50
51use self::error::Error;
52
53macro_rules! impl_macros {
54    ($($name:ident)*) => {$(
55        #[proc_macro]
56        pub fn $name(input: TokenStream) -> TokenStream {
57            use crate::to_tokens::ToTokenStream;
58
59            let mut iter = input.into_iter().peekable();
60            match $name::parse(&mut iter) {
61                Ok(value) => match iter.peek() {
62                    Some(tree) => Error::UnexpectedToken { tree: tree.clone() }.to_compile_error(),
63                    None => quote_! { const { #S(value.into_token_stream()) } },
64                },
65                Err(err) => err.to_compile_error(),
66            }
67        }
68    )*};
69}
70
71impl_macros![date datetime utc_datetime offset time timestamp];
72
73#[cfg(any(feature = "formatting", feature = "parsing"))]
74type PeekableTokenStreamIter = Peekable<proc_macro::token_stream::IntoIter>;
75
76#[cfg(any(feature = "formatting", feature = "parsing"))]
77#[derive(Clone, Copy)]
78enum FormatDescriptionVersion {
79    V1,
80    V2,
81    V3,
82}
83
84#[cfg(any(feature = "formatting", feature = "parsing"))]
85impl FormatDescriptionVersion {
86    fn is_v1(self) -> bool {
87        match self {
88            Self::V1 => true,
89            Self::V2 | Self::V3 => false,
90        }
91    }
92
93    fn is_at_most_v2(self) -> bool {
94        match self {
95            Self::V1 | Self::V2 => true,
96            Self::V3 => false,
97        }
98    }
99
100    fn is_at_least_v2(self) -> bool {
101        match self {
102            Self::V1 => false,
103            Self::V2 | Self::V3 => true,
104        }
105    }
106
107    fn is_at_least_v3(self) -> bool {
108        match self {
109            Self::V1 | Self::V2 => false,
110            Self::V3 => true,
111        }
112    }
113}
114
115#[cfg(any(feature = "formatting", feature = "parsing"))]
116fn parse_format_description_version<const NO_EQUALS_IS_MOD_NAME: bool>(
117    iter: &mut PeekableTokenStreamIter,
118) -> Result<Option<FormatDescriptionVersion>, Error> {
119    let end_of_input_err = || {
120        if NO_EQUALS_IS_MOD_NAME {
121            Error::UnexpectedEndOfInput
122        } else {
123            Error::ExpectedString {
124                span_start: None,
125                span_end: None,
126            }
127        }
128    };
129    let version_ident = match iter.peek().ok_or_else(end_of_input_err)? {
130        version @ TokenTree::Ident(ident) if ident.to_string() == "version" => {
131            let version_ident = version.clone();
132            iter.next(); // consume `version`
133            version_ident
134        }
135        _ => return Ok(None),
136    };
137
138    match iter.peek() {
139        Some(TokenTree::Punct(punct)) if punct.as_char() == '=' => iter.next(),
140        _ if NO_EQUALS_IS_MOD_NAME => {
141            // Push the `version` ident to the front of the iterator.
142            *iter = std::iter::once(version_ident)
143                .chain(iter.clone())
144                .collect::<TokenStream>()
145                .into_iter()
146                .peekable();
147            return Ok(None);
148        }
149        Some(token) => {
150            return Err(Error::Custom {
151                message: "expected `=`".into(),
152                span_start: Some(token.span()),
153                span_end: Some(token.span()),
154            });
155        }
156        None => {
157            return Err(Error::Custom {
158                message: "expected `=`".into(),
159                span_start: None,
160                span_end: None,
161            });
162        }
163    };
164    let version_literal = match iter.next() {
165        Some(TokenTree::Literal(literal)) => literal,
166        Some(token) => {
167            return Err(Error::Custom {
168                message: "expected 1, 2, or 3".into(),
169                span_start: Some(token.span()),
170                span_end: Some(token.span()),
171            });
172        }
173        None => {
174            return Err(Error::Custom {
175                message: "expected 1, 2, or 3".into(),
176                span_start: None,
177                span_end: None,
178            });
179        }
180    };
181    let version = match version_literal.to_string().as_str() {
182        "1" => FormatDescriptionVersion::V1,
183        "2" => FormatDescriptionVersion::V2,
184        "3" => FormatDescriptionVersion::V3,
185        _ => {
186            return Err(Error::Custom {
187                message: "invalid format description version".into(),
188                span_start: Some(version_literal.span()),
189                span_end: Some(version_literal.span()),
190            });
191        }
192    };
193    helpers::consume_punct(',', iter)?;
194
195    Ok(Some(version))
196}
197
198#[cfg(all(feature = "serde", any(feature = "formatting", feature = "parsing")))]
199fn parse_visibility(iter: &mut PeekableTokenStreamIter) -> Result<TokenStream, Error> {
200    let mut visibility = match iter.peek().ok_or(Error::UnexpectedEndOfInput)? {
201        pub_ident @ TokenTree::Ident(ident) if ident.to_string() == "pub" => {
202            let visibility = quote_! { #(pub_ident.clone()) };
203            iter.next(); // consume `pub`
204            visibility
205        }
206        _ => return Ok(quote_! {}),
207    };
208
209    match iter.peek().ok_or(Error::UnexpectedEndOfInput)? {
210        group @ TokenTree::Group(path) if path.delimiter() == Delimiter::Parenthesis => {
211            visibility.extend(std::iter::once(group.clone()));
212            iter.next(); // consume parentheses and path
213        }
214        _ => {}
215    }
216
217    Ok(visibility)
218}
219
220#[cfg(any(feature = "formatting", feature = "parsing"))]
221fn expand_format_description(item: format_description::public::OwnedFormatItem) -> TokenStream {
222    match item.version {
223        FormatDescriptionVersion::V1 | FormatDescriptionVersion::V2 => match &item.inner {
224            format_description::public::OwnedFormatItemInner::Compound(items) => {
225                let items = items
226                    .iter()
227                    .map(|inner_item| {
228                        quote_! {
229                            #S(format_description::public::OwnedFormatItem {
230                                version: item.version,
231                                inner: inner_item.clone(),
232                            }),
233                        }
234                    })
235                    .collect::<TokenStream>();
236                quote_! {
237                    const {
238                        use ::time::format_description::{*, modifier::*};
239                        &[#S(items)] as StaticFormatDescription
240                    }
241                }
242            }
243            _ => quote_! {
244                const {
245                    use ::time::format_description::{*, modifier::*};
246                    &[#S(item)] as StaticFormatDescription
247                }
248            },
249        },
250        FormatDescriptionVersion::V3 => quote_! {
251            const {
252                use ::time::format_description::__private::*;
253                use ::time::format_description::modifier::*;
254                #S(item).into_opaque()
255            }
256        },
257    }
258}
259
260#[cfg(any(feature = "formatting", feature = "parsing"))]
261#[proc_macro]
262pub fn format_description(input: TokenStream) -> TokenStream {
263    (|| {
264        let mut input = input.into_iter().peekable();
265        let version = parse_format_description_version::<false>(&mut input)?
266            .unwrap_or(FormatDescriptionVersion::V1);
267        let (span, string) = helpers::get_string_literal(version.is_at_most_v2(), input)?;
268        let items = format_description::parse_with_version(version, &string, span)?;
269
270        Ok(expand_format_description(items))
271    })()
272    .unwrap_or_else(|err: Error| err.to_compile_error())
273}
274
275#[cfg(all(feature = "serde", any(feature = "formatting", feature = "parsing")))]
276#[proc_macro]
277pub fn serde_format_description(input: TokenStream) -> TokenStream {
278    (|| {
279        let mut tokens = input.into_iter().peekable();
280
281        // First, the optional format description version.
282        let maybe_version = parse_format_description_version::<true>(&mut tokens)?;
283
284        // Then, the visibility of the module.
285        let visibility = parse_visibility(&mut tokens)?;
286
287        // If no explicit version was provided, allow `mod` (possibly after visibility) to imply v3
288        // syntax: `mod foo [Type] = ...`. Otherwise fall back to v1 for backwards compatibility.
289        let version = if let Some(version) = maybe_version {
290            version
291        } else if maybe_version.is_none()
292            && let Some(TokenTree::Ident(ident)) = tokens.peek()
293            && ident.to_string() == "mod"
294        {
295            FormatDescriptionVersion::V3
296        } else {
297            FormatDescriptionVersion::V1
298        };
299
300        let (mod_name, ty) = match version {
301            FormatDescriptionVersion::V1 | FormatDescriptionVersion::V2 => {
302                // Next, an identifier (the desired module name)
303                let mod_name = match tokens.next() {
304                    Some(TokenTree::Ident(ident)) => Ok(ident),
305                    Some(tree) => Err(Error::UnexpectedToken { tree }),
306                    None => Err(Error::UnexpectedEndOfInput),
307                }?;
308
309                // Followed by a comma
310                helpers::consume_punct(',', &mut tokens)?;
311
312                // Then, the type to create serde serializers for (e.g., `OffsetDateTime`).
313                let ty = match tokens.next() {
314                    Some(tree @ TokenTree::Ident(_)) => Ok(tree.into()),
315                    Some(tree) => Err(Error::UnexpectedToken { tree }),
316                    None => Err(Error::UnexpectedEndOfInput),
317                }?;
318
319                // Another comma
320                helpers::consume_punct(',', &mut tokens)?;
321
322                (mod_name, ty)
323            }
324            FormatDescriptionVersion::V3 => {
325                // Next, the `mod` token.
326                match tokens.next() {
327                    Some(TokenTree::Ident(ident)) if ident.to_string() == "mod" => {}
328                    Some(tree) => {
329                        return Err(Error::Custom {
330                            message: "expected `mod`".into(),
331                            span_start: Some(tree.span().start()),
332                            span_end: Some(tree.span().end()),
333                        });
334                    }
335                    None => return Err(Error::UnexpectedEndOfInput),
336                };
337
338                // Next, an identifier (the desired module name)
339                let mod_name = match tokens.next() {
340                    Some(TokenTree::Ident(ident)) => Ok(ident),
341                    Some(tree) => Err(Error::UnexpectedToken { tree }),
342                    None => Err(Error::UnexpectedEndOfInput),
343                }?;
344
345                // Followed by the type to create implementations for, placed in brackets.
346                let ty = match tokens.next() {
347                    Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Bracket => {
348                        Ok(group.stream())
349                    }
350                    Some(tree) => Err(Error::UnexpectedToken { tree }),
351                    None => Err(Error::UnexpectedEndOfInput),
352                }?;
353
354                // Then an equals sign
355                helpers::consume_punct('=', &mut tokens)?;
356
357                (mod_name, ty)
358            }
359        };
360
361        // We now have two options. The user can either provide a format description as a
362        // string or they can provide a path to a format description. If the
363        // latter, all remaining tokens are assumed to be part of the path.
364        let (format, format_description_display) = match tokens.peek() {
365            // string literal
366            Some(TokenTree::Literal(_)) => {
367                let (span, format_string) =
368                    helpers::get_string_literal(version.is_at_most_v2(), tokens)?;
369                let items = format_description::parse_with_version(version, &format_string, span)?;
370                let items = expand_format_description(items);
371
372                (items, String::from_utf8_lossy(&format_string).into_owned())
373            }
374            // path
375            Some(_) => {
376                let tokens = tokens.collect::<TokenStream>();
377                let tokens_string = tokens.to_string();
378                (tokens, tokens_string)
379            }
380            None => return Err(Error::UnexpectedEndOfInput),
381        };
382
383        Ok(serde_format_description::build(
384            version,
385            visibility,
386            mod_name,
387            ty,
388            format,
389            format_description_display,
390        ))
391    })()
392    .unwrap_or_else(|err: Error| err.to_compile_error_standalone())
393}