| 1 | use std::{collections::HashMap, hash::Hash, str::FromStr}; |
| 2 | |
| 3 | use crate::parser::ParseError; |
| 4 | |
| 5 | /// Parses a string of semicolon seperated key value pairs into a `HashMap` with |
| 6 | /// predefined keys. It will return an error if duplicate keys are found. |
| 7 | pub(super) fn parse_parameters<K: FromStr<Err = ParseError> + Hash + Eq>( |
| 8 | raw_parameters: &str, |
| 9 | ) -> Result<HashMap<K, String>, ParseError> { |
| 10 | let mut parameters = HashMap::new(); |
| 11 | for raw_parameter in raw_parameters.split(';') { |
| 12 | if raw_parameter.is_empty() { |
| 13 | continue; |
| 14 | } |
| 15 | let (raw_parameter, value) = raw_parameter |
| 16 | .split_once('=') |
| 17 | .ok_or_else(|| ParseError::InvalidParameterFormat(raw_parameter.into()))?; |
| 18 | let parameter = K::from_str(raw_parameter)?; |
| 19 | |
| 20 | if parameters.insert(parameter, value.into()).is_some() { |
| 21 | return Err(ParseError::DuplicateProperty(raw_parameter.into())); |
| 22 | } |
| 23 | } |
| 24 | Ok(parameters) |
| 25 | } |
| 26 | |
| 27 | #[cfg(test)] |
| 28 | mod tests { |
| 29 | use super::super::date_content_line::DateParameter; |
| 30 | |
| 31 | use super::*; |
| 32 | |
| 33 | #[test] |
| 34 | fn parses_valid_property_parameters() { |
| 35 | let tests = [ |
| 36 | ( |
| 37 | "VALUE=DATE", |
| 38 | [(DateParameter::Value, "DATE".to_string())] |
| 39 | .into_iter() |
| 40 | .collect::<HashMap<_, _>>(), |
| 41 | ), |
| 42 | ( |
| 43 | "TZID=Europe/London", |
| 44 | [(DateParameter::Timezone, "Europe/London".to_string())] |
| 45 | .into_iter() |
| 46 | .collect::<HashMap<_, _>>(), |
| 47 | ), |
| 48 | ]; |
| 49 | |
| 50 | for (input, expected_output) in tests { |
| 51 | let output = parse_parameters(input); |
| 52 | assert_eq!(output, Ok(expected_output)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn rejecets_unrecognized_property_parameters() { |
| 58 | let tests = [( |
| 59 | "VALUES=DATE", |
| 60 | ParseError::UnrecognizedParameter("VALUES".into()), |
| 61 | )]; |
| 62 | |
| 63 | for (input, expected_output) in tests { |
| 64 | let output: Result<HashMap<DateParameter, String>, _> = parse_parameters(input); |
| 65 | assert_eq!(output, Err(expected_output)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn rejecets_malformed_property_parameters() { |
| 71 | let tests = [( |
| 72 | "VALUE:DATE", |
| 73 | ParseError::InvalidParameterFormat("VALUE:DATE".into()), |
| 74 | )]; |
| 75 | |
| 76 | for (input, expected_output) in tests { |
| 77 | let output: Result<HashMap<DateParameter, String>, _> = parse_parameters(input); |
| 78 | assert_eq!(output, Err(expected_output)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | fn rejecets_duplicate_parameters() { |
| 84 | let tests = [( |
| 85 | "VALUE=DATE;TZID=Europe/London;TZID=Europe/Frankfurt", |
| 86 | ParseError::DuplicateProperty("TZID".into()), |
| 87 | )]; |
| 88 | |
| 89 | for (input, expected_output) in tests { |
| 90 | let output: Result<HashMap<DateParameter, String>, _> = parse_parameters(input); |
| 91 | assert_eq!(output, Err(expected_output)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[test] |
| 96 | fn does_not_attempt_to_parse_empty_parameters() { |
| 97 | let tests = [ |
| 98 | ("", [].into_iter().collect::<HashMap<_, _>>()), |
| 99 | (";", [].into_iter().collect::<HashMap<_, _>>()), |
| 100 | ]; |
| 101 | |
| 102 | for (input, expected_output) in tests { |
| 103 | let output: Result<HashMap<DateParameter, String>, _> = parse_parameters(input); |
| 104 | assert_eq!(output, Ok(expected_output)); |
| 105 | } |
| 106 | } |
| 107 | } |