diff --git a/Cargo.lock b/Cargo.lock index a7ac948b638..10110f599b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1956,6 +1956,7 @@ dependencies = [ "litemap", "log", "serde", + "writeable", "zerovec", ] diff --git a/experimental/transliterate/Cargo.toml b/experimental/transliterate/Cargo.toml index 5ede21f4692..e84b1d879e0 100644 --- a/experimental/transliterate/Cargo.toml +++ b/experimental/transliterate/Cargo.toml @@ -24,6 +24,7 @@ icu_provider = { workspace = true, features = ["macros", "experimental"] } icu_locid = { workspace = true } icu_collections = { workspace = true } icu_normalizer = { workspace = true } +writeable = { workspace = true } icu_properties = { workspace = true, optional = true } icu_unicodeset_parse = { workspace = true, optional = true } diff --git a/experimental/transliterate/src/ids.rs b/experimental/transliterate/src/ids.rs index 7462091e8b0..830f7dcf065 100644 --- a/experimental/transliterate/src/ids.rs +++ b/experimental/transliterate/src/ids.rs @@ -2,19 +2,34 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use alloc::string::ToString; use icu_locid::Locale; use icu_provider::prelude::*; +use writeable::Writeable; pub fn bcp47_to_data_locale(locale: &Locale) -> DataLocale { let mut data_locale = DataLocale::default(); #[allow(clippy::unwrap_used)] // any BCP-47 locale is a valid aux key - data_locale.set_aux(locale.to_string().parse().unwrap()); + data_locale.set_aux( + AuxiliaryKeys::try_from_iter( + locale + .write_to_string() + .split('-') + .map(str::parse) + .filter_map(Result::ok), + ) + .unwrap(), + ); data_locale } pub fn unparsed_bcp47_to_data_locale(dep: &str) -> Result { let mut data_locale = DataLocale::default(); - data_locale.set_aux(dep.parse()?); + data_locale.set_aux(AuxiliaryKeys::try_from_iter( + dep.split('-') + .map(str::parse) + // TODO: Bubble a potential subtag parse error out. Currently + // we ignore it inside of the `filter_map`. + .filter_map(Result::ok), + )?); Ok(data_locale) } diff --git a/provider/core/src/request.rs b/provider/core/src/request.rs index caf1da97171..8889f48a499 100644 --- a/provider/core/src/request.rs +++ b/provider/core/src/request.rs @@ -959,7 +959,15 @@ impl AuxiliaryKeys { pub fn iter(&self) -> impl Iterator + '_ { self.value .split(Self::separator()) - .flat_map(|x| x.parse().ok()) + .filter_map(|x| { + match x.parse() { + Ok(x) => Some(x), + Err(_) => { + debug_assert!(false, "failed to convert to subtag: {x}"); + None + } + } + }) } pub(crate) fn strict_cmp_iter<'l, I>(&self, mut subtags: I) -> SubtagOrderingResult