diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/rustversion/src/date.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/rustversion/src/date.rs')
-rw-r--r-- | third_party/rust/rustversion/src/date.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/third_party/rust/rustversion/src/date.rs b/third_party/rust/rustversion/src/date.rs new file mode 100644 index 0000000000..5cdb691da6 --- /dev/null +++ b/third_party/rust/rustversion/src/date.rs @@ -0,0 +1,50 @@ +use crate::error::{Error, Result}; +use crate::iter::Iter; +use crate::{time, token}; +use proc_macro::Group; +use std::fmt::{self, Display}; + +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Date { + pub year: u16, + pub month: u8, + pub day: u8, +} + +impl Display for Date { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!( + formatter, + "{:04}-{:02}-{:02}", + self.year, self.month, self.day, + ) + } +} + +pub fn parse(paren: Group, iter: Iter) -> Result<Date> { + try_parse(iter).map_err(|_| { + let msg = format!("expected nightly date, like {}", time::today()); + Error::group(paren, msg) + }) +} + +fn try_parse(iter: Iter) -> Result<Date, ()> { + let year = token::parse_literal(iter).map_err(drop)?; + token::parse_punct(iter, '-').map_err(drop)?; + let month = token::parse_literal(iter).map_err(drop)?; + token::parse_punct(iter, '-').map_err(drop)?; + let day = token::parse_literal(iter).map_err(drop)?; + + let year = year.to_string().parse::<u64>().map_err(drop)?; + let month = month.to_string().parse::<u64>().map_err(drop)?; + let day = day.to_string().parse::<u64>().map_err(drop)?; + if year >= 3000 || month > 12 || day > 31 { + return Err(()); + } + + Ok(Date { + year: year as u16, + month: month as u8, + day: day as u8, + }) +} |