time_macros/
utc_datetime.rs

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
use std::iter::Peekable;

use proc_macro::{token_stream, TokenTree};

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

pub(crate) struct UtcDateTime {
    date: Date,
    time: Time,
}

pub(crate) fn parse(chars: &mut Peekable<token_stream::IntoIter>) -> Result<UtcDateTime, Error> {
    let date = date::parse(chars)?;
    let time = time::parse(chars)?;

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

    Ok(UtcDateTime { date, time })
}

impl ToTokenTree for UtcDateTime {
    fn into_token_tree(self) -> TokenTree {
        quote_group! {{
            const DATE_TIME: ::time::UtcDateTime = ::time::UtcDateTime::new(
                #(self.date),
                #(self.time),
            );
            DATE_TIME
        }}
    }
}