Skip to content

Commit

Permalink
Split DataProvider into ResourceProvider and DynProvider (#1554)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Feb 3, 2022
1 parent 10f3d2d commit 20787e7
Show file tree
Hide file tree
Showing 113 changed files with 1,912 additions and 1,739 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions components/calendar/src/japanese.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,11 @@ pub struct JapaneseDateInner {

impl Japanese {
/// Creates a new [`Japanese`] from locale data and an options bag.
pub fn try_new<D: DataProvider<provider::JapaneseErasV1Marker> + ?Sized>(
pub fn try_new<D: ResourceProvider<provider::JapaneseErasV1Marker> + ?Sized>(
data_provider: &D,
) -> Result<Self, DataError> {
let eras = data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::JAPANESE_ERAS_V1,
options: ResourceOptions {
variant: None,
langid: None,
},
},
})?
.load_resource(&DataRequest::default())?
.take_payload()?;
Ok(Self { eras })
}
Expand Down
2 changes: 1 addition & 1 deletion components/calendar/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct EraStartDate {
pub day: u8,
}

#[icu_provider::data_struct]
#[icu_provider::data_struct(JapaneseErasV1Marker = "calendar/japanese@1")]
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This API provides necessary functionality for formatting date and time to user readable textual representation.

[`DateTimeFormat`] is the main structure of the component. It accepts a set of arguments which
allow it to collect necessary data from the [`DataProvider`], and once instantiated, can be
allow it to collect necessary data from the [data provider], and once instantiated, can be
used to quickly format any date and time provided.

## Examples
Expand Down Expand Up @@ -64,7 +64,7 @@ we expect to add more ways to customize the output, like skeletons, and componen
to develop core date and time APIs that will work as an input for this component. [`DateTime`] additionally
has support for non-Gregorian calendars, which this module will eventually be able to format.

[`DataProvider`]: icu_provider::DataProvider
[data provider]: icu_provider
[`ICU4X`]: ../icu/index.html
[`Length`]: options::length
[`DateTime`]: icu_calendar::DateTime
Expand Down
21 changes: 12 additions & 9 deletions components/datetime/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ use crate::{
use alloc::string::String;
use core::marker::PhantomData;
use icu_locid::Locale;
use icu_plurals::provider::PluralRulesV1Marker;
use icu_plurals::provider::OrdinalV1Marker;
use icu_provider::prelude::*;

use crate::{date::DateTimeInput, CldrCalendar, DateTimeFormatError, FormattedDateTime};

/// [`DateTimeFormat`] is the main structure of the [`icu_datetime`] component.
/// When constructed, it uses data from the [`DataProvider`], selected [`Locale`] and provided options to
/// When constructed, it uses data from the [data provider], selected [`Locale`] and provided options to
/// collect all data necessary to format any dates into that locale.
///
/// For that reason, one should think of the process of formatting a date in two steps - first, a computational
Expand Down Expand Up @@ -56,12 +56,13 @@ use crate::{date::DateTimeInput, CldrCalendar, DateTimeFormatError, FormattedDat
/// let value = dtf.format_to_string(&datetime);
/// ```
///
/// This model replicates that of `ICU` and `ECMA402`. In the future this will become even more pronounced
/// when we introduce asynchronous [`DataProvider`] and corresponding asynchronous constructor.
/// This model replicates that of `ICU` and `ECMA402`.
///
/// [data provider]: icu_provider
pub struct DateTimeFormat<C>(pub(super) raw::DateTimeFormat, PhantomData<C>);

impl<C: CldrCalendar> DateTimeFormat<C> {
/// Constructor that takes a selected [`Locale`], reference to a [`DataProvider`] and
/// Constructor that takes a selected [`Locale`], reference to a [data provider] and
/// a list of options, then collects all data necessary to format date and time values into the given locale.
///
/// # Examples
Expand All @@ -83,17 +84,19 @@ impl<C: CldrCalendar> DateTimeFormat<C> {
///
/// assert_eq!(dtf.is_ok(), true);
/// ```
///
/// [data provider]: icu_provider
#[inline]
pub fn try_new<T: Into<Locale>, D>(
locale: T,
data_provider: &D,
options: &DateTimeFormatOptions,
) -> Result<Self, DateTimeFormatError>
where
D: DataProvider<DateSymbolsV1Marker>
+ DataProvider<DatePatternsV1Marker>
+ DataProvider<DateSkeletonPatternsV1Marker>
+ DataProvider<PluralRulesV1Marker>,
D: ResourceProvider<DateSymbolsV1Marker>
+ ResourceProvider<DatePatternsV1Marker>
+ ResourceProvider<DateSkeletonPatternsV1Marker>
+ ResourceProvider<OrdinalV1Marker>,
{
Ok(Self(
raw::DateTimeFormat::try_new(locale, data_provider, options, C::IDENTIFIER)?,
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum DateTimeFormatError {
/// An error originating from the [`Write`](std::fmt::Write) trait.
#[displaydoc("{0}")]
Format(core::fmt::Error),
/// An error originating inside of the [`DataProvider`](icu_provider::DataProvider).
/// An error originating inside of the [data provider](icu_provider).
#[displaydoc("{0}")]
DataProvider(DataError),
/// An error originating from a missing field in datetime input.
Expand Down
12 changes: 5 additions & 7 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,14 +359,12 @@ mod tests {

let provider = icu_testdata::get_provider();
let data: DataPayload<DateSymbolsV1Marker> = provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::DATE_SYMBOLS_V1,
options: ResourceOptions {
variant: Some("gregory".into()),
langid: Some("en".parse().unwrap()),
},
.load_resource(&DataRequest {
options: ResourceOptions {
variant: Some("gregory".into()),
langid: Some("en".parse().unwrap()),
},
metadata: Default::default(),
})
.unwrap()
.take_payload()
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! This API provides necessary functionality for formatting date and time to user readable textual representation.
//!
//! [`DateTimeFormat`] is the main structure of the component. It accepts a set of arguments which
//! allow it to collect necessary data from the [`DataProvider`], and once instantiated, can be
//! allow it to collect necessary data from the [data provider], and once instantiated, can be
//! used to quickly format any date and time provided.
//!
//! # Examples
Expand Down Expand Up @@ -70,7 +70,7 @@
//! to develop core date and time APIs that will work as an input for this component. [`DateTime`] additionally
//! has support for non-Gregorian calendars, which this module will eventually be able to format.
//!
//! [`DataProvider`]: icu_provider::DataProvider
//! [data provider]: icu_provider
//! [`ICU4X`]: ../icu/index.html
//! [`Length`]: options::length
//! [`DateTime`]: icu_calendar::DateTime
Expand Down
8 changes: 3 additions & 5 deletions components/datetime/src/provider/calendar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ mod skeletons;
mod symbols;

use crate::pattern;
use icu_provider::{
yoke::{self, *},
DataMarker,
};
use icu_provider::prelude::*;
use icu_provider::yoke;
pub use skeletons::*;
pub use symbols::*;

#[icu_provider::data_struct]
#[icu_provider::data_struct(DatePatternsV1Marker = "datetime/lengths@1")]
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
4 changes: 2 additions & 2 deletions components/datetime/src/provider/calendar/skeletons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::{
skeleton::{reference::Skeleton, SkeletonError},
};
use core::convert::TryFrom;
use icu_provider::yoke::{self, *};
use icu_provider::yoke;
use litemap::LiteMap;

#[icu_provider::data_struct]
#[icu_provider::data_struct(DateSkeletonPatternsV1Marker = "datetime/skeletons@1")]
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/provider/calendar/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::borrow::Cow;
use icu_provider::yoke::{self, *};
use zerovec::map::ZeroMap;

#[icu_provider::data_struct]
#[icu_provider::data_struct(DateSymbolsV1Marker = "datetime/symbols@1")]
#[derive(Debug, PartialEq, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
26 changes: 11 additions & 15 deletions components/datetime/src/provider/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<D> Clone for PatternSelector<'_, D> {

impl<D> PatternSelector<'_, D>
where
D: DataProvider<DatePatternsV1Marker> + DataProvider<DateSkeletonPatternsV1Marker>,
D: ResourceProvider<DatePatternsV1Marker> + ResourceProvider<DateSkeletonPatternsV1Marker>,
{
pub(crate) fn for_options<'a>(
data_provider: &'a D,
Expand Down Expand Up @@ -208,14 +208,12 @@ where
fn patterns_data_payload(self) -> Result<DataPayload<DatePatternsV1Marker>> {
let data = self
.data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::DATE_PATTERNS_V1,
options: ResourceOptions {
variant: Some(self.calendar.into()),
langid: Some(self.locale.clone().into()),
},
.load_resource(&DataRequest {
options: ResourceOptions {
variant: Some(self.calendar.into()),
langid: Some(self.locale.clone().into()),
},
metadata: Default::default(),
})?
.take_payload()?;
Ok(data)
Expand All @@ -224,14 +222,12 @@ where
fn skeleton_data_payload(self) -> Result<DataPayload<DateSkeletonPatternsV1Marker>> {
let data = self
.data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::DATE_SKELETON_PATTERNS_V1,
options: ResourceOptions {
variant: Some(self.calendar.into()),
langid: Some(self.locale.clone().into()),
},
.load_resource(&DataRequest {
options: ResourceOptions {
variant: Some(self.calendar.into()),
langid: Some(self.locale.clone().into()),
},
metadata: Default::default(),
})?
.take_payload()?;
Ok(data)
Expand Down
14 changes: 7 additions & 7 deletions components/datetime/src/provider/time_zones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use alloc::borrow::Cow;
use icu_provider::yoke::{self, *};
use icu_provider::yoke;
use litemap::LiteMap;
use tinystr::TinyStr8;

Expand Down Expand Up @@ -84,7 +84,7 @@ macro_rules! map_access_with_overrides {

/// An ICU4X mapping to the CLDR timeZoneNames format strings.
/// See CLDR-JSON timeZoneNames.json for more context.
#[icu_provider::data_struct]
#[icu_provider::data_struct(TimeZoneFormatsV1Marker = "time_zone/formats@1")]
#[derive(PartialEq, Debug, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand All @@ -109,7 +109,7 @@ pub struct TimeZoneFormatsV1<'data> {

/// An ICU4X mapping to the CLDR timeZoneNames exemplar cities.
/// See CLDR-JSON timeZoneNames.json for more context.
#[icu_provider::data_struct]
#[icu_provider::data_struct(ExemplarCitiesV1Marker = "time_zone/exemplar_cities@1")]
#[derive(PartialEq, Debug, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand All @@ -121,7 +121,7 @@ map_access!(ExemplarCitiesV1<'data>[str] => Cow<'data, str>: 'data);

/// An ICU4X mapping to the long-form generic metazone names.
/// See CLDR-JSON timeZoneNames.json for more context.
#[icu_provider::data_struct]
#[icu_provider::data_struct(MetaZoneGenericNamesLongV1Marker = "time_zone/generic_long@1")]
#[derive(PartialEq, Debug, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand All @@ -138,7 +138,7 @@ map_access_with_overrides!(MetaZoneGenericNamesLongV1<'data>[str] => Cow<'data,

/// An ICU4X mapping to the short-form generic metazone names.
/// See CLDR-JSON timeZoneNames.json for more context.
#[icu_provider::data_struct]
#[icu_provider::data_struct(MetaZoneGenericNamesShortV1Marker = "time_zone/generic_short@1")]
#[derive(PartialEq, Debug, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand All @@ -156,7 +156,7 @@ map_access_with_overrides!(MetaZoneGenericNamesShortV1<'data>[str] => Cow<'data,
/// An ICU4X mapping to the long-form specific metazone names.
/// Specific names include time variants such as "daylight."
/// See CLDR-JSON timeZoneNames.json for more context.
#[icu_provider::data_struct]
#[icu_provider::data_struct(MetaZoneSpecificNamesLongV1Marker = "time_zone/specific_long@1")]
#[derive(PartialEq, Debug, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand All @@ -174,7 +174,7 @@ map_access_with_overrides!(MetaZoneSpecificNamesLongV1<'data>[str] => MetaZoneSp
/// An ICU4X mapping to the short-form specific metazone names.
/// Specific names include time variants such as "daylight."
/// See CLDR-JSON timeZoneNames.json for more context.
#[icu_provider::data_struct]
#[icu_provider::data_struct(MetaZoneSpecificNamesShortV1Marker = "time_zone/specific_short@1")]
#[derive(PartialEq, Debug, Clone, Default)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
28 changes: 11 additions & 17 deletions components/datetime/src/raw/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};
use alloc::string::String;
use icu_locid::Locale;
use icu_plurals::{provider::PluralRulesV1Marker, PluralRuleType, PluralRules};
use icu_plurals::{provider::OrdinalV1Marker, PluralRules};
use icu_provider::prelude::*;

use crate::{
Expand Down Expand Up @@ -44,10 +44,10 @@ impl DateTimeFormat {
calendar: &'static str,
) -> Result<Self, DateTimeFormatError>
where
D: DataProvider<DateSymbolsV1Marker>
+ DataProvider<DatePatternsV1Marker>
+ DataProvider<DateSkeletonPatternsV1Marker>
+ DataProvider<PluralRulesV1Marker>,
D: ResourceProvider<DateSymbolsV1Marker>
+ ResourceProvider<DatePatternsV1Marker>
+ ResourceProvider<DateSkeletonPatternsV1Marker>
+ ResourceProvider<OrdinalV1Marker>,
{
let locale = locale.into();

Expand All @@ -64,26 +64,20 @@ impl DateTimeFormat {
let langid: icu_locid::LanguageIdentifier = locale.clone().into();

let ordinal_rules = if let PatternPlurals::MultipleVariants(_) = &patterns.get().0 {
Some(PluralRules::try_new(
locale.clone(),
data_provider,
PluralRuleType::Ordinal,
)?)
Some(PluralRules::try_new_ordinal(locale.clone(), data_provider)?)
} else {
None
};

let symbols_data = if requires_data {
Some(
data_provider
.load_payload(&DataRequest {
resource_path: ResourcePath {
key: provider::key::DATE_SYMBOLS_V1,
options: ResourceOptions {
variant: Some(calendar.into()),
langid: Some(langid),
},
.load_resource(&DataRequest {
options: ResourceOptions {
variant: Some(calendar.into()),
langid: Some(langid),
},
metadata: Default::default(),
})?
.take_payload()?,
)
Expand Down
Loading

0 comments on commit 20787e7

Please sign in to comment.