zoneinfo_rs/types/
rule.rs1use alloc::borrow::ToOwned;
4
5use crate::parser::{ContextParse, LineParseContext, TryFromStr, ZoneInfoParseError};
6
7#[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#[derive(Debug, Clone, Copy, PartialEq)]
59pub enum DayOfMonth {
60 Last(WeekDay),
62 WeekDayGEThanMonthDay(WeekDay, u8),
63 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#[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}