From 988c7e28a21dff9e2b3b08be1be3c736101e6387 Mon Sep 17 00:00:00 2001 From: Greg Tatum Date: Mon, 8 Feb 2021 11:17:55 -0600 Subject: [PATCH] Add support for the Gregorian Calendar availableFormats --- components/datetime/src/provider/helpers.rs | 8 ++-- components/datetime/src/provider/mod.rs | 12 +++++- components/datetime/tests/datetime.rs | 13 ++++++- .../provider_cldr/src/transform/dates.rs | 37 ++++++++++++++++++- 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/components/datetime/src/provider/helpers.rs b/components/datetime/src/provider/helpers.rs index 542171d1f48..fb517e4a775 100644 --- a/components/datetime/src/provider/helpers.rs +++ b/components/datetime/src/provider/helpers.rs @@ -85,10 +85,10 @@ impl DateTimeDates for provider::gregory::DatesV1 { ) -> Result { let date_time = &self.patterns.date_time; let s = match style { - style::Date::Full => &date_time.full, - style::Date::Long => &date_time.long, - style::Date::Medium => &date_time.medium, - style::Date::Short => &date_time.short, + style::Date::Full => &date_time.style_patterns.full, + style::Date::Long => &date_time.style_patterns.long, + style::Date::Medium => &date_time.style_patterns.medium, + style::Date::Short => &date_time.style_patterns.short, }; Ok(Pattern::from_bytes_combination(s, date, time)?) } diff --git a/components/datetime/src/provider/mod.rs b/components/datetime/src/provider/mod.rs index a03fe6e1cb8..fd257d84aca 100644 --- a/components/datetime/src/provider/mod.rs +++ b/components/datetime/src/provider/mod.rs @@ -50,7 +50,7 @@ pub mod gregory { pub time: patterns::StylePatternsV1, - pub date_time: patterns::StylePatternsV1, + pub date_time: patterns::DateTimeFormatsV1, } macro_rules! symbols { @@ -193,5 +193,15 @@ pub mod gregory { pub medium: Cow<'static, str>, pub short: Cow<'static, str>, } + + #[derive(Debug, PartialEq, Clone, Default)] + #[cfg_attr( + feature = "provider_serde", + derive(serde::Serialize, serde::Deserialize) + )] + pub struct DateTimeFormatsV1 { + pub style_patterns: StylePatternsV1, + pub available_formats: Vec<(Cow<'static, str>, Cow<'static, str>)>, + } } } diff --git a/components/datetime/tests/datetime.rs b/components/datetime/tests/datetime.rs index d9689ede879..29087825d1b 100644 --- a/components/datetime/tests/datetime.rs +++ b/components/datetime/tests/datetime.rs @@ -20,7 +20,10 @@ use std::{borrow::Cow, fmt::Write}; fn test_fixture(fixture_name: &str) { let provider = icu_testdata::get_provider(); - for fx in fixtures::get_fixture(fixture_name).unwrap().0 { + for fx in fixtures::get_fixture(fixture_name) + .expect("Unable to get fixture.") + .0 + { let langid = fx.input.locale.parse().unwrap(); let options = fixtures::get_options(&fx.input.options); let dtf = DateTimeFormat::try_new(langid, &provider, &options).unwrap(); @@ -63,7 +66,13 @@ fn test_dayperiod_patterns() { .unwrap() .take_payload() .unwrap(); - *data.to_mut().patterns.date_time.long.to_mut() = String::from("{0}"); + *data + .to_mut() + .patterns + .date_time + .style_patterns + .long + .to_mut() = String::from("{0}"); for test_case in &test.test_cases { for dt_input in &test_case.date_times { let date_time: MockDateTime = dt_input.parse().unwrap(); diff --git a/components/provider_cldr/src/transform/dates.rs b/components/provider_cldr/src/transform/dates.rs index 89f29ded04b..c8ff691fb55 100644 --- a/components/provider_cldr/src/transform/dates.rs +++ b/components/provider_cldr/src/transform/dates.rs @@ -130,6 +130,21 @@ impl From<&cldr_json::StylePatterns> for gregory::patterns::StylePatternsV1 { } } +impl From<&cldr_json::DateTimeFormats> for gregory::patterns::DateTimeFormatsV1 { + fn from(other: &cldr_json::DateTimeFormats) -> Self { + // TODO(#308): Support numbering system variations. We currently throw them away. + Self { + style_patterns: gregory::patterns::StylePatternsV1 { + full: other.full.get_pattern().clone(), + long: other.long.get_pattern().clone(), + medium: other.medium.get_pattern().clone(), + short: other.short.get_pattern().clone(), + }, + available_formats: other.available_formats.0.clone(), + } + } +} + impl From<&cldr_json::Dates> for gregory::DatesV1 { fn from(other: &cldr_json::Dates) -> Self { Self { @@ -389,6 +404,26 @@ pub(self) mod cldr_json { pub short: StylePattern, } + #[derive(PartialEq, Debug, Deserialize)] + pub struct DateTimeFormats { + pub full: StylePattern, + pub long: StylePattern, + pub medium: StylePattern, + pub short: StylePattern, + #[serde(rename = "availableFormats")] + pub available_formats: AvailableFormats, + } + + #[derive(PartialEq, Clone, Debug, Deserialize)] + pub struct AvailableFormats( + #[serde(with = "tuple_vec_map")] pub(crate) Vec<(Cow<'static, str>, Cow<'static, str>)>, + ); + + /// This struct represents a 1:1 mapping of the CLDR ca-gregorian.json data at the key + /// "main.LANGID.dates.calendars.gregorian" where "LANGID" is the identifier. + /// + /// e.g. + /// https://github.com/unicode-org/cldr-json/blob/master/cldr-json/cldr-dates-full/main/en/ca-gregorian.json #[derive(PartialEq, Debug, Deserialize)] pub struct GregoryDates { pub months: months::Contexts, @@ -400,7 +435,7 @@ pub(self) mod cldr_json { #[serde(rename = "timeFormats")] pub time_formats: StylePatterns, #[serde(rename = "dateTimeFormats")] - pub date_time_formats: StylePatterns, + pub date_time_formats: DateTimeFormats, } #[derive(PartialEq, Debug, Deserialize)]