| 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#, 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 logrust-analyzer cargo log 0.4.27 crate/::warnrust-analyzer cargo log 0.4.27 macros/warn!; |
| 4 | |
| 5 | use craterust-analyzer cargo rrule 0.14.0 crate/::{ |
| 6 | parserrust-analyzer cargo rrule 0.14.0 parser/::{ |
| 7 | datetimerust-analyzer cargo rrule 0.14.0 parser/datetime/::{datestring_to_daterust-analyzer cargo rrule 0.14.0 parser/datetime/datestring_to_date()., parse_timezonerust-analyzer cargo rrule 0.14.0 parser/datetime/parse_timezone().}, |
| 8 | ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#, |
| 9 | }, |
| 10 | Tzrust-analyzer cargo rrule 0.14.0 core/timezone/Tz#, |
| 11 | }; |
| 12 | |
| 13 | use superrust-analyzer cargo rrule 0.14.0 parser/content_line/::{content_line_partsrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/::ContentLineCapturesrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#, parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/::parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().}; |
| 14 | |
| 15 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 16 | pub enum DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter# { |
| 17 | Timezonerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#Timezone#, |
| 18 | Valuerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#Value#, |
| 19 | } |
| 20 | |
| 21 | impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for DateParameterrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter# { |
| 22 | type Errrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[DateParameter][FromStr]Err# = ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#; |
| 23 | |
| 24 | fn from_strrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[DateParameter][FromStr]from_str().(s: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[DateParameter][FromStr], Selfrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[DateParameter][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> { |
| 25 | let param = match &s.to_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_uppercase().()[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index(). { |
| 26 | "TZID" => Selfrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[DateParameter][FromStr]::Timezonerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#Timezone#, |
| 27 | "VALUE" => Selfrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[DateParameter][FromStr]::Valuerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/DateParameter#Value#, |
| 28 | _ => 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#::UnrecognizedParameterrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#UnrecognizedParameter#(s.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())), |
| 29 | }; |
| 30 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(param) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<ContentLineCapturesrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#<'_>> for Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<chronorust-analyzer cargo chrono 0.4.40 crate/::DateTimerust-analyzer cargo chrono 0.4.40 datetime/DateTime#<Tzrust-analyzer cargo rrule 0.14.0 core/timezone/Tz#>> { |
| 35 | type Errorrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[`Vec<chrono::DateTime<Tz>>`][`TryFrom<ContentLineCaptures<'_>>`]Error# = ParseErrorrust-analyzer cargo rrule 0.14.0 parser/error/ParseError#; |
| 36 | |
| 37 | fn try_fromrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[`Vec<chrono::DateTime<Tz>>`][`TryFrom<ContentLineCaptures<'_>>`]try_from().(value: ContentLineCapturesrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[`Vec<chrono::DateTime<Tz>>`][`TryFrom<ContentLineCaptures<'_>>`], Selfrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[`Vec<chrono::DateTime<Tz>>`][`TryFrom<ContentLineCaptures<'_>>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> { |
| 38 | let parameters: 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#> = value |
| 39 | .parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#parameters. |
| 40 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(parse_parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/parameters/parse_parameters().) |
| 41 | .transposerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<Result<T, E>>`]transpose().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 42 | .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().(); |
| 43 | |
| 44 | match parameters |
| 45 | .getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&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#) |
| 46 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|val| val.to_ascii_lowercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_lowercase().()) |
| 47 | .as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().() |
| 48 | { |
| 49 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("date") => { |
| 50 | warnrust-analyzer cargo log 0.4.27 macros/warn!!( |
| 51 | "Parameter `DATE` is not supported for property name: `{}`. The dates will be interpreter with the `DATE-TIME` parameter instead.", |
| 52 | value.property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name. |
| 53 | ); |
| 54 | } |
| 55 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("period") => { |
| 56 | warnrust-analyzer cargo log 0.4.27 macros/warn!!( |
| 57 | "Parameter `PERIOD` is not supported for property name: `{}`. The dates will be interpreter with the `DATE-TIME` parameter instead.", |
| 58 | value.property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name. |
| 59 | ); |
| 60 | } |
| 61 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("date-time") => {} |
| 62 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(param) => { |
| 63 | warnrust-analyzer cargo log 0.4.27 macros/warn!!( |
| 64 | "Encountered unexpected parameter `{param}` for property name: `{}`", |
| 65 | value.property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name. |
| 66 | ); |
| 67 | } |
| 68 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {} |
| 69 | } |
| 70 | |
| 71 | let timezone = parameters |
| 72 | .getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&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#) |
| 73 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|tz| parse_timezonerust-analyzer cargo rrule 0.14.0 parser/datetime/parse_timezone().(tz)) |
| 74 | .transposerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<Result<T, E>>`]transpose().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 75 | let property = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}", value.property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name.); |
| 76 | |
| 77 | let mut dates = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 78 | for val in value.valuerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#value..splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(',') { |
| 79 | if val.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { |
| 80 | continue; |
| 81 | } |
| 82 | let datetime = datestring_to_daterust-analyzer cargo rrule 0.14.0 parser/datetime/datestring_to_date().(val, timezone, &property)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | dates.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(datetime); |
| 84 | } |
| 85 | |
| 86 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(dates) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[cfg(test)] |
| 91 | mod testsrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/ { |
| 92 | use chronorust-analyzer cargo chrono 0.4.40 crate/::TimeZonerust-analyzer cargo chrono 0.4.40 offset/TimeZone#; |
| 93 | |
| 94 | use craterust-analyzer cargo rrule 0.14.0 crate/::{corerust-analyzer cargo rrule 0.14.0 core/::Tzrust-analyzer cargo rrule 0.14.0 core/timezone/Tz#, parserrust-analyzer cargo rrule 0.14.0 parser/::content_linerust-analyzer cargo rrule 0.14.0 parser/content_line/::PropertyNamerust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#}; |
| 95 | |
| 96 | use superrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/::*; |
| 97 | |
| 98 | const UTCrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/UTC.: Tzrust-analyzer cargo rrule 0.14.0 core/timezone/Tz# = Tzrust-analyzer cargo rrule 0.14.0 core/timezone/Tz#::UTCrust-analyzer cargo rrule 0.14.0 core/timezone/impl#[Tz]UTC.; |
| 99 | |
| 100 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 101 | fn parses_date_content_linerust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/parses_date_content_line().() { |
| 102 | let tests = [ |
| 103 | ( |
| 104 | ContentLineCapturesrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures# { |
| 105 | property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name.: PropertyNamerust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#::RDaterust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#RDate#, |
| 106 | parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#parameters.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 107 | valuerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#value.: "19970714T123000Z", |
| 108 | }, |
| 109 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![UTCrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/UTC..with_ymd_and_hmsrust-analyzer cargo chrono 0.4.40 offset/TimeZone#with_ymd_and_hms().(1997, 7, 14, 12, 30, 0).unwraprust-analyzer cargo chrono 0.4.40 offset/impl#[`LocalResult<T>`]unwrap().()], |
| 110 | ), |
| 111 | ( |
| 112 | ContentLineCapturesrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures# { |
| 113 | property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name.: PropertyNamerust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#::RDaterust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#RDate#, |
| 114 | parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#parameters.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 115 | valuerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#value.: "19970714T123000", |
| 116 | }, |
| 117 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Tzrust-analyzer cargo rrule 0.14.0 core/timezone/Tz#::LOCALrust-analyzer cargo rrule 0.14.0 core/timezone/impl#[Tz]LOCAL..with_ymd_and_hmsrust-analyzer cargo chrono 0.4.40 offset/TimeZone#with_ymd_and_hms().(1997, 7, 14, 12, 30, 0).unwraprust-analyzer cargo chrono 0.4.40 offset/impl#[`LocalResult<T>`]unwrap().()], |
| 118 | ), |
| 119 | ( |
| 120 | ContentLineCapturesrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures# { |
| 121 | property_namerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#property_name.: PropertyNamerust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#::RDaterust-analyzer cargo rrule 0.14.0 parser/content_line/PropertyName#RDate#, |
| 122 | parametersrust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#parameters.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("VALUE=DATE;TZID=UTC"), |
| 123 | valuerust-analyzer cargo rrule 0.14.0 parser/content_line/content_line_parts/ContentLineCaptures#value.: "19970101,19970120,19970217,19970421", |
| 124 | }, |
| 125 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 126 | UTCrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/UTC..with_ymd_and_hmsrust-analyzer cargo chrono 0.4.40 offset/TimeZone#with_ymd_and_hms().(1997, 1, 1, 0, 0, 0).unwraprust-analyzer cargo chrono 0.4.40 offset/impl#[`LocalResult<T>`]unwrap().(), |
| 127 | UTCrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/UTC..with_ymd_and_hmsrust-analyzer cargo chrono 0.4.40 offset/TimeZone#with_ymd_and_hms().(1997, 1, 20, 0, 0, 0).unwraprust-analyzer cargo chrono 0.4.40 offset/impl#[`LocalResult<T>`]unwrap().(), |
| 128 | UTCrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/UTC..with_ymd_and_hmsrust-analyzer cargo chrono 0.4.40 offset/TimeZone#with_ymd_and_hms().(1997, 2, 17, 0, 0, 0).unwraprust-analyzer cargo chrono 0.4.40 offset/impl#[`LocalResult<T>`]unwrap().(), |
| 129 | UTCrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/tests/UTC..with_ymd_and_hmsrust-analyzer cargo chrono 0.4.40 offset/TimeZone#with_ymd_and_hms().(1997, 4, 21, 0, 0, 0).unwraprust-analyzer cargo chrono 0.4.40 offset/impl#[`LocalResult<T>`]unwrap().(), |
| 130 | ], |
| 131 | ), |
| 132 | ]; |
| 133 | |
| 134 | for (input, expected_output) in tests { |
| 135 | let output = TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#::try_fromrust-analyzer cargo rrule 0.14.0 parser/content_line/date_content_line/impl#[`Vec<chrono::DateTime<Tz>>`][`TryFrom<ContentLineCaptures<'_>>`]try_from().(input); |
| 136 | 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)); |
| 137 | } |
| 138 | } |
| 139 | } |