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

Baked data for icu_relativetime #3567

Merged
merged 1 commit into from
Jun 22, 2023
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
3 changes: 2 additions & 1 deletion Cargo.lock

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

8 changes: 5 additions & 3 deletions experimental/relativetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ license = "Unicode-DFS-2016"
categories = ["internationalization"]
# Keep this in sync with other crates unless there are exceptions
include = [
"data/*",
"src/**/*",
"examples/**/*",
"benches/**/*",
Expand Down Expand Up @@ -43,14 +42,17 @@ zerovec = { version = "0.9.4", path = "../../utils/zerovec", features = ["yoke"]
databake = { version = "0.1.3", path = "../../utils/databake", features = ["derive"], optional = true}
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true }

icu_relativetime_data = { path = "data", optional = true }
icu_locid_transform = { path = "../../components/locid_transform", features = ["data"], optional = true }

[dev-dependencies]
icu = { path = "../../components/icu", features = ["icu_relativetime"] }
icu_testdata = { path = "../../provider/testdata", default-features = false, features = ["icu_plurals", "icu_decimal", "icu_relativetime", "icu_locid_transform"] }

[features]
std = ["fixed_decimal/std", "icu_decimal/std", "icu_plurals/std", "icu_provider/std"]
serde = ["dep:serde", "zerovec/serde"]
serde = ["dep:serde", "zerovec/serde", "icu_provider/serde", "icu_plurals/serde", "icu_decimal/serde"]
datagen = ["std", "serde", "dep:databake", "zerovec/databake"]
data = ["dep:icu_relativetime_data", "dep:icu_locid_transform", "icu_decimal/data", "icu_plurals/data"]

[[test]]
name = "tests"
Expand Down
34 changes: 34 additions & 0 deletions experimental/relativetime/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ use alloc::borrow::Cow;
use icu_provider::prelude::*;
use zerovec::ZeroMap;

#[cfg(feature = "data")]
#[derive(Debug)]
/// Baked data
pub struct Baked;

#[cfg(feature = "data")]
const _: () = {
use crate as icu_relativetime;
icu_relativetime_data::impl_relativetime_long_day_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_hour_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_minute_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_month_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_quarter_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_second_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_week_v1!(Baked);
icu_relativetime_data::impl_relativetime_long_year_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_day_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_hour_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_minute_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_month_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_quarter_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_second_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_week_v1!(Baked);
icu_relativetime_data::impl_relativetime_narrow_year_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_day_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_hour_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_minute_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_month_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_quarter_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_second_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_week_v1!(Baked);
icu_relativetime_data::impl_relativetime_short_year_v1!(Baked);
};

/// Relative time format V1 data struct.

#[icu_provider::data_struct(
Expand Down
140 changes: 128 additions & 12 deletions experimental/relativetime/src/relativetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ use crate::{options::RelativeTimeFormatterOptions, RelativeTimeError};
/// use writeable::assert_writeable_eq;
///
/// let relative_time_formatter =
/// RelativeTimeFormatter::try_new_long_second_unstable(
/// &icu_testdata::unstable(),
/// RelativeTimeFormatter::try_new_long_second(
/// &locale!("en").into(),
/// RelativeTimeFormatterOptions::default(),
/// )
/// .expect("Data should load successfully.");
/// .expect("locale should be present");
///
/// assert_writeable_eq!(
/// relative_time_formatter.format(FixedDecimal::from(5i8)),
Expand All @@ -55,14 +54,13 @@ use crate::{options::RelativeTimeFormatterOptions, RelativeTimeError};
/// use writeable::assert_writeable_eq;
///
/// let relative_time_formatter =
/// RelativeTimeFormatter::try_new_short_day_unstable(
/// &icu_testdata::unstable(),
/// RelativeTimeFormatter::try_new_short_day(
/// &locale!("es").into(),
/// RelativeTimeFormatterOptions {
/// numeric: Numeric::Auto,
/// },
/// )
/// .expect("Data should load successfully.");
/// .expect("locale should be present");
///
/// assert_writeable_eq!(
/// relative_time_formatter.format(FixedDecimal::from(0u8)),
Expand Down Expand Up @@ -93,12 +91,11 @@ use crate::{options::RelativeTimeFormatterOptions, RelativeTimeError};
/// use writeable::assert_writeable_eq;
///
/// let relative_time_formatter =
/// RelativeTimeFormatter::try_new_narrow_year_unstable(
/// &icu_testdata::unstable(),
/// RelativeTimeFormatter::try_new_narrow_year(
/// &locale!("bn").into(),
/// RelativeTimeFormatterOptions::default(),
/// )
/// .expect("Data should load successfully.");
/// .expect("locale should be present");
///
/// assert_writeable_eq!(
/// relative_time_formatter.format(FixedDecimal::from(3u8)),
Expand All @@ -117,9 +114,56 @@ pub struct RelativeTimeFormatter {
}

macro_rules! constructor {
($name: ident, $marker: ty) => {
#[doc = concat!("Create a new [`RelativeTimeFormatter`]")]
pub fn $name<D>(
($unstable: ident, $baked: ident, $any: ident, $buffer: ident, $marker: ty) => {

/// Create a new [`RelativeTimeFormatter`]
///
/// ✨ **Enabled with the `"data"` feature.**
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
#[cfg(feature = "data")]
pub fn $baked(
locale: &DataLocale,
options: RelativeTimeFormatterOptions,
) -> Result<Self, RelativeTimeError> {
let plural_rules = PluralRules::try_new_cardinal(locale)?;
// Initialize FixedDecimalFormatter with default options
let fixed_decimal_format = FixedDecimalFormatter::try_new(
locale,
FixedDecimalFormatterOptions::default(),
)?;
let rt: DataPayload<$marker> = crate::provider::Baked
.load(DataRequest {
locale,
metadata: Default::default(),
})?
.take_payload()?;
let rt = rt.cast();
Ok(RelativeTimeFormatter {
plural_rules,
options,
rt,
fixed_decimal_format,
})
}

icu_provider::gen_any_buffer_data_constructors!(
locale: include,
options: RelativeTimeFormatterOptions,
error: RelativeTimeError,
#[cfg(skip)]
functions: [
$baked,
$any,
$buffer,
$unstable,
Self,
]
);


#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::$baked)]
pub fn $unstable<D>(
data_provider: &D,
locale: &DataLocale,
options: RelativeTimeFormatterOptions,
Expand Down Expand Up @@ -157,98 +201,170 @@ macro_rules! constructor {
impl RelativeTimeFormatter {
constructor!(
try_new_long_second_unstable,
try_new_long_second,
try_new_long_second_with_any_provider,
try_new_long_second_with_buffer_provider,
LongSecondRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_minute_unstable,
try_new_long_minute,
try_new_long_minute_with_any_provider,
try_new_long_minute_with_buffer_provider,
LongMinuteRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_hour_unstable,
try_new_long_hour,
try_new_long_hour_with_any_provider,
try_new_long_hour_with_buffer_provider,
LongHourRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_day_unstable,
try_new_long_day,
try_new_long_day_with_any_provider,
try_new_long_day_with_buffer_provider,
LongDayRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_week_unstable,
try_new_long_week,
try_new_long_week_with_any_provider,
try_new_long_week_with_buffer_provider,
LongWeekRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_month_unstable,
try_new_long_month,
try_new_long_month_with_any_provider,
try_new_long_month_with_buffer_provider,
LongMonthRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_quarter_unstable,
try_new_long_quarter,
try_new_long_quarter_with_any_provider,
try_new_long_quarter_with_buffer_provider,
LongQuarterRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_long_year_unstable,
try_new_long_year,
try_new_long_year_with_any_provider,
try_new_long_year_with_buffer_provider,
LongYearRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_second_unstable,
try_new_short_second,
try_new_short_second_with_any_provider,
try_new_short_second_with_buffer_provider,
ShortSecondRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_minute_unstable,
try_new_short_minute,
try_new_short_minute_with_any_provider,
try_new_short_minute_with_buffer_provider,
ShortMinuteRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_hour_unstable,
try_new_short_hour,
try_new_short_hour_with_any_provider,
try_new_short_hour_with_buffer_provider,
ShortHourRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_day_unstable,
try_new_short_day,
try_new_short_day_with_any_provider,
try_new_short_day_with_buffer_provider,
ShortDayRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_week_unstable,
try_new_short_week,
try_new_short_week_with_any_provider,
try_new_short_week_with_buffer_provider,
ShortWeekRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_month_unstable,
try_new_short_month,
try_new_short_month_with_any_provider,
try_new_short_month_with_buffer_provider,
ShortMonthRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_quarter_unstable,
try_new_short_quarter,
try_new_short_quarter_with_any_provider,
try_new_short_quarter_with_buffer_provider,
ShortQuarterRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_short_year_unstable,
try_new_short_year,
try_new_short_year_with_any_provider,
try_new_short_year_with_buffer_provider,
ShortYearRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_second_unstable,
try_new_narrow_second,
try_new_narrow_second_with_any_provider,
try_new_narrow_second_with_buffer_provider,
NarrowSecondRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_minute_unstable,
try_new_narrow_minute,
try_new_narrow_minute_with_any_provider,
try_new_narrow_minute_with_buffer_provider,
NarrowMinuteRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_hour_unstable,
try_new_narrow_hour,
try_new_narrow_hour_with_any_provider,
try_new_narrow_hour_with_buffer_provider,
NarrowHourRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_day_unstable,
try_new_narrow_day,
try_new_narrow_day_with_any_provider,
try_new_narrow_day_with_buffer_provider,
NarrowDayRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_week_unstable,
try_new_narrow_week,
try_new_narrow_week_with_any_provider,
try_new_narrow_week_with_buffer_provider,
NarrowWeekRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_month_unstable,
try_new_narrow_month,
try_new_narrow_month_with_any_provider,
try_new_narrow_month_with_buffer_provider,
NarrowMonthRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_quarter_unstable,
try_new_narrow_quarter,
try_new_narrow_quarter_with_any_provider,
try_new_narrow_quarter_with_buffer_provider,
NarrowQuarterRelativeTimeFormatDataV1Marker
);
constructor!(
try_new_narrow_year_unstable,
try_new_narrow_year,
try_new_narrow_year_with_any_provider,
try_new_narrow_year_with_buffer_provider,
NarrowYearRelativeTimeFormatDataV1Marker
);

Expand Down
Loading