| 1 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#, hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#}; |
| 2 | |
| 3 | use craterust-analyzer cargo rrule 0.14.0 crate/::parserrust-analyzer cargo rrule 0.14.0 parser/::ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/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(superrust-analyzer cargo rrule 0.14.0 parser/content_line/) fn parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().<K: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#<Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err# = ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#> + Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# + Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq#>( |
| 8 | raw_parameters: &str, |
| 9 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<K, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#> { |
| 10 | let mut parameters = HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V>`]new().(); |
| 11 | for raw_parameter in raw_parameters.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(';') { |
| 12 | if raw_parameter.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 13 | continue; |
| 14 | } |
| 15 | let (raw_parameter, value) = raw_parameter |
| 16 | .split_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_once().('=') |
| 17 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#::InvalidParameterFormatrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#InvalidParameterFormat#(raw_parameter.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 18 | let parameter = K::from_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#from_str().(raw_parameter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 19 | |
| 20 | if parameters.insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(parameter, value.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() { |
| 21 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#::DuplicatePropertyrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#DuplicateProperty#(raw_parameter.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 22 | } |
| 23 | } |
| 24 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(parameters) |
| 25 | } |
| 26 | |
| 27 | #[cfg(test)] |
| 28 | mod testsrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/tests/ { |
| 29 | use superrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/::superrust-analyzer cargo rrule 0.14.0 parser/content_line/::date_content_linerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/::DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#; |
| 30 | |
| 31 | use superrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/::*; |
| 32 | |
| 33 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 34 | fn parses_valid_property_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/tests/parses_valid_property_parameters().() { |
| 35 | let tests = [ |
| 36 | ( |
| 37 | "VALUE=DATE", |
| 38 | [(DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#::Valuerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#Value#, "DATE".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())] |
| 39 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 40 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _>>(), |
| 41 | ), |
| 42 | ( |
| 43 | "TZID=Europe/London", |
| 44 | [(DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#::Timezonerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#Timezone#, "Europe/London".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())] |
| 45 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().() |
| 46 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _>>(), |
| 47 | ), |
| 48 | ]; |
| 49 | |
| 50 | for (input, expected_output) in tests { |
| 51 | let output = parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().(input); |
| 52 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(expected_output)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 57 | fn rejecets_unrecognized_property_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/tests/rejecets_unrecognized_property_parameters().() { |
| 58 | let tests = [( |
| 59 | "VALUES=DATE", |
| 60 | ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#::UnrecognizedParameterrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#UnrecognizedParameter#("VALUES".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 61 | )]; |
| 62 | |
| 63 | for (input, expected_output) in tests { |
| 64 | let output: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, _> = parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().(input); |
| 65 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(expected_output)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 70 | fn rejecets_malformed_property_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/tests/rejecets_malformed_property_parameters().() { |
| 71 | let tests = [( |
| 72 | "VALUE:DATE", |
| 73 | ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#::InvalidParameterFormatrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#InvalidParameterFormat#("VALUE:DATE".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 74 | )]; |
| 75 | |
| 76 | for (input, expected_output) in tests { |
| 77 | let output: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, _> = parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().(input); |
| 78 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(expected_output)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 83 | fn rejecets_duplicate_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/tests/rejecets_duplicate_parameters().() { |
| 84 | let tests = [( |
| 85 | "VALUE=DATE;TZID=Europe/London;TZID=Europe/Frankfurt", |
| 86 | ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#::DuplicatePropertyrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#DuplicateProperty#("TZID".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 87 | )]; |
| 88 | |
| 89 | for (input, expected_output) in tests { |
| 90 | let output: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, _> = parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().(input); |
| 91 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(expected_output)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 96 | fn does_not_attempt_to_parse_empty_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/tests/does_not_attempt_to_parse_empty_parameters().() { |
| 97 | let tests = [ |
| 98 | ("", [].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _>>()), |
| 99 | (";", [].into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/iter/impl#[`[T; N]`][IntoIterator]into_iter().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<_, _>>()), |
| 100 | ]; |
| 101 | |
| 102 | for (input, expected_output) in tests { |
| 103 | let output: Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, _> = parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().(input); |
| 104 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(output, Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(expected_output)); |
| 105 | } |
| 106 | } |
| 107 | } |