Skip to main content

zoneinfo_rs/types/
rule.rs

1//! Types used to represent a Rule line in a zoneinfo file.
2
3use alloc::borrow::ToOwned;
4
5use crate::parser::{ContextParse, LineParseContext, TryFromStr, ZoneInfoParseError};
6
7/// The value present in the `TO` column of a rule line.
8///
9/// This value can either be "max" or an unsigned integer representing the year.
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub enum ToYear {
12    Max,
13    Year(u16),
14}
15
16impl ToYear {
17    pub(crate) fn parse_optional_to_year(
18        s: &str,
19        ctx: &mut LineParseContext,
20    ) -> Result<Option<ToYear>, ZoneInfoParseError> {
21        if s == "only" {
22            Ok(None)
23        } else {
24            s.context_parse::<ToYear>(ctx).map(Some)
25        }
26    }
27
28    pub(crate) fn to_i32(self) -> i32 {
29        match self {
30            Self::Max => 275_760,
31            Self::Year(y) => y as i32,
32        }
33    }
34
35    pub(crate) fn to_optional_u16(self) -> Option<u16> {
36        match self {
37            Self::Max => None,
38            Self::Year(y) => Some(y),
39        }
40    }
41}
42
43impl TryFromStr<LineParseContext> for ToYear {
44    type Error = ZoneInfoParseError;
45
46    fn try_from_str(s: &str, ctx: &mut LineParseContext) -> Result<Self, Self::Error> {
47        if s == "max" {
48            return Ok(ToYear::Max);
49        }
50        s.context_parse::<u16>(ctx).map(ToYear::Year)
51    }
52}
53
54/// The day of the month as listed by the `ON` column of a rule line.
55///
56/// The values can be a day, a GE or LE identifier (Sun>=8), or "lastSun", which
57/// represents the last sunday of the month.
58#[derive(Debug, Clone, Copy, PartialEq)]
59pub enum DayOfMonth {
60    // Again, hacky default. Not a fan
61    Last(WeekDay),
62    WeekDayGEThanMonthDay(WeekDay, u8),
63    // Potentially, depracated
64    WeekDayLEThanMonthDay(WeekDay, u8),
65    Day(u8),
66}
67
68impl TryFromStr<LineParseContext> for DayOfMonth {
69    type Error = ZoneInfoParseError;
70    fn try_from_str(s: &str, ctx: &mut LineParseContext) -> Result<Self, Self::Error> {
71        ctx.enter("DayOfMonth");
72        let result = if let Some(weekday) = s.strip_prefix("last") {
73            Ok(DayOfMonth::Last(weekday.context_parse(ctx)?))
74        } else if s.contains(">=") {
75            let (week_day, day) = parse_date_split(s, ">=", ctx)?;
76            Ok(DayOfMonth::WeekDayGEThanMonthDay(week_day, day))
77        } else if s.contains("<=") {
78            let (week_day, day) = parse_date_split(s, "<=", ctx)?;
79            Ok(DayOfMonth::WeekDayLEThanMonthDay(week_day, day))
80        } else {
81            s.context_parse(ctx).map(DayOfMonth::Day)
82        };
83        ctx.exit();
84        result
85    }
86}
87
88fn parse_date_split(
89    s: &str,
90    pat: &str,
91    ctx: &mut LineParseContext,
92) -> Result<(WeekDay, u8), ZoneInfoParseError> {
93    let (week_day, num) = s
94        .split_once(pat)
95        .ok_or(ZoneInfoParseError::unknown(s, ctx))?;
96    let w = week_day.context_parse::<WeekDay>(ctx)?;
97    let d = num.context_parse(ctx)?;
98    Ok((w, d))
99}
100
101/// A week day value, this is used in the `ON` column values.
102///
103/// NOTE: week days are zero based beginning with Sunday.
104#[derive(Debug, Clone, Copy, PartialEq)]
105#[repr(u8)]
106pub enum WeekDay {
107    Sun = 0,
108    Mon,
109    Tues,
110    Wed,
111    Thurs,
112    Fri,
113    Sat,
114}
115
116impl WeekDay {
117    pub(crate) fn from_u8(value: u8) -> Self {
118        match value {
119            0 => Self::Sun,
120            1 => Self::Mon,
121            2 => Self::Tues,
122            3 => Self::Wed,
123            4 => Self::Thurs,
124            5 => Self::Fri,
125            6 => Self::Sat,
126            _ => unreachable!("invalid week day value"),
127        }
128    }
129}
130
131impl TryFromStr<LineParseContext> for WeekDay {
132    type Error = ZoneInfoParseError;
133    fn try_from_str(s: &str, ctx: &mut LineParseContext) -> Result<Self, Self::Error> {
134        match s {
135            "Mon" => Ok(Self::Mon),
136            "Tues" => Ok(Self::Tues),
137            "Wed" => Ok(Self::Wed),
138            "Thu" => Ok(Self::Thurs),
139            "Fri" => Ok(Self::Fri),
140            "Sat" => Ok(Self::Sat),
141            "Sun" => Ok(Self::Sun),
142            _ => Err(ZoneInfoParseError::UnknownValue(
143                ctx.line_number,
144                s.to_owned(),
145            )),
146        }
147    }
148}