| 1 | use crate::parser::{regex::get_property_name, ParseError}; |
| 2 | |
| 3 | use super::PropertyName; |
| 4 | |
| 5 | #[derive(Debug, PartialEq, Clone)] |
| 6 | pub(crate) struct ContentLineCaptures<'a> { |
| 7 | pub property_name: PropertyName, |
| 8 | pub parameters: Option<&'a str>, |
| 9 | pub value: &'a str, |
| 10 | } |
| 11 | |
| 12 | impl<'a> ContentLineCaptures<'a> { |
| 13 | pub(crate) fn new(line: &'a str) -> Result<Self, ParseError> { |
| 14 | // Default property name to RRULE. |
| 15 | let property_name = get_property_name(line)?.unwrap_or(PropertyName::RRule); |
| 16 | match property_name { |
| 17 | // If the line did not contain a property name (i.e. no ':'), then the |
| 18 | // entire line is interpreted as the value |
| 19 | PropertyName::RRule if !line.contains(':') => Ok(ContentLineCaptures { |
| 20 | property_name: PropertyName::RRule, |
| 21 | parameters: None, |
| 22 | value: line, |
| 23 | }), |
| 24 | property_name => { |
| 25 | let mut parameters = None; |
| 26 | if line.starts_with(&format!("{};", property_name)) { |
| 27 | let only_colon_idx = line.find(':'); |
| 28 | if let Some(only_colon_idx) = only_colon_idx { |
| 29 | parameters = |
| 30 | Some(&line[property_name.to_string().len() + 1..only_colon_idx]); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | Ok(Self { |
| 35 | property_name, |
| 36 | parameters, |
| 37 | value: line |
| 38 | .split_once(':') |
| 39 | .map(|(_name, val)| val) |
| 40 | .unwrap_or_default(), |
| 41 | }) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[cfg(test)] |
| 48 | mod tests { |
| 49 | use super::*; |
| 50 | |
| 51 | #[test] |
| 52 | fn parses_content_line_parts() { |
| 53 | let tests = [ |
| 54 | ( |
| 55 | "DTSTART;TZID=America/Everywhere:20120251T023000Z", |
| 56 | ContentLineCaptures { |
| 57 | property_name: PropertyName::DtStart, |
| 58 | parameters: Some("TZID=America/Everywhere"), |
| 59 | value: "20120251T023000Z", |
| 60 | }, |
| 61 | ), |
| 62 | ( |
| 63 | "DTSTART:20120251T023000Z", |
| 64 | ContentLineCaptures { |
| 65 | property_name: PropertyName::DtStart, |
| 66 | parameters: None, |
| 67 | value: "20120251T023000Z", |
| 68 | }, |
| 69 | ), |
| 70 | ( |
| 71 | "DTSTART;TZID=America/Everywhere:20120251T023000Z", |
| 72 | ContentLineCaptures { |
| 73 | property_name: PropertyName::DtStart, |
| 74 | parameters: Some("TZID=America/Everywhere"), |
| 75 | value: "20120251T023000Z", |
| 76 | }, |
| 77 | ), |
| 78 | ( |
| 79 | "RDATE:19970714T123000Z", |
| 80 | ContentLineCaptures { |
| 81 | property_name: PropertyName::RDate, |
| 82 | parameters: None, |
| 83 | value: "19970714T123000Z", |
| 84 | }, |
| 85 | ), |
| 86 | ( |
| 87 | "RDATE;VALUE=DATE:19970101,19970120,19970217,19970421", |
| 88 | ContentLineCaptures { |
| 89 | property_name: PropertyName::RDate, |
| 90 | parameters: Some("VALUE=DATE"), |
| 91 | value: "19970101,19970120,19970217,19970421", |
| 92 | }, |
| 93 | ), |
| 94 | ( |
| 95 | "RRULE:FREQ=DAILY;COUNT=10", |
| 96 | ContentLineCaptures { |
| 97 | property_name: PropertyName::RRule, |
| 98 | parameters: None, |
| 99 | value: "FREQ=DAILY;COUNT=10", |
| 100 | }, |
| 101 | ), |
| 102 | ( |
| 103 | "FREQ=DAILY;COUNT=10", |
| 104 | ContentLineCaptures { |
| 105 | // Defaults to RRULE |
| 106 | property_name: PropertyName::RRule, |
| 107 | parameters: None, |
| 108 | value: "FREQ=DAILY;COUNT=10", |
| 109 | }, |
| 110 | ), |
| 111 | ]; |
| 112 | for (input, expected_output) in tests { |
| 113 | let output = ContentLineCaptures::new(input); |
| 114 | assert_eq!(output, Ok(expected_output)); |
| 115 | } |
| 116 | } |
| 117 | } |