Skip to content

Commit

Permalink
Unifying DataLocale construction (#4912)
Browse files Browse the repository at this point in the history
In preparation for #3632 

There are two cases:
* `DataLocales` that are passed into ICU4X constructors:
* These are constructed with `locale!("foo").into()`, or `"foo".parse()`
* When changing a constructor to preferences, the code doesn't need to
be changed if we add a `From<Locale>` impl for the preferences
* `DataLocales` that are put into the `DataRequest::locale` field.
* These are constructed with `langid!("foo").into()`, or `"foo".parse()`
* When changing the field to `&LanguageIdentifier`, these can be
clippy-cleaned-up, as the `.into()` will become redundant.

I've taken care to avoid having intermediate `Locale` or
`LanguageIdentifier` variables, everything that is macro-constructed is
immediately `into()`ed. This will simplify find-and-replace later.
  • Loading branch information
robertbastian authored May 21, 2024
1 parent f5a650a commit 0776faf
Show file tree
Hide file tree
Showing 54 changed files with 347 additions and 361 deletions.
10 changes: 5 additions & 5 deletions components/calendar/src/week_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,34 +711,34 @@ fn test_simple_week_of() {

#[test]
fn test_weekend() {
use icu_locid::langid;
use icu_locid::locale;

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

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

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

assert_eq!(
WeekCalculator::try_new(&langid!("und-IR").into())
WeekCalculator::try_new(&locale!("und-IR").into())
.unwrap()
.weekend()
.collect::<Vec<_>>(),
Expand Down
10 changes: 5 additions & 5 deletions components/collator/README.md

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

10 changes: 5 additions & 5 deletions components/collator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@
//! ```
//! use core::cmp::Ordering;
//! use icu::collator::*;
//! use icu::locid::{locale, Locale};
//! use icu::locid::locale;
//!
//! let locale_es: Locale = locale!("es-u-co-trad");
//! let locale_es = locale!("es-u-co-trad").into();
//! let mut options = CollatorOptions::new();
//! options.strength = Some(Strength::Primary);
//! let collator_es: Collator =
//! Collator::try_new(&locale_es.into(), options).unwrap();
//! Collator::try_new(&locale_es, options).unwrap();
//!
//! // "pollo" > "polvo" in traditional Spanish
//! assert_eq!(collator_es.compare("pollo", "polvo"), Ordering::Greater);
//!
//! let locale_en: Locale = locale!("en");
//! let locale_en = locale!("en").into();
//! let mut options = CollatorOptions::new();
//! options.strength = Some(Strength::Primary);
//! let collator_en: Collator =
//! Collator::try_new(&locale_en.into(), options).unwrap();
//! Collator::try_new(&locale_en, options).unwrap();
//!
//! // "pollo" < "polvo" according to English rules
//! assert_eq!(collator_en.compare("pollo", "polvo"), Ordering::Less);
Expand Down
15 changes: 6 additions & 9 deletions components/collator/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ pub enum Strength {
/// assert_eq!(collator.compare("e", "e"), // Full-width e
/// core::cmp::Ordering::Less);
///
/// let locale = icu::locid::locale!("ja");
/// let locale = icu::locid::locale!("ja").into();
/// let ja_collator =
/// Collator::try_new(&locale.into(),
/// options).unwrap();
/// Collator::try_new(&locale, options).unwrap();
/// assert_eq!(ja_collator.compare("E", "e"),
/// core::cmp::Ordering::Greater);
/// assert_eq!(ja_collator.compare("e", "é"),
Expand Down Expand Up @@ -116,10 +115,9 @@ pub enum Strength {
/// let mut options = CollatorOptions::new();
/// options.strength = Some(Strength::Quaternary);
///
/// let ja_locale = icu::locid::locale!("ja");
/// let ja_locale = icu::locid::locale!("ja").into();
/// let ja_collator =
/// Collator::try_new(&ja_locale.into(),
/// options).unwrap();
/// Collator::try_new(&ja_locale, options).unwrap();
/// assert_eq!(ja_collator.compare("あ", "ア"),
/// core::cmp::Ordering::Less);
/// assert_eq!(ja_collator.compare("ア", "ア"),
Expand Down Expand Up @@ -153,10 +151,9 @@ pub enum Strength {
/// let mut options = CollatorOptions::new();
/// options.strength = Some(Strength::Identical);
///
/// let ja_locale = icu::locid::locale!("ja");
/// let ja_locale = icu::locid::locale!("ja").into();
/// let ja_collator =
/// Collator::try_new(&ja_locale.into(),
/// options).unwrap();
/// Collator::try_new(&ja_locale, options).unwrap();
/// assert_eq!(ja_collator.compare("ア", "ア"),
/// core::cmp::Ordering::Less);
/// assert_eq!(ja_collator.compare("e", "e"), // Full-width e
Expand Down
Loading

0 comments on commit 0776faf

Please sign in to comment.