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

Put date skeletons behind a feature #2370

Merged
merged 18 commits into from
Aug 16, 2022
Merged
1 change: 0 additions & 1 deletion Cargo.lock

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

19 changes: 15 additions & 4 deletions components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ fixed_decimal = { version = "0.4", path = "../../utils/fixed_decimal" }

[dev-dependencies]
criterion = "0.3"
icu = { path = "../icu", default-features = false, features = ["experimental"] }
icu = { path = "../icu" }
icu_benchmark_macros = { version = "0.7", path = "../../tools/benchmark/macros" }
icu_provider = { version = "1.0.0-beta1", path = "../../provider/core" }
icu_provider_adapters = { path = "../../provider/adapters" }
icu_testdata = { path = "../../provider/testdata", features = ["baked"] }
icu_testdata = { path = "../../provider/testdata" }
icu_calendar = { version = "1.0.0-beta1", path = "../calendar", features = ["serde"] }
icu_datetime = { path = "../../components/datetime", features = ["datagen"] }

serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand All @@ -72,8 +71,12 @@ bench = false # This option is required for Benchmark CI
std = ["icu_provider/std", "icu_locid/std", "icu_calendar/std"]
default = []
bench = ["serde"]
# experimental_skeleton_matching is the minimal set of skeleton features required for datagen.
experimental_skeleton_matching = []
# experimental is the full set of skeleton and component APIs.
experimental = ["experimental_skeleton_matching"]
serde = ["dep:serde", "litemap/serde", "zerovec/serde", "tinystr/serde", "smallvec/serde", "icu_calendar/serde", "icu_decimal/serde", "icu_provider/serde", "icu_plurals/serde", "icu_timezone/serde"]
datagen = ["serde", "icu_calendar/datagen", "icu_timezone/datagen", "icu_provider/datagen", "std", "databake"]
datagen = ["serde", "experimental_skeleton_matching", "icu_calendar/datagen", "icu_timezone/datagen", "icu_provider/datagen", "std", "databake"]

[[bench]]
name = "datetime"
Expand All @@ -89,6 +92,14 @@ required-features = ["bench"]
name = "datetime"
required-features = ["serde"]

[[test]]
name = "resolved_components"
required-features = ["experimental"]

[[test]]
name = "skeleton_serialization"
required-features = ["experimental"]

[[example]]
name = "work_log"
required-features = ["serde"]
22 changes: 12 additions & 10 deletions components/datetime/benches/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn datetime_benches(c: &mut Criterion) {
.collect();
for setup in &fx.setups {
let locale: Locale = setup.locale.parse().expect("Failed to parse locale.");
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand All @@ -58,6 +58,8 @@ fn datetime_benches(c: &mut Criterion) {
};

bench_datetime_with_fixture("lengths");

#[cfg(feature = "experimental")]
bench_datetime_with_fixture("components");

let fxs = fixtures::get_fixture("lengths_with_zones").unwrap();
Expand All @@ -71,7 +73,7 @@ fn datetime_benches(c: &mut Criterion) {
.collect();
for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -110,7 +112,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -140,7 +142,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand All @@ -167,7 +169,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -198,7 +200,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -227,7 +229,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -258,7 +260,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -286,7 +288,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down Expand Up @@ -318,7 +320,7 @@ fn datetime_benches(c: &mut Criterion) {

for setup in &fx.setups {
let locale: Locale = setup.locale.parse().unwrap();
let options = fixtures::get_options(&setup.options);
let options = fixtures::get_options(&setup.options).unwrap();
let dtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_unstable(
&provider,
&locale.into(),
Expand Down
9 changes: 6 additions & 3 deletions components/datetime/benches/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ pub fn get_patterns_fixture() -> std::io::Result<structs::PatternsFixture> {
}

#[allow(dead_code)]
pub fn get_options(input: &structs::TestOptions) -> DateTimeFormatterOptions {
pub fn get_options(input: &structs::TestOptions) -> Option<DateTimeFormatterOptions> {
match input {
structs::TestOptions::Length(bag) => (*bag).into(),
structs::TestOptions::Components(bag) => (*bag).into(),
structs::TestOptions::Length(bag) => Some((*bag).into()),
#[cfg(feature = "experimental")]
structs::TestOptions::Components(bag) => Some((*bag).into()),
#[cfg(not(feature = "experimental"))]
structs::TestOptions::Components(_) => None,
}
}
4 changes: 4 additions & 0 deletions components/datetime/benches/fixtures/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ pub enum TestOptions {
#[serde(rename = "length")]
Length(options::length::Bag),
#[serde(rename = "components")]
#[cfg(feature = "experimental")]
Components(options::components::Bag),
#[serde(rename = "components")]
#[cfg(not(feature = "experimental"))]
Components(serde_json::Value),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down
1 change: 0 additions & 1 deletion components/datetime/src/any/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ impl DateFormatter {
where
P: DataProvider<TimeSymbolsV1Marker>
+ DataProvider<TimeLengthsV1Marker>
+ DataProvider<DateSkeletonPatternsV1Marker>
+ DataProvider<OrdinalV1Marker>
+ DataProvider<WeekDataV1Marker>
+ DataProvider<DecimalSymbolsV1Marker>
Expand Down
83 changes: 74 additions & 9 deletions components/datetime/src/any/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::{
calendar,
options::{components, DateTimeFormatterOptions},
raw, DateFormatter, TimeFormatter,
};
use crate::{calendar, options::DateTimeFormatterOptions, raw, DateFormatter, TimeFormatter};
use alloc::string::String;

use icu_provider::prelude::*;

use crate::provider::{calendar::*, week_data::WeekDataV1Marker};
#[cfg(feature = "experimental")]
use crate::options::components;
use crate::provider::{calendar::*, date_time::PatternSelector, week_data::WeekDataV1Marker};
use crate::{input::DateTimeInput, DateTimeFormatterError, FormattedDateTime};
use icu_calendar::any_calendar::{AnyCalendar, AnyCalendarKind};
use icu_calendar::provider::{JapaneseErasV1Marker, JapaneseExtendedErasV1Marker};
Expand Down Expand Up @@ -218,6 +216,7 @@ impl DateTimeFormatter {
/// let value = dtf.format_to_string(&any_datetime).expect("calendars should match");
/// assert_eq!(value, "Sep 1, 2020, 12:34 PM");
/// ```
#[cfg(feature = "experimental")]
#[inline(never)]
pub fn try_new_unstable<P>(
data_provider: &P,
Expand All @@ -227,7 +226,7 @@ impl DateTimeFormatter {
where
P: DataProvider<TimeSymbolsV1Marker>
+ DataProvider<TimeLengthsV1Marker>
+ DataProvider<DateSkeletonPatternsV1Marker>
+ DataProvider<crate::provider::calendar::DateSkeletonPatternsV1Marker>
+ DataProvider<OrdinalV1Marker>
+ DataProvider<WeekDataV1Marker>
+ DataProvider<DecimalSymbolsV1Marker>
Expand Down Expand Up @@ -257,13 +256,77 @@ impl DateTimeFormatter {
let kind = calendar.kind();
kind.set_on_data_locale(&mut locale_with_cal);

let patterns = PatternSelector::for_options(
data_provider,
calendar::load_lengths_for_any_calendar_kind(data_provider, locale, kind)?,
&locale_with_cal,
&options,
)?;

Ok(Self(
raw::DateTimeFormatter::try_new(
data_provider,
patterns,
|| calendar::load_symbols_for_any_calendar_kind(data_provider, locale, kind),
locale_with_cal,
)?,
calendar,
))
}

#[allow(missing_docs)] // The docs use the "experimental" version
#[cfg(not(feature = "experimental"))]
#[inline(never)]
pub fn try_new_unstable<P>(
data_provider: &P,
locale: &DataLocale,
options: DateTimeFormatterOptions,
) -> Result<Self, DateTimeFormatterError>
where
P: DataProvider<TimeSymbolsV1Marker>
+ DataProvider<TimeLengthsV1Marker>
+ DataProvider<OrdinalV1Marker>
+ DataProvider<WeekDataV1Marker>
+ DataProvider<DecimalSymbolsV1Marker>
+ DataProvider<GregorianDateLengthsV1Marker>
+ DataProvider<BuddhistDateLengthsV1Marker>
+ DataProvider<JapaneseDateLengthsV1Marker>
+ DataProvider<JapaneseExtendedDateLengthsV1Marker>
+ DataProvider<CopticDateLengthsV1Marker>
+ DataProvider<IndianDateLengthsV1Marker>
+ DataProvider<EthiopicDateLengthsV1Marker>
+ DataProvider<GregorianDateSymbolsV1Marker>
+ DataProvider<BuddhistDateSymbolsV1Marker>
+ DataProvider<JapaneseDateSymbolsV1Marker>
+ DataProvider<JapaneseExtendedDateSymbolsV1Marker>
+ DataProvider<CopticDateSymbolsV1Marker>
+ DataProvider<IndianDateSymbolsV1Marker>
+ DataProvider<EthiopicDateSymbolsV1Marker>
+ DataProvider<JapaneseErasV1Marker>
+ DataProvider<JapaneseExtendedErasV1Marker>
+ ?Sized,
{
// TODO(#2188): Avoid cloning the DataLocale by passing the calendar
// separately into the raw formatter.
let mut locale_with_cal = locale.clone();

let calendar = AnyCalendar::try_new_for_locale_unstable(data_provider, &locale_with_cal)?;
let kind = calendar.kind();
kind.set_on_data_locale(&mut locale_with_cal);

let patterns = PatternSelector::for_options(
data_provider,
calendar::load_lengths_for_any_calendar_kind(data_provider, locale, kind)?,
&locale_with_cal,
&options,
)?;

Ok(Self(
raw::DateTimeFormatter::try_new(
data_provider,
calendar::load_lengths_for_any_calendar_kind(data_provider, locale, kind)?,
patterns,
|| calendar::load_symbols_for_any_calendar_kind(data_provider, locale, kind),
locale_with_cal,
options,
)?,
calendar,
))
Expand Down Expand Up @@ -410,6 +473,7 @@ where {
///
/// assert_eq!(dtf.resolve_components(), expected_components_bag);
/// ```
#[cfg(feature = "experimental")]
pub fn resolve_components(&self) -> components::Bag {
self.0.resolve_components()
}
Expand Down Expand Up @@ -449,6 +513,7 @@ where {
}

#[cfg(test)]
#[cfg(feature = "serde")]
mod tests {
use core::str::FromStr;
use icu::calendar::{AnyCalendar, DateTime};
Expand Down
Loading