Skip to content

Commit

Permalink
Fix remaining preferences & options in constructors (#5852)
Browse files Browse the repository at this point in the history
Partially fixes #5839

---------

Co-authored-by: Shane F. Carr <shane@unicode.org>
  • Loading branch information
zbraniecki and sffc authored Nov 22, 2024
1 parent 3b3fddf commit d38dd38
Show file tree
Hide file tree
Showing 17 changed files with 201 additions and 121 deletions.
27 changes: 18 additions & 9 deletions components/calendar/src/week_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ use crate::{
provider::*,
types::{DayOfMonth, DayOfYearInfo, IsoWeekday, WeekOfMonth},
};
use icu_locale_core::preferences::define_preferences;
use icu_provider::prelude::*;

/// Minimum number of days in a month unit required for using this module
pub const MIN_UNIT_DAYS: u16 = 14;

define_preferences!(
/// The preferences for the week calculator.
[Copy]
WeekPreferences,
{}
);

/// Calculator for week-of-month and week-of-year based on locale-specific configurations.
///
/// Note that things get subtly tricky for weeks that straddle the boundary between two years: different locales
Expand All @@ -32,18 +40,19 @@ pub struct WeekCalculator {

impl WeekCalculator {
icu_provider::gen_any_buffer_data_constructors!(
(locale) -> error: DataError,
(prefs: WeekPreferences) -> error: DataError,
/// Creates a new [`WeekCalculator`] from compiled data.
);

#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<P>(provider: &P, locale: &DataLocale) -> Result<Self, DataError>
pub fn try_new_unstable<P>(provider: &P, prefs: WeekPreferences) -> Result<Self, DataError>
where
P: DataProvider<crate::provider::WeekDataV2Marker> + ?Sized,
{
let locale = DataLocale::from_preferences_locale::<WeekDataV2Marker>(prefs.locale_prefs);
provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(locale),
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})
.map(|response| WeekCalculator {
Expand All @@ -68,7 +77,7 @@ impl WeekCalculator {
/// use icu::calendar::week::WeekCalculator;
///
/// let week_calculator =
/// WeekCalculator::try_new(&icu::locale::locale!("und-GB").into())
/// WeekCalculator::try_new(icu::locale::locale!("und-GB").into())
/// .expect("locale should be present");
///
/// // Wednesday the 10th is in week 2:
Expand Down Expand Up @@ -97,7 +106,7 @@ impl WeekCalculator {
/// use icu::calendar::Date;
///
/// let week_calculator =
/// WeekCalculator::try_new(&icu::locale::locale!("und-GB").into())
/// WeekCalculator::try_new(icu::locale::locale!("und-GB").into())
/// .expect("locale should be present");
///
/// let iso_date = Date::try_new_iso(2022, 8, 26).unwrap();
Expand Down Expand Up @@ -668,31 +677,31 @@ fn test_weekend() {
use icu_locale_core::locale;

assert_eq!(
WeekCalculator::try_new(&locale!("und").into())
WeekCalculator::try_new(locale!("und").into())
.unwrap()
.weekend()
.collect::<Vec<_>>(),
vec![IsoWeekday::Saturday, IsoWeekday::Sunday],
);

assert_eq!(
WeekCalculator::try_new(&locale!("und-FR").into())
WeekCalculator::try_new(locale!("und-FR").into())
.unwrap()
.weekend()
.collect::<Vec<_>>(),
vec![IsoWeekday::Saturday, IsoWeekday::Sunday],
);

assert_eq!(
WeekCalculator::try_new(&locale!("und-IQ").into())
WeekCalculator::try_new(locale!("und-IQ").into())
.unwrap()
.weekend()
.collect::<Vec<_>>(),
vec![IsoWeekday::Saturday, IsoWeekday::Friday],
);

assert_eq!(
WeekCalculator::try_new(&locale!("und-IR").into())
WeekCalculator::try_new(locale!("und-IR").into())
.unwrap()
.weekend()
.collect::<Vec<_>>(),
Expand Down
2 changes: 1 addition & 1 deletion components/experimental/src/dimension/units/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub struct UnitsFormatter {

impl UnitsFormatter {
icu_provider::gen_any_buffer_data_constructors!(
(wprefs: UnitsFormatterPreferences, unit: &str, options: super::options::UnitsFormatterOptions) -> error: DataError,
(prefs: UnitsFormatterPreferences, unit: &str, options: super::options::UnitsFormatterOptions) -> error: DataError,
functions: [
try_new: skip,
try_new_with_any_provider,
Expand Down
62 changes: 40 additions & 22 deletions components/experimental/src/displaynames/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ use crate::displaynames::options::*;
use crate::displaynames::provider::*;
use alloc::borrow::Cow;
use alloc::string::String;
use icu_locale_core::preferences::define_preferences;
use icu_locale_core::{
subtags::Language, subtags::Region, subtags::Script, subtags::Variant, LanguageIdentifier,
Locale,
};
use icu_provider::prelude::*;
use potential_utf::PotentialUtf8;

define_preferences!(
/// The preferences for list formatting.
[Copy]
DisplayNamesPreferences,
{}
);

/// Lookup of the locale-specific display names by region code.
///
/// # Example
Expand All @@ -27,7 +35,7 @@ use potential_utf::PotentialUtf8;
///
/// let locale = locale!("en-001").into();
/// let options: DisplayNamesOptions = Default::default();
/// let display_name = RegionDisplayNames::try_new(&locale, options)
/// let display_name = RegionDisplayNames::try_new(locale, options)
/// .expect("Data should load successfully");
///
/// assert_eq!(display_name.of(region!("AE")), Some("United Arab Emirates"));
Expand All @@ -40,7 +48,7 @@ pub struct RegionDisplayNames {

impl RegionDisplayNames {
icu_provider::gen_any_buffer_data_constructors!(
(locale, options: DisplayNamesOptions) -> error: DataError,
(prefs: DisplayNamesPreferences, options: DisplayNamesOptions) -> error: DataError,
/// Creates a new [`RegionDisplayNames`] from locale data and an options bag using compiled data.
functions: [
try_new,
Expand All @@ -54,12 +62,14 @@ impl RegionDisplayNames {
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D: DataProvider<RegionDisplayNamesV1Marker> + ?Sized>(
provider: &D,
locale: &DataLocale,
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<RegionDisplayNamesV1Marker>(prefs.locale_prefs);
let region_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(locale),
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Expand Down Expand Up @@ -94,7 +104,7 @@ impl RegionDisplayNames {
///
/// let locale = locale!("en-001").into();
/// let options: DisplayNamesOptions = Default::default();
/// let display_name = ScriptDisplayNames::try_new(&locale, options)
/// let display_name = ScriptDisplayNames::try_new(locale, options)
/// .expect("Data should load successfully");
///
/// assert_eq!(display_name.of(script!("Maya")), Some("Mayan hieroglyphs"));
Expand All @@ -107,7 +117,7 @@ pub struct ScriptDisplayNames {

impl ScriptDisplayNames {
icu_provider::gen_any_buffer_data_constructors!(
(locale, options: DisplayNamesOptions) -> error: DataError,
(prefs: DisplayNamesPreferences, options: DisplayNamesOptions) -> error: DataError,
/// Creates a new [`ScriptDisplayNames`] from locale data and an options bag using compiled data.
functions: [
try_new,
Expand All @@ -121,12 +131,14 @@ impl ScriptDisplayNames {
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D: DataProvider<ScriptDisplayNamesV1Marker> + ?Sized>(
provider: &D,
locale: &DataLocale,
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<ScriptDisplayNamesV1Marker>(prefs.locale_prefs);
let script_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(locale),
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Expand Down Expand Up @@ -161,7 +173,7 @@ impl ScriptDisplayNames {
///
/// let locale = locale!("en-001").into();
/// let options: DisplayNamesOptions = Default::default();
/// let display_name = VariantDisplayNames::try_new(&locale, options)
/// let display_name = VariantDisplayNames::try_new(locale, options)
/// .expect("Data should load successfully");
///
/// assert_eq!(display_name.of(variant!("POSIX")), Some("Computer"));
Expand All @@ -175,7 +187,7 @@ pub struct VariantDisplayNames {

impl VariantDisplayNames {
icu_provider::gen_any_buffer_data_constructors!(
(locale, options: DisplayNamesOptions) -> error: DataError,
(prefs: DisplayNamesPreferences, options: DisplayNamesOptions) -> error: DataError,
/// Creates a new [`VariantDisplayNames`] from locale data and an options bag using compiled data.
functions: [
try_new,
Expand All @@ -189,12 +201,14 @@ impl VariantDisplayNames {
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D: DataProvider<VariantDisplayNamesV1Marker> + ?Sized>(
provider: &D,
locale: &DataLocale,
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<VariantDisplayNamesV1Marker>(prefs.locale_prefs);
let variant_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(locale),
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Expand Down Expand Up @@ -225,7 +239,7 @@ impl VariantDisplayNames {
///
/// let locale = locale!("en-001").into();
/// let options: DisplayNamesOptions = Default::default();
/// let display_name = LanguageDisplayNames::try_new(&locale, options)
/// let display_name = LanguageDisplayNames::try_new(locale, options)
/// .expect("Data should load successfully");
///
/// assert_eq!(display_name.of(language!("de")), Some("German"));
Expand All @@ -238,7 +252,7 @@ pub struct LanguageDisplayNames {

impl LanguageDisplayNames {
icu_provider::gen_any_buffer_data_constructors!(
(locale, options: DisplayNamesOptions) -> error: DataError,
(prefs: DisplayNamesPreferences, options: DisplayNamesOptions) -> error: DataError,
/// Creates a new [`LanguageDisplayNames`] from locale data and an options bag using compiled data.
functions: [
try_new,
Expand All @@ -252,12 +266,14 @@ impl LanguageDisplayNames {
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D: DataProvider<LanguageDisplayNamesV1Marker> + ?Sized>(
provider: &D,
locale: &DataLocale,
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale =
DataLocale::from_preferences_locale::<LanguageDisplayNamesV1Marker>(prefs.locale_prefs);
let language_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(locale),
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
})?
.payload;
Expand Down Expand Up @@ -296,7 +312,7 @@ impl LanguageDisplayNames {
///
/// let locale = locale!("en-001").into();
/// let options: DisplayNamesOptions = Default::default();
/// let display_name = LocaleDisplayNamesFormatter::try_new(&locale, options)
/// let display_name = LocaleDisplayNamesFormatter::try_new(locale, options)
/// .expect("Data should load successfully");
///
/// assert_eq!(display_name.of(&locale!("en-GB")), "British English");
Expand All @@ -322,7 +338,7 @@ pub struct LocaleDisplayNamesFormatter {

impl LocaleDisplayNamesFormatter {
icu_provider::gen_any_buffer_data_constructors!(
(locale, options: DisplayNamesOptions) -> error: DataError,
(prefs: DisplayNamesPreferences, options: DisplayNamesOptions) -> error: DataError,
/// Creates a new [`LocaleDisplayNamesFormatter`] from locale data and an options bag using compiled data.
functions: [
try_new,
Expand All @@ -336,7 +352,7 @@ impl LocaleDisplayNamesFormatter {
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
pub fn try_new_unstable<D>(
provider: &D,
locale: &DataLocale,
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError>
where
Expand All @@ -347,8 +363,10 @@ impl LocaleDisplayNamesFormatter {
+ DataProvider<VariantDisplayNamesV1Marker>
+ ?Sized,
{
let locale =
DataLocale::from_preferences_locale::<LocaleDisplayNamesV1Marker>(prefs.locale_prefs);
let req = DataRequest {
id: DataIdentifierBorrowed::for_locale(locale),
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
};

Expand Down Expand Up @@ -503,15 +521,15 @@ fn test_language_display() {
use icu_locale_core::locale;

let dialect = LocaleDisplayNamesFormatter::try_new(
&locale!("en").into(),
locale!("en").into(),
DisplayNamesOptions {
language_display: LanguageDisplay::Dialect,
..Default::default()
},
)
.unwrap();
let standard = LocaleDisplayNamesFormatter::try_new(
&locale!("en").into(),
locale!("en").into(),
DisplayNamesOptions {
language_display: LanguageDisplay::Standard,
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion components/experimental/src/displaynames/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/// let locale = locale!("en-001");
/// let mut options: DisplayNamesOptions = Default::default();
/// options.style = Some(Style::Short);
/// let display_name = RegionDisplayNames::try_new(&locale.into(), options)
/// let display_name = RegionDisplayNames::try_new(locale.into(), options)
/// .expect("Data should load successfully");
///
/// // Full name would be "Bosnia & Herzegovina"
Expand Down
2 changes: 1 addition & 1 deletion components/experimental/tests/displaynames/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn test_concatenate() {
let locale = locale!("en-001");
let options: DisplayNamesOptions = Default::default();

let display_name = LocaleDisplayNamesFormatter::try_new(&locale.into(), options)
let display_name = LocaleDisplayNamesFormatter::try_new(locale.into(), options)
.expect("Data should load successfully");

let result = display_name.of(cas.input_1);
Expand Down
4 changes: 2 additions & 2 deletions components/plurals/README.md

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

10 changes: 6 additions & 4 deletions components/plurals/benches/pluralrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ fn pluralrules(c: &mut Criterion) {
c.bench_function("plurals/pluralrules/overview", |b| {
b.iter(|| {
for lang in &plurals_data.langs {
let pr = PluralRules::try_new(lang.into(), PluralRuleType::Cardinal).unwrap();
let pr =
PluralRules::try_new(lang.into(), PluralRuleType::Cardinal.into()).unwrap();
for s in &numbers_data.usize {
let _ = pr.category_for(*s);
}
Expand All @@ -35,13 +36,14 @@ fn pluralrules(c: &mut Criterion) {
c.bench_function("plurals/pluralrules/construct/fs", |b| {
b.iter(|| {
for lang in &plurals_data.langs {
PluralRules::try_new(lang.into(), PluralRuleType::Ordinal).unwrap();
PluralRules::try_new(lang.into(), PluralRuleType::Cardinal).unwrap();
PluralRules::try_new(lang.into(), PluralRuleType::Ordinal.into()).unwrap();
PluralRules::try_new(lang.into(), PluralRuleType::Cardinal.into()).unwrap();
}
});
});

let pr = PluralRules::try_new(locale!("ru").into(), PluralRuleType::Cardinal).unwrap();
let pr =
PluralRules::try_new(locale!("ru").into(), PluralRuleType::Cardinal.into()).unwrap();
c.bench_function("plurals/pluralrules/select/fs", |b| {
b.iter(|| {
for s in &numbers_data.usize {
Expand Down
Loading

0 comments on commit d38dd38

Please sign in to comment.