From b2ffd403b48c7e6456afd350daa7fec7c73b2685 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Fri, 30 Aug 2024 13:39:21 -0700 Subject: [PATCH] Fix transliterator fallback loading --- .../src/transliterate/transliterator/mod.rs | 82 ++++++++++--------- 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/components/experimental/src/transliterate/transliterator/mod.rs b/components/experimental/src/transliterate/transliterator/mod.rs index 2508691d5db..7c496be7a98 100644 --- a/components/experimental/src/transliterate/transliterator/mod.rs +++ b/components/experimental/src/transliterate/transliterator/mod.rs @@ -306,21 +306,31 @@ impl Transliterator { // 1. Insert a placeholder to avoid infinite recursion. env.insert(dep.to_string(), InternalTransliterator::Null); // 2. Load the transliterator, by checking - let internal_t = dep - // a) hardcoded specials - .strip_prefix("x-") - .map(|special| Transliterator::load_special(special, normalizer_provider)) - // b) the user-provided override - .or_else(|| Transliterator::load_with_override(&dep, lookup?).transpose()) - // c) the data - .unwrap_or_else(|| { + let mut internal_t = None; + // a) hardcoded specials + if let Some(special) = dep.strip_prefix("x-") { + internal_t = Transliterator::load_special(special, normalizer_provider)?; + } + // b) the user-provided override + if let Some(lookup) = lookup { + if internal_t.is_none() { + if let Ok(locale) = dep.parse() { + internal_t = Transliterator::load_with_override(&locale, lookup)?; + } + } + } + // c) the data + let internal_t = match internal_t { + Some(t) => t, + None => { let rbt = Transliterator::load_rbt( #[allow(clippy::unwrap_used)] // infallible DataMarkerAttributes::try_from_str(&dep.to_ascii_lowercase()).unwrap(), transliterator_provider, )?; - Ok(InternalTransliterator::RuleBased(rbt)) - })?; + InternalTransliterator::RuleBased(rbt) + } + }; if let InternalTransliterator::RuleBased(rbt) = &internal_t { // 3. Recursively load the dependencies of the dependency. Self::load_dependencies_recursive( @@ -341,7 +351,7 @@ impl Transliterator { fn load_special

( special: &str, normalizer_provider: &P, - ) -> Result + ) -> Result, DataError> where P: DataProvider + DataProvider @@ -352,51 +362,47 @@ impl Transliterator { { // TODO(#3909, #3910): add more match special { - "any-nfc" => Ok(InternalTransliterator::Composing( + "any-nfc" => Ok(Some(InternalTransliterator::Composing( ComposingTransliterator::try_nfc(normalizer_provider)?, - )), - "any-nfkc" => Ok(InternalTransliterator::Composing( + ))), + "any-nfkc" => Ok(Some(InternalTransliterator::Composing( ComposingTransliterator::try_nfkc(normalizer_provider)?, - )), - "any-nfd" => Ok(InternalTransliterator::Decomposing( + ))), + "any-nfd" => Ok(Some(InternalTransliterator::Decomposing( DecomposingTransliterator::try_nfd(normalizer_provider)?, - )), - "any-nfkd" => Ok(InternalTransliterator::Decomposing( + ))), + "any-nfkd" => Ok(Some(InternalTransliterator::Decomposing( DecomposingTransliterator::try_nfkd(normalizer_provider)?, - )), - "any-null" => Ok(InternalTransliterator::Null), - "any-remove" => Ok(InternalTransliterator::Remove), - "any-hex/unicode" => Ok(InternalTransliterator::Hex( + ))), + "any-null" => Ok(Some(InternalTransliterator::Null)), + "any-remove" => Ok(Some(InternalTransliterator::Remove)), + "any-hex/unicode" => Ok(Some(InternalTransliterator::Hex( hardcoded::HexTransliterator::new("U+", "", 4, Case::Upper), - )), - "any-hex/rust" => Ok(InternalTransliterator::Hex( + ))), + "any-hex/rust" => Ok(Some(InternalTransliterator::Hex( hardcoded::HexTransliterator::new("\\u{", "}", 2, Case::Lower), - )), - "any-hex/xml" => Ok(InternalTransliterator::Hex( + ))), + "any-hex/xml" => Ok(Some(InternalTransliterator::Hex( hardcoded::HexTransliterator::new("&#x", ";", 1, Case::Upper), - )), - "any-hex/perl" => Ok(InternalTransliterator::Hex( + ))), + "any-hex/perl" => Ok(Some(InternalTransliterator::Hex( hardcoded::HexTransliterator::new("\\x{", "}", 1, Case::Upper), - )), - "any-hex/plain" => Ok(InternalTransliterator::Hex( + ))), + "any-hex/plain" => Ok(Some(InternalTransliterator::Hex( hardcoded::HexTransliterator::new("", "", 4, Case::Upper), - )), - s => Err(DataError::custom("unavailable transliterator").with_debug_context(s)), + ))), + _ => Ok(None), } } fn load_with_override( - id: &str, + locale: &Locale, lookup: &F, ) -> Result, DataError> where F: Fn(&Locale) -> Option>, { - let locale: Locale = id.parse().map_err(|e| { - DataError::custom("invalid data: transliterator dependency is not a valid Locale") - .with_debug_context(&e) - })?; - Ok(lookup(&locale).map(InternalTransliterator::Dyn)) + Ok(lookup(locale).map(InternalTransliterator::Dyn)) } fn load_rbt

(