1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::iter::Peekable;

use proc_macro::{token_stream, Ident, Span, TokenTree};

use crate::date::Date;
use crate::error::Error;
use crate::offset::Offset;
use crate::time::Time;
use crate::to_tokens::ToTokenTree;
use crate::{date, offset, time};

pub(crate) struct DateTime {
    date: Date,
    time: Time,
    offset: Option<Offset>,
}

pub(crate) fn parse(chars: &mut Peekable<token_stream::IntoIter>) -> Result<DateTime, Error> {
    let date = date::parse(chars)?;
    let time = time::parse(chars)?;
    let offset = match offset::parse(chars) {
        Ok(offset) => Some(offset),
        Err(Error::UnexpectedEndOfInput | Error::MissingComponent { name: "sign", .. }) => None,
        Err(err) => return Err(err),
    };

    if let Some(token) = chars.peek() {
        return Err(Error::UnexpectedToken {
            tree: token.clone(),
        });
    }

    Ok(DateTime { date, time, offset })
}

impl ToTokenTree for DateTime {
    fn into_token_tree(self) -> TokenTree {
        let (type_name, maybe_offset) = match self.offset {
            Some(offset) => (
                Ident::new("OffsetDateTime", Span::mixed_site()),
                quote!(.assume_offset(#(offset))),
            ),
            None => (
                Ident::new("PrimitiveDateTime", Span::mixed_site()),
                quote!(),
            ),
        };

        quote_group! {{
            const DATE_TIME: ::time::#(type_name) = ::time::PrimitiveDateTime::new(
                #(self.date),
                #(self.time),
            ) #S(maybe_offset);
            DATE_TIME
        }}
    }
}