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 AsRef<LocaleExpander> to LocaleDirectionality #5704

Merged
merged 1 commit into from
Oct 18, 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 @@ -13,6 +13,7 @@
- `icu_experimental`
- `icu_locale`
- New crate
- Allow `LocaleDirectionality` to wrap a `LocaleExpander` with user-controlled storage (https://github.com/unicode-org/icu4x/pull/5704)
- `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
28 changes: 16 additions & 12 deletions components/locale/src/directionality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum Direction {

/// Provides methods to determine the direction of a locale.
///
/// The `Expander` generic parameter wraps a [`LocaleExpander`].
///
/// # Examples
///
/// ```
Expand All @@ -32,32 +34,31 @@ pub enum Direction {
/// assert_eq!(ld.get(&langid!("en")), Some(Direction::LeftToRight));
/// ```
#[derive(Debug)]
pub struct LocaleDirectionality {
pub struct LocaleDirectionality<Expander = LocaleExpander> {
script_direction: DataPayload<ScriptDirectionV1Marker>,
expander: LocaleExpander,
expander: Expander,
}

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

impl LocaleDirectionality {
impl LocaleDirectionality<LocaleExpander> {
/// Creates a [`LocaleDirectionality`] from compiled data.
///
/// This includes limited likely subtags data, see [`LocaleExpander::new()`].
#[cfg(feature = "compiled_data")]
pub const fn new() -> Self {
Self::new_with_expander(LocaleExpander::new())
}

// Note: This is a custom impl because the bounds on `try_new_unstable` don't suffice
#[doc = icu_provider::gen_any_buffer_unstable_docs!(ANY, Self::new)]
pub fn try_new_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
) -> Result<LocaleDirectionality, DataError> {
) -> Result<Self, DataError> {
let expander = LocaleExpander::try_new_with_any_provider(provider)?;
Self::try_new_with_expander_unstable(&provider.as_downcasting(), expander)
}
Expand All @@ -67,11 +68,10 @@ impl LocaleDirectionality {
#[cfg(feature = "serde")]
pub fn try_new_with_buffer_provider(
provider: &(impl BufferProvider + ?Sized),
) -> Result<LocaleDirectionality, DataError> {
) -> Result<Self, DataError> {
let expander = LocaleExpander::try_new_with_buffer_provider(provider)?;
Self::try_new_with_expander_unstable(&provider.as_deserializing(), expander)
}

#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::new)]
pub fn try_new_unstable<P>(provider: &P) -> Result<LocaleDirectionality, DataError>
where
Expand All @@ -83,7 +83,9 @@ impl LocaleDirectionality {
let expander = LocaleExpander::try_new_unstable(provider)?;
Self::try_new_with_expander_unstable(provider, expander)
}
}

impl<Expander: AsRef<LocaleExpander>> LocaleDirectionality<Expander> {
/// Creates a [`LocaleDirectionality`] with a custom [`LocaleExpander`] and compiled data.
///
/// This allows using [`LocaleExpander::new_extended()`] with data for all locales.
Expand All @@ -108,7 +110,7 @@ impl LocaleDirectionality {
/// );
/// ```
#[cfg(feature = "compiled_data")]
pub const fn new_with_expander(expander: LocaleExpander) -> Self {
pub const fn new_with_expander(expander: Expander) -> Self {
LocaleDirectionality {
script_direction: DataPayload::from_static_ref(
crate::provider::Baked::SINGLETON_SCRIPT_DIRECTION_V1_MARKER,
Expand All @@ -120,8 +122,8 @@ impl LocaleDirectionality {
#[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,
) -> Result<LocaleDirectionality, DataError>
expander: Expander,
) -> Result<Self, DataError>
where
P: DataProvider<ScriptDirectionV1Marker> + ?Sized,
{
Expand Down Expand Up @@ -177,7 +179,7 @@ impl LocaleDirectionality {
/// );
/// ```
pub fn get(&self, langid: &LanguageIdentifier) -> Option<Direction> {
let script = self.expander.get_likely_script(langid)?;
let script = self.expander.as_ref().get_likely_script(langid)?;

if self.script_in_ltr(script) {
Some(Direction::LeftToRight)
Expand All @@ -197,6 +199,7 @@ impl LocaleDirectionality {
/// See [`LocaleDirectionality::get`] for more information.
pub fn is_right_to_left(&self, langid: &LanguageIdentifier) -> bool {
self.expander
.as_ref()
.get_likely_script(langid)
.map(|s| self.script_in_rtl(s))
.unwrap_or(false)
Expand All @@ -211,6 +214,7 @@ impl LocaleDirectionality {
/// See [`LocaleDirectionality::get`] for more information.
pub fn is_left_to_right(&self, langid: &LanguageIdentifier) -> bool {
self.expander
.as_ref()
.get_likely_script(langid)
.map(|s| self.script_in_ltr(s))
.unwrap_or(false)
Expand Down
6 changes: 6 additions & 0 deletions components/locale/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,12 @@ impl LocaleExpander {
}
}

impl AsRef<LocaleExpander> for LocaleExpander {
fn as_ref(&self) -> &LocaleExpander {
self
}
}

#[cfg(feature = "serde")]
#[cfg(test)]
mod tests {
Expand Down