Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split DateTimeFormat keys #774

Merged
merged 1 commit into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions components/datetime/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use crate::{
format::datetime,
options::DateTimeFormatOptions,
pattern::PatternItem,
provider::{gregory::DatesV1Marker, helpers::DateTimePatterns},
provider::{
gregory::{DatePatternsV1Marker, DateSymbolsV1Marker},
helpers::DateTimePatterns,
},
};
use icu_locid::Locale;
use icu_provider::prelude::*;
Expand Down Expand Up @@ -59,7 +62,7 @@ use crate::{
pub struct DateTimeFormat<'d> {
pub(super) locale: Locale,
pub(super) pattern: Pattern,
pub(super) symbols: DataPayload<'d, 'd, provider::gregory::DateSymbolsV1Marker>,
pub(super) symbols: DataPayload<'d, 'd, DateSymbolsV1Marker>,
}

impl<'d> DateTimeFormat<'d> {
Expand Down Expand Up @@ -87,17 +90,19 @@ impl<'d> DateTimeFormat<'d> {
/// ```
pub fn try_new<
T: Into<Locale>,
D: DataProvider<'d, 'd, provider::gregory::DatesV1Marker> + ?Sized,
D: DataProvider<'d, 'd, DateSymbolsV1Marker>
+ DataProvider<'d, 'd, DatePatternsV1Marker>
+ ?Sized,
>(
locale: T,
data_provider: &D,
options: &DateTimeFormatOptions,
) -> Result<Self, DateTimeFormatError> {
let locale = locale.into();
let data = data_provider
let symbols_data = data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::GREGORY_V1,
key: provider::key::GREGORY_DATE_SYMBOLS_V1,
options: ResourceOptions {
variant: None,
langid: Some(locale.clone().into()),
Expand All @@ -106,9 +111,24 @@ impl<'d> DateTimeFormat<'d> {
})?
.take_payload()?;

let pattern = data
let patterns_data: icu_provider::DataPayload<
'_,
'_,
provider::gregory::DatePatternsV1Marker,
> = data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::GREGORY_DATE_PATTERNS_V1,
options: ResourceOptions {
variant: None,
langid: Some(locale.clone().into()),
},
},
})?
.take_payload()?;

let pattern = patterns_data
.get()
.patterns
.get_pattern_for_options(options)?
.unwrap_or_default();

Expand All @@ -125,7 +145,7 @@ impl<'d> DateTimeFormat<'d> {
return Err(DateTimeFormatError::UnsupportedField(field.symbol));
}

Ok(Self::new(locale, pattern, data))
Ok(Self::new(locale, pattern, symbols_data))
}

/// Creates a new [`DateTimeFormat`] regardless of whether there are time-zone symbols in the pattern.
Expand All @@ -144,13 +164,10 @@ impl<'d> DateTimeFormat<'d> {
pub(super) fn new<T: Into<Locale>>(
locale: T,
pattern: Pattern,
data: DataPayload<'d, 'd, DatesV1Marker>,
symbols: DataPayload<'d, 'd, DateSymbolsV1Marker>,
) -> Self {
let locale = locale.into();

// TODO(#257): Avoid the need for this clone by splitting the keys.
let symbols = DataPayload::from_owned(data.get().symbols.clone());

Self {
locale,
pattern,
Expand Down
8 changes: 4 additions & 4 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ mod tests {
#[cfg(feature = "provider_serde")]
fn test_basic() {
use crate::mock::datetime::MockDateTime;
use crate::provider::gregory::DatesV1Marker;
use crate::provider::gregory::DateSymbolsV1Marker;
use icu_provider::prelude::*;

let provider = icu_testdata::get_provider();
let data: DataPayload<DatesV1Marker> = provider
let data: DataPayload<DateSymbolsV1Marker> = provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::GREGORY_V1,
key: provider::key::GREGORY_DATE_SYMBOLS_V1,
options: ResourceOptions {
variant: None,
langid: Some("en".parse().unwrap()),
Expand All @@ -268,7 +268,7 @@ mod tests {
let mut sink = String::new();
write_pattern(
&pattern,
&data.get().symbols,
&data.get(),
&datetime,
&"und".parse().unwrap(),
&mut sink,
Expand Down
25 changes: 7 additions & 18 deletions components/datetime/src/provider/gregory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,6 @@

use std::borrow::Cow;

#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct DatesV1 {
pub symbols: DateSymbolsV1,

pub patterns: PatternsV1,
}

icu_provider::impl_data_marker_no_lifetime!(
DatesV1,
/// Marker type for [`DatesV1`]
DatesV1Marker
);

#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand All @@ -45,14 +28,20 @@ icu_provider::impl_data_marker_no_lifetime!(
feature = "provider_serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub struct PatternsV1 {
pub struct DatePatternsV1 {
pub date: patterns::LengthPatternsV1,

pub time: patterns::LengthPatternsV1,

pub datetime: patterns::DateTimeFormatsV1,
}

icu_provider::impl_data_marker_no_lifetime!(
DatePatternsV1,
/// Marker type for [`DatePatternsV1`]
DatePatternsV1Marker
);

macro_rules! symbols {
($name: ident, $expr: ty) => {
pub mod $name {
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/provider/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub trait DateTimeSymbols {
) -> &Cow<str>;
}

impl DateTimePatterns for provider::gregory::PatternsV1 {
impl DateTimePatterns for provider::gregory::DatePatternsV1 {
fn get_pattern_for_options(&self, options: &DateTimeFormatOptions) -> Result<Option<Pattern>> {
match options {
DateTimeFormatOptions::Length(bag) => self.get_pattern_for_length_bag(bag),
Expand Down
3 changes: 2 additions & 1 deletion components/datetime/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub mod time_zones;

pub mod key {
use icu_provider::{resource_key, ResourceKey};
pub const GREGORY_V1: ResourceKey = resource_key!(dates, "gregory", 1);
pub const GREGORY_DATE_PATTERNS_V1: ResourceKey = resource_key!(date_patterns, "gregory", 1);
pub const GREGORY_DATE_SYMBOLS_V1: ResourceKey = resource_key!(date_symbols, "gregory", 1);
pub const TIMEZONE_FORMATS_V1: ResourceKey = resource_key!(time_zones, "formats", 1);
pub const TIMEZONE_EXEMPLAR_CITIES_V1: ResourceKey =
resource_key!(time_zones, "exemplar-cities", 1);
Expand Down
18 changes: 9 additions & 9 deletions components/datetime/src/skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,16 @@ mod test {
use crate::{
fields::{Day, Field, FieldLength, Month, Weekday},
options::components,
provider::{gregory::DatesV1Marker, key::GREGORY_V1},
provider::{gregory::DatePatternsV1Marker, key::GREGORY_DATE_PATTERNS_V1},
};

fn get_data_payload() -> DataPayload<'static, 'static, DatesV1Marker> {
fn get_data_payload() -> DataPayload<'static, 'static, DatePatternsV1Marker> {
let provider = icu_testdata::get_provider();
let langid = langid!("en");
provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: GREGORY_V1,
key: GREGORY_DATE_PATTERNS_V1,
options: ResourceOptions {
variant: None,
langid: Some(langid),
Expand Down Expand Up @@ -672,7 +672,7 @@ mod test {
let data_provider = get_data_payload();

match get_best_available_format_pattern(
&data_provider.get().patterns.datetime.skeletons,
&data_provider.get().datetime.skeletons,
&requested_fields,
) {
BestSkeleton::AllFieldsMatch(available_format_pattern)
Expand All @@ -699,7 +699,7 @@ mod test {
let data_provider = get_data_payload();

match get_best_available_format_pattern(
&data_provider.get().patterns.datetime.skeletons,
&data_provider.get().datetime.skeletons,
&requested_fields,
) {
BestSkeleton::MissingOrExtraFields(available_format_pattern) => {
Expand All @@ -724,8 +724,8 @@ mod test {
let data_provider = get_data_payload();

match create_best_pattern_for_fields(
&data_provider.get().patterns.datetime.skeletons,
&data_provider.get().patterns.datetime.length_patterns,
&data_provider.get().datetime.skeletons,
&data_provider.get().datetime.length_patterns,
&requested_fields,
) {
BestSkeleton::AllFieldsMatch(available_format_pattern) => {
Expand All @@ -748,7 +748,7 @@ mod test {

assert_eq!(
get_best_available_format_pattern(
&data_provider.get().patterns.datetime.skeletons,
&data_provider.get().datetime.skeletons,
&requested_fields
),
BestSkeleton::NoMatch,
Expand All @@ -769,7 +769,7 @@ mod test {

assert_eq!(
get_best_available_format_pattern(
&data_provider.get().patterns.datetime.skeletons,
&data_provider.get().datetime.skeletons,
&requested_fields
),
BestSkeleton::NoMatch,
Expand Down
33 changes: 27 additions & 6 deletions components/datetime/src/zoned_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ impl<'d> ZonedDateTimeFormat<'d> {
) -> Result<Self, DateTimeFormatError>
where
L: Into<Locale>,
DP: DataProvider<'d, 'd, provider::gregory::DatesV1Marker> + ?Sized,
DP: DataProvider<'d, 'd, provider::gregory::DatePatternsV1Marker>
+ DataProvider<'d, 'd, provider::gregory::DateSymbolsV1Marker>
+ ?Sized,
ZP: DataProvider<'d, 'd, provider::time_zones::TimeZoneFormatsV1Marker>
+ DataProvider<'d, 'd, provider::time_zones::ExemplarCitiesV1Marker>
+ DataProvider<'d, 'd, provider::time_zones::MetaZoneGenericNamesLongV1Marker>
Expand All @@ -106,10 +108,30 @@ impl<'d> ZonedDateTimeFormat<'d> {
+ ?Sized,
{
let locale = locale.into();
let data = date_provider
let pattern_data: icu_provider::DataPayload<
'_,
'_,
provider::gregory::DatePatternsV1Marker,
> = date_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::GREGORY_DATE_PATTERNS_V1,
options: ResourceOptions {
variant: None,
langid: Some(locale.clone().into()),
},
},
})?
.take_payload()?;

let symbols_data: icu_provider::DataPayload<
'_,
'_,
provider::gregory::DateSymbolsV1Marker,
> = date_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::GREGORY_V1,
key: provider::key::GREGORY_DATE_SYMBOLS_V1,
options: ResourceOptions {
variant: None,
langid: Some(locale.clone().into()),
Expand All @@ -118,13 +140,12 @@ impl<'d> ZonedDateTimeFormat<'d> {
})?
.take_payload()?;

let pattern = data
let pattern = pattern_data
.get()
.patterns
.get_pattern_for_options(options)?
.unwrap_or_default();

let datetime_format = DateTimeFormat::new(locale, pattern, data);
let datetime_format = DateTimeFormat::new(locale, pattern, symbols_data);
let time_zone_format = TimeZoneFormat::try_new(
datetime_format.locale.clone(),
datetime_format.pattern.clone(),
Expand Down
Loading