| 1 | use std::{collections::HashMap, marker::PhantomData, str::FromStr}; |
| 2 | |
| 3 | use chrono::Weekday; |
| 4 | |
| 5 | use crate::{ |
| 6 | parser::{ |
| 7 | content_line::parameters::parse_parameters, |
| 8 | datetime::{datestring_to_date, parse_weekdays}, |
| 9 | str_to_weekday, |
| 10 | utils::parse_str_to_vec, |
| 11 | ParseError, |
| 12 | }, |
| 13 | Frequency, RRule, Unvalidated, |
| 14 | }; |
| 15 | |
| 16 | use super::content_line_parts::ContentLineCaptures; |
| 17 | |
| 18 | #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] |
| 19 | pub enum RRuleProperty { |
| 20 | Freq, |
| 21 | Until, |
| 22 | Count, |
| 23 | Interval, |
| 24 | BySecond, |
| 25 | ByMinute, |
| 26 | ByHour, |
| 27 | ByDay, |
| 28 | ByMonthDay, |
| 29 | ByYearDay, |
| 30 | ByWeekNo, |
| 31 | ByMonth, |
| 32 | BySetPos, |
| 33 | Wkst, |
| 34 | #[cfg(feature = "by-easter")] |
| 35 | ByEaster, |
| 36 | } |
| 37 | |
| 38 | impl FromStr for RRuleProperty { |
| 39 | type Err = ParseError; |
| 40 | |
| 41 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 42 | let prop = match &s.to_uppercase()[..] { |
| 43 | "FREQ" => Self::Freq, |
| 44 | "UNTIL" => Self::Until, |
| 45 | "COUNT" => Self::Count, |
| 46 | "INTERVAL" => Self::Interval, |
| 47 | "BYSECOND" => Self::BySecond, |
| 48 | "BYMINUTE" => Self::ByMinute, |
| 49 | "BYHOUR" => Self::ByHour, |
| 50 | "BYWEEKDAY" | "BYDAY" => Self::ByDay, |
| 51 | "BYMONTHDAY" => Self::ByMonthDay, |
| 52 | "BYYEARDAY" => Self::ByYearDay, |
| 53 | "BYWEEKNO" => Self::ByWeekNo, |
| 54 | "BYMONTH" => Self::ByMonth, |
| 55 | "BYSETPOS" => Self::BySetPos, |
| 56 | "WKST" => Self::Wkst, |
| 57 | #[cfg(feature = "by-easter")] |
| 58 | "BYEASTER" => Self::ByEaster, |
| 59 | _ => return Err(ParseError::UnrecognizedParameter(s.into())), |
| 60 | }; |
| 61 | Ok(prop) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl TryFrom<ContentLineCaptures<'_>> for RRule<Unvalidated> { |
| 66 | type Error = ParseError; |
| 67 | |
| 68 | fn try_from(value: ContentLineCaptures) -> Result<Self, Self::Error> { |
| 69 | if let Some(parameters) = value.parameters { |
| 70 | if !parameters.is_empty() { |
| 71 | return Err(ParseError::PropertyParametersNotSupported( |
| 72 | parameters.into(), |
| 73 | )); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | let properties: HashMap<RRuleProperty, String> = parse_parameters(value.value)?; |
| 78 | |
| 79 | props_to_rrule(&properties) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Takes a map of [`RRuleProperty`] and returns an [`RRule`]. |
| 84 | #[allow(clippy::too_many_lines)] |
| 85 | fn props_to_rrule( |
| 86 | props: &HashMap<RRuleProperty, String>, |
| 87 | ) -> Result<RRule<Unvalidated>, ParseError> { |
| 88 | let freq = props |
| 89 | .get(&RRuleProperty::Freq) |
| 90 | .map(|freq| Frequency::from_str(freq)) |
| 91 | .transpose()? |
| 92 | .ok_or_else(|| ParseError::MissingProperty("FREQ".into()))?; |
| 93 | let interval = props |
| 94 | .get(&RRuleProperty::Interval) |
| 95 | .map(|interval| { |
| 96 | interval |
| 97 | .parse() |
| 98 | .map_err(|_| ParseError::InvalidInterval(interval.into())) |
| 99 | }) |
| 100 | .transpose()? |
| 101 | .unwrap_or(1); |
| 102 | let count = props |
| 103 | .get(&RRuleProperty::Count) |
| 104 | .map(|count| { |
| 105 | count |
| 106 | .parse() |
| 107 | .map_err(|_| ParseError::InvalidCount(count.into())) |
| 108 | }) |
| 109 | .transpose()?; |
| 110 | let until = props |
| 111 | .get(&RRuleProperty::Until) |
| 112 | .map(|until| datestring_to_date(until, None, "UNTIL")) |
| 113 | .transpose()?; |
| 114 | let week_start = props |
| 115 | .get(&RRuleProperty::Wkst) |
| 116 | .map(|week_start| { |
| 117 | str_to_weekday(week_start) |
| 118 | .map_err(|_| ParseError::InvalidWeekdayStart(week_start.into())) |
| 119 | }) |
| 120 | .transpose()? |
| 121 | .unwrap_or(Weekday::Mon); |
| 122 | let by_set_pos = props |
| 123 | .get(&RRuleProperty::BySetPos) |
| 124 | .map(|by_set_pos| { |
| 125 | parse_str_to_vec(by_set_pos, |_| true) |
| 126 | .map_err(|_| ParseError::InvalidBySetPos(by_set_pos.into())) |
| 127 | }) |
| 128 | .transpose()? |
| 129 | .unwrap_or_default(); |
| 130 | let by_month = props |
| 131 | .get(&RRuleProperty::ByMonth) |
| 132 | .map(|by_month| { |
| 133 | parse_str_to_vec(by_month, |month| (1..=12).contains(&month)) |
| 134 | .map_err(|_| ParseError::InvalidByMonth(by_month.into())) |
| 135 | }) |
| 136 | .transpose()? |
| 137 | .unwrap_or_default(); |
| 138 | let by_month_day = props |
| 139 | .get(&RRuleProperty::ByMonthDay) |
| 140 | .map(|by_month_day| { |
| 141 | parse_str_to_vec(by_month_day, |monthday| (-31..=31).contains(&monthday)) |
| 142 | .map_err(|_| ParseError::InvalidByMonthDay(by_month_day.into())) |
| 143 | }) |
| 144 | .transpose()? |
| 145 | .unwrap_or_default(); |
| 146 | let by_year_day = props |
| 147 | .get(&RRuleProperty::ByYearDay) |
| 148 | .map(|by_year_day| { |
| 149 | parse_str_to_vec(by_year_day, |yearday| (-366..=366).contains(&yearday)) |
| 150 | .map_err(|_| ParseError::InvalidByYearDay(by_year_day.into())) |
| 151 | }) |
| 152 | .transpose()? |
| 153 | .unwrap_or_default(); |
| 154 | let by_week_no = props |
| 155 | .get(&RRuleProperty::ByWeekNo) |
| 156 | .map(|by_week_no| { |
| 157 | parse_str_to_vec(by_week_no, |weekno| (-53..=53).contains(&weekno)) |
| 158 | .map_err(|_| ParseError::InvalidByWeekNo(by_week_no.into())) |
| 159 | }) |
| 160 | .transpose()? |
| 161 | .unwrap_or_default(); |
| 162 | let by_weekday = props |
| 163 | .get(&RRuleProperty::ByDay) |
| 164 | .map(|by_weekday| parse_weekdays(by_weekday)) |
| 165 | .transpose()? |
| 166 | .unwrap_or_default(); |
| 167 | let by_hour = props |
| 168 | .get(&RRuleProperty::ByHour) |
| 169 | .map(|by_hour| { |
| 170 | parse_str_to_vec(by_hour, |hour| hour < 24) |
| 171 | .map_err(|_| ParseError::InvalidByHour(by_hour.into())) |
| 172 | }) |
| 173 | .transpose()? |
| 174 | .unwrap_or_default(); |
| 175 | let by_minute = props |
| 176 | .get(&RRuleProperty::ByMinute) |
| 177 | .map(|by_minute| { |
| 178 | parse_str_to_vec(by_minute, |minute| minute < 60) |
| 179 | .map_err(|_| ParseError::InvalidByMinute(by_minute.into())) |
| 180 | }) |
| 181 | .transpose()? |
| 182 | .unwrap_or_default(); |
| 183 | let by_second = props |
| 184 | .get(&RRuleProperty::BySecond) |
| 185 | .map(|by_second| { |
| 186 | parse_str_to_vec(by_second, |second| second < 60) |
| 187 | .map_err(|_| ParseError::InvalidBySecond(by_second.into())) |
| 188 | }) |
| 189 | .transpose()? |
| 190 | .unwrap_or_default(); |
| 191 | |
| 192 | #[cfg(not(feature = "by-easter"))] |
| 193 | let by_easter = None; |
| 194 | #[cfg(feature = "by-easter")] |
| 195 | let by_easter = props |
| 196 | .get(&RRuleProperty::ByEaster) |
| 197 | .map(|new_by_easter: &String| { |
| 198 | i16::from_str(new_by_easter) |
| 199 | .map_err(|_| ParseError::InvalidByEaster(new_by_easter.into())) |
| 200 | }) |
| 201 | .transpose()?; |
| 202 | |
| 203 | // Check if mandatory fields are set |
| 204 | Ok(RRule { |
| 205 | freq, |
| 206 | interval, |
| 207 | count, |
| 208 | until, |
| 209 | week_start, |
| 210 | by_set_pos, |
| 211 | by_month, |
| 212 | by_month_day, |
| 213 | by_n_month_day: vec![], |
| 214 | by_year_day, |
| 215 | by_week_no, |
| 216 | by_weekday, |
| 217 | by_hour, |
| 218 | by_minute, |
| 219 | by_second, |
| 220 | by_easter, |
| 221 | stage: PhantomData, |
| 222 | }) |
| 223 | } |
| 224 | |
| 225 | #[cfg(test)] |
| 226 | mod tests { |
| 227 | use crate::parser::content_line::{ContentLineCaptures, PropertyName}; |
| 228 | |
| 229 | use super::*; |
| 230 | |
| 231 | #[test] |
| 232 | fn parses_rrule_content_line() { |
| 233 | let tests = [ |
| 234 | ( |
| 235 | ContentLineCaptures { |
| 236 | property_name: PropertyName::RRule, |
| 237 | parameters: None, |
| 238 | value: "FREQ=DAILY", |
| 239 | }, |
| 240 | RRule { |
| 241 | freq: Frequency::Daily, |
| 242 | ..Default::default() |
| 243 | }, |
| 244 | ), |
| 245 | ( |
| 246 | ContentLineCaptures { |
| 247 | property_name: PropertyName::RRule, |
| 248 | parameters: None, |
| 249 | value: "BYHOUR=4;FREQ=DAILY", |
| 250 | }, |
| 251 | RRule { |
| 252 | by_hour: vec![4], |
| 253 | freq: Frequency::Daily, |
| 254 | ..Default::default() |
| 255 | }, |
| 256 | ), |
| 257 | ( |
| 258 | ContentLineCaptures { |
| 259 | property_name: PropertyName::RRule, |
| 260 | parameters: None, |
| 261 | // Testing case insensitivity |
| 262 | value: "byhour=4;freQ=DAILY", |
| 263 | }, |
| 264 | RRule { |
| 265 | by_hour: vec![4], |
| 266 | freq: Frequency::Daily, |
| 267 | ..Default::default() |
| 268 | }, |
| 269 | ), |
| 270 | ]; |
| 271 | |
| 272 | for (input, expected_output) in tests { |
| 273 | let output = RRule::try_from(input); |
| 274 | assert_eq!(output, Ok(expected_output)); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn rejects_property_parameters_in_rrule_line() { |
| 280 | let tests = [( |
| 281 | ContentLineCaptures { |
| 282 | property_name: PropertyName::RRule, |
| 283 | parameters: Some("TZID=Europe/London"), |
| 284 | value: "BYHOUR=4", |
| 285 | }, |
| 286 | ParseError::PropertyParametersNotSupported("TZID=Europe/London".into()), |
| 287 | )]; |
| 288 | |
| 289 | for (input, expected_output) in tests { |
| 290 | let output = RRule::try_from(input); |
| 291 | assert_eq!(output, Err(expected_output)); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | #[test] |
| 296 | fn rejects_invalid_freq() { |
| 297 | let mut props = HashMap::new(); |
| 298 | props.insert(RRuleProperty::Freq, "DAIL".into()); |
| 299 | let res = props_to_rrule(&props); |
| 300 | assert_eq!( |
| 301 | res.unwrap_err(), |
| 302 | ParseError::InvalidFrequency("DAIL".into()) |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | #[test] |
| 307 | fn rejects_invalid_byhour() { |
| 308 | let mut props = HashMap::new(); |
| 309 | props.insert(RRuleProperty::Freq, "DAILY".into()); |
| 310 | props.insert(RRuleProperty::ByHour, "24".into()); |
| 311 | let res = props_to_rrule(&props); |
| 312 | assert_eq!(res.unwrap_err(), ParseError::InvalidByHour("24".into())); |
| 313 | |
| 314 | props.insert(RRuleProperty::ByHour, "5,6,25".into()); |
| 315 | let res = props_to_rrule(&props); |
| 316 | assert_eq!(res.unwrap_err(), ParseError::InvalidByHour("5,6,25".into())); |
| 317 | } |
| 318 | |
| 319 | #[test] |
| 320 | fn rejects_invalid_byminute() { |
| 321 | let mut props = HashMap::new(); |
| 322 | props.insert(RRuleProperty::Freq, "DAILY".into()); |
| 323 | props.insert(RRuleProperty::ByMinute, "60".into()); |
| 324 | let res = props_to_rrule(&props); |
| 325 | assert_eq!(res.unwrap_err(), ParseError::InvalidByMinute("60".into())); |
| 326 | |
| 327 | props.insert(RRuleProperty::ByMinute, "4,5,64".into()); |
| 328 | let res = props_to_rrule(&props); |
| 329 | assert_eq!( |
| 330 | res.unwrap_err(), |
| 331 | ParseError::InvalidByMinute("4,5,64".into()) |
| 332 | ); |
| 333 | } |
| 334 | } |