Skip to content

Commit

Permalink
Make LocalePreferences internally-private, move DataLocale to locale_…
Browse files Browse the repository at this point in the history
…core, other refactors (#5988)

Fixes #5785.
  • Loading branch information
Manishearth authored Jan 15, 2025
1 parent 7b823de commit e9e0819
Show file tree
Hide file tree
Showing 40 changed files with 596 additions and 626 deletions.
2 changes: 1 addition & 1 deletion components/calendar/src/any_calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ impl AnyCalendarKind {
if let Some(kind) = Self::get_for_prefs(prefs) {
kind
} else {
let lang = prefs.locale_preferences.language;
let lang = prefs.locale_preferences.language();
if lang == language!("th") {
Self::Buddhist
} else if lang == language!("sa") {
Expand Down
3 changes: 1 addition & 2 deletions components/calendar/src/week_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ impl WeekCalculator {
where
P: DataProvider<crate::provider::WeekDataV2Marker> + ?Sized,
{
let locale =
DataLocale::from_preferences_locale::<WeekDataV2Marker>(prefs.locale_preferences);
let locale = WeekDataV2Marker::make_locale(prefs.locale_preferences);
provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down
6 changes: 2 additions & 4 deletions components/collator/src/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ impl LocaleSpecificDataHolder {
.map(|c| DataMarkerAttributes::from_str_or_panic(c.as_str()))
.unwrap_or_default();

let data_locale = DataLocale::from_preferences_locale::<CollationTailoringV1Marker>(
prefs.locale_preferences,
);
let id = DataIdentifierCow::from_borrowed_and_owned(marker_attributes, data_locale.clone());
let data_locale = CollationTailoringV1Marker::make_locale(prefs.locale_preferences);
let id = DataIdentifierCow::from_borrowed_and_owned(marker_attributes, data_locale);

let req = DataRequest {
id: id.as_borrowed(),
Expand Down
18 changes: 9 additions & 9 deletions components/collator/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,9 +1449,9 @@ fn test_nb_nn_no() {
DataProvider::<CollationTailoringV1Marker>::load(
&icu_collator::provider::Baked,
DataRequest {
id: DataIdentifierCow::from_locale(DataLocale::from_preferences_locale::<
CollationTailoringV1Marker,
>(prefs.locale_preferences))
id: DataIdentifierCow::from_locale(CollationTailoringV1Marker::make_locale(
prefs.locale_preferences
))
.as_borrowed(),
..Default::default()
}
Expand All @@ -1472,9 +1472,9 @@ fn test_nb_nn_no() {
DataProvider::<CollationTailoringV1Marker>::load(
&icu_collator::provider::Baked,
DataRequest {
id: DataIdentifierCow::from_locale(DataLocale::from_preferences_locale::<
CollationTailoringV1Marker,
>(prefs.locale_preferences))
id: DataIdentifierCow::from_locale(CollationTailoringV1Marker::make_locale(
prefs.locale_preferences
))
.as_borrowed(),
..Default::default()
}
Expand All @@ -1495,9 +1495,9 @@ fn test_nb_nn_no() {
DataProvider::<CollationTailoringV1Marker>::load(
&icu_collator::provider::Baked,
DataRequest {
id: DataIdentifierCow::from_locale(DataLocale::from_preferences_locale::<
CollationTailoringV1Marker,
>(prefs.locale_preferences))
id: DataIdentifierCow::from_locale(CollationTailoringV1Marker::make_locale(
prefs.locale_preferences
))
.as_borrowed(),
..Default::default()
}
Expand Down
3 changes: 2 additions & 1 deletion components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ define_preferences!(
///
/// let prefs1: DateTimeFormatterPreferences = Locale::try_from_str("fr-u-ca-buddhist-hc-h12").unwrap().into();
///
/// let locale = Locale::try_from_str("fr").unwrap();
/// let mut prefs2 = DateTimeFormatterPreferences::default();
/// prefs2.locale_preferences.language = Language::try_from_str("fr").unwrap();
/// prefs2.locale_preferences = (&locale).into();
/// prefs2.hour_cycle = Some(HourCycle::H12);
/// prefs2.calendar_algorithm = Some(CalendarAlgorithm::Buddhist);
///
Expand Down
4 changes: 1 addition & 3 deletions components/decimal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ impl FixedDecimalFormatter {
prefs: FixedDecimalFormatterPreferences,
options: options::FixedDecimalFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<provider::DecimalSymbolsV2Marker>(
prefs.locale_preferences,
);
let locale = provider::DecimalSymbolsV2Marker::make_locale(prefs.locale_preferences);
let provided_nu = prefs.numbering_system.as_ref().map(|s| s.as_str());

// In case the user explicitly specified a numbering system, use digits from that numbering system. In case of explicitly specified numbering systems,
Expand Down
16 changes: 4 additions & 12 deletions components/experimental/src/compactdecimal/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ impl CompactDecimalFormatter {
prefs: CompactDecimalFormatterPreferences,
options: CompactDecimalFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<ShortCompactDecimalFormatDataV1Marker>(
prefs.locale_preferences,
);
let locale = ShortCompactDecimalFormatDataV1Marker::make_locale(prefs.locale_preferences);
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new(
(&prefs).into(),
Expand Down Expand Up @@ -154,9 +152,7 @@ impl CompactDecimalFormatter {
+ DataProvider<icu_plurals::provider::CardinalV1Marker>
+ ?Sized,
{
let locale = DataLocale::from_preferences_locale::<ShortCompactDecimalFormatDataV1Marker>(
prefs.locale_preferences,
);
let locale = ShortCompactDecimalFormatDataV1Marker::make_locale(prefs.locale_preferences);
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new_unstable(
provider,
Expand Down Expand Up @@ -200,9 +196,7 @@ impl CompactDecimalFormatter {
prefs: CompactDecimalFormatterPreferences,
options: CompactDecimalFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<LongCompactDecimalFormatDataV1Marker>(
prefs.locale_preferences,
);
let locale = LongCompactDecimalFormatDataV1Marker::make_locale(prefs.locale_preferences);
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new(
(&prefs).into(),
Expand Down Expand Up @@ -245,9 +239,7 @@ impl CompactDecimalFormatter {
+ DataProvider<icu_plurals::provider::CardinalV1Marker>
+ ?Sized,
{
let locale = DataLocale::from_preferences_locale::<LongCompactDecimalFormatDataV1Marker>(
prefs.locale_preferences,
);
let locale = LongCompactDecimalFormatDataV1Marker::make_locale(prefs.locale_preferences);
Ok(Self {
fixed_decimal_formatter: FixedDecimalFormatter::try_new_unstable(
provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ impl CompactCurrencyFormatter {
prefs: CompactCurrencyFormatterPreferences,
options: CompactCurrencyFormatterOptions,
) -> Result<Self, DataError> {
let short_locale = DataLocale::from_preferences_locale::<ShortCurrencyCompactV1Marker>(
prefs.locale_preferences,
);
let short_locale = ShortCurrencyCompactV1Marker::make_locale(prefs.locale_preferences);

let short_currency_compact = crate::provider::Baked
.load(DataRequest {
Expand All @@ -101,9 +99,7 @@ impl CompactCurrencyFormatter {
})?
.payload;

let essential_locale = DataLocale::from_preferences_locale::<CurrencyEssentialsV1Marker>(
prefs.locale_preferences,
);
let essential_locale = CurrencyEssentialsV1Marker::make_locale(prefs.locale_preferences);

let essential = crate::provider::Baked
.load(DataRequest {
Expand Down Expand Up @@ -140,9 +136,7 @@ impl CompactCurrencyFormatter {
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>
+ DataProvider<icu_plurals::provider::CardinalV1Marker>,
{
let locale = DataLocale::from_preferences_locale::<CurrencyEssentialsV1Marker>(
prefs.locale_preferences,
);
let locale = CurrencyEssentialsV1Marker::make_locale(prefs.locale_preferences);

let compact_decimal_formatter = CompactDecimalFormatter::try_new_short_unstable(
provider,
Expand Down
8 changes: 2 additions & 6 deletions components/experimental/src/dimension/currency/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ impl CurrencyFormatter {
prefs: CurrencyFormatterPreferences,
options: super::options::CurrencyFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<CurrencyEssentialsV1Marker>(
prefs.locale_preferences,
);
let locale = CurrencyEssentialsV1Marker::make_locale(prefs.locale_preferences);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new(
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
Expand Down Expand Up @@ -114,9 +112,7 @@ impl CurrencyFormatter {
+ DataProvider<icu_decimal::provider::DecimalSymbolsV2Marker>
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>,
{
let locale = DataLocale::from_preferences_locale::<CurrencyEssentialsV1Marker>(
prefs.locale_preferences,
);
let locale = CurrencyEssentialsV1Marker::make_locale(prefs.locale_preferences);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new_unstable(
provider,
(&prefs).into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ impl LongCompactCurrencyFormatter {
.with_debug_context("failed to get data marker attribute from a `CurrencyCode`")
})?;

let locale = &DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(
prefs.locale_preferences,
);
let locale = &CurrencyPatternsDataV1Marker::make_locale(prefs.locale_preferences);

let extended = crate::provider::Baked
.load(DataRequest {
Expand Down Expand Up @@ -143,9 +141,7 @@ impl LongCompactCurrencyFormatter {
+ DataProvider<icu_plurals::provider::CardinalV1Marker>
+ DataProvider<crate::compactdecimal::provider::LongCompactDecimalFormatDataV1Marker>,
{
let locale = DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(
prefs.locale_preferences,
);
let locale = CurrencyPatternsDataV1Marker::make_locale(prefs.locale_preferences);

let marker_attributes = DataMarkerAttributes::try_from_str(currency_code.0.as_str())
.map_err(|_| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ impl LongCurrencyFormatter {
prefs: CurrencyFormatterPreferences,
currency_code: &CurrencyCode,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(
prefs.locale_preferences,
);
let locale = CurrencyPatternsDataV1Marker::make_locale(prefs.locale_preferences);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new(
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
Expand Down Expand Up @@ -114,9 +112,7 @@ impl LongCurrencyFormatter {
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>
+ DataProvider<icu_plurals::provider::CardinalV1Marker>,
{
let locale = DataLocale::from_preferences_locale::<CurrencyPatternsDataV1Marker>(
prefs.locale_preferences,
);
let locale = CurrencyPatternsDataV1Marker::make_locale(prefs.locale_preferences);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new_unstable(
provider,
(&prefs).into(),
Expand Down
13 changes: 4 additions & 9 deletions components/experimental/src/dimension/percent/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ use icu_decimal::{FixedDecimalFormatter, FixedDecimalFormatterPreferences};
use icu_locale_core::preferences::{
define_preferences, extensions::unicode::keywords::NumberingSystem, prefs_convert,
};
use icu_provider::{
DataError, DataIdentifierBorrowed, DataLocale, DataPayload, DataProvider, DataRequest,
};

use icu_provider::prelude::*;

use super::super::provider::percent::PercentEssentialsV1Marker;
use super::format::FormattedPercent;
Expand Down Expand Up @@ -154,9 +153,7 @@ where
fixed_decimal_formatter: R,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<PercentEssentialsV1Marker>(
prefs.locale_preferences,
);
let locale = PercentEssentialsV1Marker::make_locale(prefs.locale_preferences);
let essential = crate::provider::Baked
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand All @@ -178,9 +175,7 @@ where
fixed_decimal_formatter: R,
options: PercentFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<PercentEssentialsV1Marker>(
prefs.locale_preferences,
);
let locale = PercentEssentialsV1Marker::make_locale(prefs.locale_preferences);
let essential = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down
8 changes: 2 additions & 6 deletions components/experimental/src/dimension/units/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ impl UnitsFormatter {
unit: &str,
options: super::options::UnitsFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<UnitsDisplayNameV1Marker>(
prefs.locale_preferences,
);
let locale = UnitsDisplayNameV1Marker::make_locale(prefs.locale_preferences);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new(
(&prefs).into(),
FixedDecimalFormatterOptions::default(),
Expand Down Expand Up @@ -147,9 +145,7 @@ impl UnitsFormatter {
+ DataProvider<icu_decimal::provider::DecimalDigitsV1Marker>
+ DataProvider<icu_plurals::provider::CardinalV1Marker>,
{
let locale = DataLocale::from_preferences_locale::<UnitsDisplayNameV1Marker>(
prefs.locale_preferences,
);
let locale = UnitsDisplayNameV1Marker::make_locale(prefs.locale_preferences);
let fixed_decimal_formatter = FixedDecimalFormatter::try_new_unstable(
provider,
(&prefs).into(),
Expand Down
20 changes: 5 additions & 15 deletions components/experimental/src/displaynames/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ impl RegionDisplayNames {
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<RegionDisplayNamesV1Marker>(
prefs.locale_preferences,
);
let locale = RegionDisplayNamesV1Marker::make_locale(prefs.locale_preferences);
let region_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down Expand Up @@ -135,9 +133,7 @@ impl ScriptDisplayNames {
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<ScriptDisplayNamesV1Marker>(
prefs.locale_preferences,
);
let locale = ScriptDisplayNamesV1Marker::make_locale(prefs.locale_preferences);
let script_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down Expand Up @@ -206,9 +202,7 @@ impl VariantDisplayNames {
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<VariantDisplayNamesV1Marker>(
prefs.locale_preferences,
);
let locale = VariantDisplayNamesV1Marker::make_locale(prefs.locale_preferences);
let variant_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down Expand Up @@ -272,9 +266,7 @@ impl LanguageDisplayNames {
prefs: DisplayNamesPreferences,
options: DisplayNamesOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<LanguageDisplayNamesV1Marker>(
prefs.locale_preferences,
);
let locale = LanguageDisplayNamesV1Marker::make_locale(prefs.locale_preferences);
let language_data = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down Expand Up @@ -367,9 +359,7 @@ impl LocaleDisplayNamesFormatter {
+ DataProvider<VariantDisplayNamesV1Marker>
+ ?Sized,
{
let locale = DataLocale::from_preferences_locale::<LocaleDisplayNamesV1Marker>(
prefs.locale_preferences,
);
let locale = LocaleDisplayNamesV1Marker::make_locale(prefs.locale_preferences);
let req = DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
..Default::default()
Expand Down
8 changes: 2 additions & 6 deletions components/experimental/src/duration/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ impl DurationFormatter {
prefs: DurationFormatterPreferences,
options: ValidatedDurationFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<provider::DigitalDurationDataV1Marker>(
prefs.locale_preferences,
);
let locale = provider::DigitalDurationDataV1Marker::make_locale(prefs.locale_preferences);
let digital = crate::provider::Baked
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down Expand Up @@ -247,9 +245,7 @@ impl DurationFormatter {
prefs: DurationFormatterPreferences,
options: ValidatedDurationFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<provider::DigitalDurationDataV1Marker>(
prefs.locale_preferences,
);
let locale = provider::DigitalDurationDataV1Marker::make_locale(prefs.locale_preferences);
let digital = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&locale),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn name_order_derive(
let chain_locale_str = chain_locale.write_to_string();

// switch lookup with UND
let mut chain_locale_und = chain_locale.clone();
let mut chain_locale_und = *chain_locale;
chain_locale_und.language = Language::UND;
let chain_locale_und_str = chain_locale_und.write_to_string();

Expand Down
4 changes: 2 additions & 2 deletions components/experimental/src/relativetime/relativetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ macro_rules! constructor {
prefs: RelativeTimeFormatterPreferences,
options: RelativeTimeFormatterOptions,
) -> Result<Self, DataError> {
let locale = DataLocale::from_preferences_locale::<$marker>(prefs.locale_preferences);
let locale = <$marker>::make_locale(prefs.locale_preferences);
let plural_rules = PluralRules::try_new_cardinal((&prefs).into())?;
// Initialize FixedDecimalFormatter with default options
let fixed_decimal_format = FixedDecimalFormatter::try_new(
Expand Down Expand Up @@ -183,7 +183,7 @@ macro_rules! constructor {
+ DataProvider<DecimalSymbolsV2Marker> + DataProvider<DecimalDigitsV1Marker>
+ ?Sized,
{
let locale = DataLocale::from_preferences_locale::<$marker>(prefs.locale_preferences);
let locale = <$marker>::make_locale(prefs.locale_preferences);
let plural_rules = PluralRules::try_new_cardinal_unstable(provider, (&prefs).into())?;
// Initialize FixedDecimalFormatter with default options
let fixed_decimal_format = FixedDecimalFormatter::try_new_unstable(
Expand Down
Loading

0 comments on commit e9e0819

Please sign in to comment.