From 2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 14:47:55 +0200 Subject: Adding upstream version 0.70.1+ds1. Signed-off-by: Daniel Baumann --- vendor/time-macros/src/datetime.rs | 57 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 vendor/time-macros/src/datetime.rs (limited to 'vendor/time-macros/src/datetime.rs') diff --git a/vendor/time-macros/src/datetime.rs b/vendor/time-macros/src/datetime.rs new file mode 100644 index 0000000..2d41e9a --- /dev/null +++ b/vendor/time-macros/src/datetime.rs @@ -0,0 +1,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, +} + +pub(crate) fn parse(chars: &mut Peekable) -> Result { + 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 + }} + } +} -- cgit v1.2.3