| 1 | //! Utility functions around the regexes we use for parsing rrule strings. |
| 2 | |
| 3 | use std::{str::FromStr, sync::OnceLock}; |
| 4 | |
| 5 | use regex::{Captures, Regex}; |
| 6 | |
| 7 | use super::{content_line::PropertyName, ParseError}; |
| 8 | |
| 9 | #[derive(Debug, PartialEq)] |
| 10 | pub(crate) struct ParsedDateString { |
| 11 | pub year: i32, |
| 12 | pub month: u32, |
| 13 | pub day: u32, |
| 14 | pub time: Option<ParsedDateStringTime>, |
| 15 | pub flags: ParsedDateStringFlags, |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, PartialEq)] |
| 19 | pub(crate) struct ParsedDateStringFlags { |
| 20 | pub zulu_timezone_set: bool, |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug, PartialEq)] |
| 24 | pub(crate) struct ParsedDateStringTime { |
| 25 | pub hour: u32, |
| 26 | pub min: u32, |
| 27 | pub sec: u32, |
| 28 | } |
| 29 | |
| 30 | fn get_datetime_captures<T: FromStr>( |
| 31 | captures: &Captures, |
| 32 | idx: usize, |
| 33 | val: &str, |
| 34 | ) -> Result<T, ParseError> { |
| 35 | captures |
| 36 | .get(idx) |
| 37 | .ok_or_else(|| ParseError::InvalidDateTimeFormat(val.into()))? |
| 38 | .as_str() |
| 39 | .parse() |
| 40 | .map_err(|_| ParseError::InvalidDateTimeFormat(val.into())) |
| 41 | } |
| 42 | |
| 43 | impl ParsedDateString { |
| 44 | /// Parses a date string with format `YYYYMMDD(THHMMSSZ)` where the part in parentheses |
| 45 | /// is optional. It returns [`ParsedDateString`]. |
| 46 | pub(crate) fn from_ical_datetime(val: &str) -> Result<Self, ParseError> { |
| 47 | static DATESTR_RE: OnceLock<Regex> = OnceLock::new(); |
| 48 | |
| 49 | let captures = DATESTR_RE |
| 50 | .get_or_init(|| { |
| 51 | Regex::new( |
| 52 | r"(?m)^([0-9]{4})([0-9]{2})([0-9]{2})(T([0-9]{2})([0-9]{2})([0-9]{2})(Z?))?$", |
| 53 | ) |
| 54 | .expect("DATESTR_RE must compile") |
| 55 | }) |
| 56 | .captures(val) |
| 57 | .ok_or_else(|| ParseError::InvalidDateTimeFormat(val.into()))?; |
| 58 | |
| 59 | let year = get_datetime_captures(&captures, 1, val)?; |
| 60 | let month = get_datetime_captures(&captures, 2, val)?; |
| 61 | let day = get_datetime_captures(&captures, 3, val)?; |
| 62 | |
| 63 | // Check if time part is captured |
| 64 | let time = if captures.get(4).is_some() { |
| 65 | let hour = get_datetime_captures(&captures, 5, val)?; |
| 66 | let min = get_datetime_captures(&captures, 6, val)?; |
| 67 | let sec = get_datetime_captures(&captures, 7, val)?; |
| 68 | Some(ParsedDateStringTime { hour, min, sec }) |
| 69 | } else { |
| 70 | None |
| 71 | }; |
| 72 | |
| 73 | let zulu_timezone_set = match captures.get(8) { |
| 74 | Some(part) => part.as_str() == "Z", |
| 75 | None => false, |
| 76 | }; |
| 77 | let flags = ParsedDateStringFlags { zulu_timezone_set }; |
| 78 | |
| 79 | Ok(Self { |
| 80 | year, |
| 81 | month, |
| 82 | day, |
| 83 | time, |
| 84 | flags, |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Get the line property name, the `RRULE:`, `EXRULE:` etc part. |
| 90 | pub(crate) fn get_property_name(val: &str) -> Result<Option<PropertyName>, ParseError> { |
| 91 | static PARSE_PROPERTY_NAME_RE: OnceLock<Regex> = OnceLock::new(); |
| 92 | |
| 93 | PARSE_PROPERTY_NAME_RE |
| 94 | .get_or_init(|| { |
| 95 | Regex::new(r"(?m)^([A-Z]+?)[:;]").expect("PARSE_PROPERTY_NAME_RE regex must compile") |
| 96 | }) |
| 97 | .captures(val) |
| 98 | .and_then(|captures| captures.get(1)) |
| 99 | .map(|name| PropertyName::from_str(name.as_str())) |
| 100 | .transpose() |
| 101 | } |
| 102 | |
| 103 | #[cfg(test)] |
| 104 | mod tests { |
| 105 | use crate::parser::{content_line::PropertyName, regex::get_property_name, ParseError}; |
| 106 | |
| 107 | use super::{ParsedDateString, ParsedDateStringFlags, ParsedDateStringTime}; |
| 108 | |
| 109 | const GARBAGE_INPUTS: [&str; 4] = ["", " ", "fasfa!2414", "-20101017T120000Z"]; |
| 110 | |
| 111 | #[test] |
| 112 | fn parses_valid_datestrings_correctly() { |
| 113 | let tests = [ |
| 114 | ( |
| 115 | "20101017T120000Z", |
| 116 | ParsedDateString { |
| 117 | year: 2010, |
| 118 | month: 10, |
| 119 | day: 17, |
| 120 | time: Some(ParsedDateStringTime { |
| 121 | hour: 12, |
| 122 | min: 0, |
| 123 | sec: 0, |
| 124 | }), |
| 125 | flags: ParsedDateStringFlags { |
| 126 | zulu_timezone_set: true, |
| 127 | }, |
| 128 | }, |
| 129 | ), |
| 130 | ( |
| 131 | "20101017", |
| 132 | ParsedDateString { |
| 133 | year: 2010, |
| 134 | month: 10, |
| 135 | day: 17, |
| 136 | time: None, |
| 137 | flags: ParsedDateStringFlags { |
| 138 | zulu_timezone_set: false, |
| 139 | }, |
| 140 | }, |
| 141 | ), |
| 142 | ( |
| 143 | "20220101T121049Z", |
| 144 | ParsedDateString { |
| 145 | year: 2022, |
| 146 | month: 1, |
| 147 | day: 1, |
| 148 | time: Some(ParsedDateStringTime { |
| 149 | hour: 12, |
| 150 | min: 10, |
| 151 | sec: 49, |
| 152 | }), |
| 153 | flags: ParsedDateStringFlags { |
| 154 | zulu_timezone_set: true, |
| 155 | }, |
| 156 | }, |
| 157 | ), |
| 158 | ( |
| 159 | "20220101", |
| 160 | ParsedDateString { |
| 161 | year: 2022, |
| 162 | month: 1, |
| 163 | day: 1, |
| 164 | time: None, |
| 165 | flags: ParsedDateStringFlags { |
| 166 | zulu_timezone_set: false, |
| 167 | }, |
| 168 | }, |
| 169 | ), |
| 170 | ]; |
| 171 | for (input, expected_output) in tests { |
| 172 | let output = ParsedDateString::from_ical_datetime(input); |
| 173 | assert_eq!(output, Ok(expected_output)); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn rejects_invalid_datestrings() { |
| 179 | let tests = [ |
| 180 | GARBAGE_INPUTS.to_vec(), |
| 181 | [ |
| 182 | "-20101017T120000Z", |
| 183 | "20101017T", |
| 184 | "201010177", |
| 185 | "20101017T1200", |
| 186 | "210101017T1200", |
| 187 | ] |
| 188 | .to_vec(), |
| 189 | ] |
| 190 | .concat(); |
| 191 | for input in tests { |
| 192 | let res = ParsedDateString::from_ical_datetime(input); |
| 193 | assert!(res.is_err()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #[test] |
| 198 | fn parses_property_name_from_line() { |
| 199 | let tests = [ |
| 200 | ( |
| 201 | "RRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;BYMINUTE=30", |
| 202 | PropertyName::RRule, |
| 203 | ), |
| 204 | ( |
| 205 | "EXRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;BYMINUTE=30", |
| 206 | PropertyName::ExRule, |
| 207 | ), |
| 208 | ( |
| 209 | "DTSTART;TZID=America/Everywhere:20120251T023000Z\nFREQ=DAILY;INTERVAL=1;", |
| 210 | PropertyName::DtStart, |
| 211 | ), |
| 212 | ]; |
| 213 | for (input, expected_output) in tests { |
| 214 | let output = get_property_name(input); |
| 215 | assert_eq!(output, Ok(Some(expected_output))); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn parses_line_without_property_name() { |
| 221 | let tests = [ |
| 222 | GARBAGE_INPUTS.to_vec(), |
| 223 | vec![ |
| 224 | "FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;BYMINUTE=30", |
| 225 | "TZID=America/Everywhere:20120251T023000Z\nFREQ=DAILY;INTERVAL=1;", |
| 226 | ], |
| 227 | ] |
| 228 | .concat(); |
| 229 | for input in tests { |
| 230 | let res = get_property_name(input); |
| 231 | assert_eq!(res, Ok(None)); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn rejects_line_with_invalid_property_name() { |
| 237 | let tests = [ |
| 238 | ( |
| 239 | "RRULES:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;BYMINUTE=30", |
| 240 | ParseError::UnrecognizedPropertyName("RRULES".into()), |
| 241 | ), |
| 242 | ( |
| 243 | "START;TZID=America/Everywhere:20120251T023000Z\nFREQ=DAILY;INTERVAL=1;", |
| 244 | ParseError::UnrecognizedPropertyName("START".into()), |
| 245 | ), |
| 246 | ]; |
| 247 | for (input, expected_output) in tests { |
| 248 | let output = get_property_name(input); |
| 249 | assert_eq!(output, Err(expected_output)); |
| 250 | } |
| 251 | } |
| 252 | } |