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

Add Expander argument to LocaleCanonicalizer #5718

Merged
merged 1 commit into from
Oct 22, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `icu_locale`
- New crate
- Allow `LocaleDirectionality` to wrap a `LocaleExpander` with user-controlled storage (https://github.com/unicode-org/icu4x/pull/5704)
- Allow `LocaleCanonicalizer` to wrap a `LocaleExpander` with user-controlled storage (https://github.com/unicode-org/icu4x/pull/5718)
- `icu_locale_core`
- New crate, renamed from `icu_locid`
- Removed `Ord` and `PartialOrd` impl from `extensions::unicode::Unicode` (https://github.com/unicode-org/icu4x/pull/5617)
Expand Down
21 changes: 13 additions & 8 deletions components/locale/src/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ use tinystr::TinyAsciiStr;
///
/// [UTS #35: Annex C, LocaleId Canonicalization]: http://unicode.org/reports/tr35/#LocaleId_Canonicalization
#[derive(Debug)]
pub struct LocaleCanonicalizer {
pub struct LocaleCanonicalizer<Expander = LocaleExpander> {
/// Data to support canonicalization.
aliases: DataPayload<AliasesV2Marker>,
/// Likely subtags implementation for delegation.
expander: LocaleExpander,
expander: Expander,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convention is to use single letters for generics, this looks like there's an Expander type. Can you use E?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used Expander elsewhere in this crate already. We've been trying to use slightly more descriptive generics where possible, e.g. CaseMapCloser takes a CM. I couldn't come up with a good two-letter one for Expander.

cc @sffc

}

fn uts35_rule_matches<'a, I>(
Expand Down Expand Up @@ -197,13 +197,13 @@ fn uts35_check_language_rules(
}

#[cfg(feature = "compiled_data")]
impl Default for LocaleCanonicalizer {
impl Default for LocaleCanonicalizer<LocaleExpander> {
fn default() -> Self {
Self::new()
}
}

impl LocaleCanonicalizer {
impl LocaleCanonicalizer<LocaleExpander> {
/// A constructor which creates a [`LocaleCanonicalizer`] from compiled data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
Expand Down Expand Up @@ -235,14 +235,16 @@ impl LocaleCanonicalizer {
let expander = LocaleExpander::try_new_unstable(provider)?;
Self::try_new_with_expander_unstable(provider, expander)
}
}

impl<Expander: AsRef<LocaleExpander>> LocaleCanonicalizer<Expander> {
/// Creates a [`LocaleCanonicalizer`] with a custom [`LocaleExpander`] and compiled data.
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
#[cfg(feature = "compiled_data")]
pub const fn new_with_expander(expander: LocaleExpander) -> Self {
pub const fn new_with_expander(expander: Expander) -> Self {
Self {
aliases: DataPayload::from_static_ref(
crate::provider::Baked::SINGLETON_ALIASES_V2_MARKER,
Expand All @@ -254,7 +256,7 @@ impl LocaleCanonicalizer {
#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new_with_expander)]
pub fn try_new_with_expander_unstable<P>(
provider: &P,
expander: LocaleExpander,
expander: Expander,
) -> Result<Self, DataError>
where
P: DataProvider<AliasesV2Marker> + ?Sized,
Expand All @@ -264,7 +266,7 @@ impl LocaleCanonicalizer {
Ok(Self { aliases, expander })
}

icu_provider::gen_any_buffer_data_constructors!((options: LocaleExpander) -> error: DataError,
icu_provider::gen_any_buffer_data_constructors!((options: Expander) -> error: DataError,
functions: [
new_with_expander: skip,
try_new_with_expander_with_any_provider,
Expand Down Expand Up @@ -395,7 +397,10 @@ impl LocaleCanonicalizer {
};

locale.id.region = Some(
match (self.expander.maximize(&mut maximized), maximized.region) {
match (
self.expander.as_ref().maximize(&mut maximized),
maximized.region,
) {
(TransformResult::Modified, Some(candidate))
if regions.iter().any(|x| x == candidate) =>
{
Expand Down