Skip to main content

flexible_time/timestamp/
parsed.rs

1use crate::error::ParsingError;
2use crate::{error::ParsedError, primitives::*};
3use std::num::NonZeroU8;
4use std::str::FromStr;
5use time::Month;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8pub enum ParsedTimestamp {
9    Year(i32),
10    YearMonth(i32, Month),
11    YearMonthDay(i32, Month, NonZeroU8),
12
13    YearMonthDayHour(i32, Month, NonZeroU8, u8),
14    YearMonthDayHourMinute(i32, Month, NonZeroU8, u8, u8),
15    YearMonthDayHourMinuteSecond(i32, Month, NonZeroU8, u8, u8, u8),
16}
17
18impl FromStr for ParsedTimestamp {
19    type Err = ParsedError;
20
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        let s = s.as_bytes();
23
24        // we start out with a random error, just to keep the pattern
25        Err(ParsingError::MissingInformation)
26            .or_else(|_| parse_year_month_day_hour_minute_second(s))
27            .or_else(|_| parse_year_month_day_hour_minute(s))
28            .or_else(|_| parse_year_month_day_hour(s))
29            .or_else(|_| parse_year_month_day(s))
30            .or_else(|_| parse_year_month(s))
31            .or_else(|_| parse_year(s))
32            .or(Err(ParsedError::UnknownFormat))
33    }
34}