Skip to content

Commit

Permalink
Fix call sites
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Mar 16, 2022
1 parent 18e2f24 commit b0f1608
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
10 changes: 5 additions & 5 deletions components/locale_canonicalizer/src/locale_canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn uts35_check_language_rules(
locale: &mut Locale,
alias_data: &DataPayload<AliasesV1Marker>,
) -> CanonicalizationResult {
let maybe_lang: Option<TinyAsciiStr<3>> = locale.id.language.into();
let maybe_lang: Option<TinyAsciiStr<3>> = locale.id.language.to_option().map(Into::into);
if let Some(lang) = maybe_lang {
let replacement = if lang.len() == 2 {
alias_data
Expand Down Expand Up @@ -557,14 +557,14 @@ impl LocaleCanonicalizer {
return CanonicalizationResult::Unmodified;
}

if let Some(language) = langid.language.into() {
if let Some(language) = langid.language.to_option() {
if let Some(region) = langid.region {
maximize_locale!(langid, data.language_region, language, region.into());
maximize_locale!(langid, data.language_region, language.into(), region.into());
}
if let Some(script) = langid.script {
maximize_locale!(langid, data.language_script, language, script.into());
maximize_locale!(langid, data.language_script, language.into(), script.into());
}
maximize_locale!(langid, data.language, language);
maximize_locale!(langid, data.language, language.into());
}
if let Some(script) = langid.script {
if let Some(region) = langid.region {
Expand Down
33 changes: 30 additions & 3 deletions components/locid/src/subtags/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ impl Language {
///
/// `Notice`: For many use cases, such as comparison,
/// [`Language`] implements [`PartialEq`]`<&`[`str`]`>` which allows for direct comparisons.
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}

/// Resets the [`Language`] subtag to an empty one.
/// Resets the [`Language`] subtag to an empty one (equal to `"und"`).
///
/// # Examples
///
Expand All @@ -172,11 +173,12 @@ impl Language {
///
/// assert_eq!(lang.as_str(), "und");
/// ```
#[inline]
pub fn clear(&mut self) {
*self = UND
}

/// Tests if the [`Language`] subtag is empty.
/// Tests if the [`Language`] subtag is empty (equal to `"und"`).
///
/// # Examples
///
Expand All @@ -192,9 +194,34 @@ impl Language {
///
/// assert_eq!(lang.is_empty(), true);
/// ```
#[inline]
pub fn is_empty(self) -> bool {
self == UND
}

/// Returns an `Option<Language>`, which is `None` if the subtag is `"und"`,
/// and `Some(self)` otherwise.
///
/// # Examples
///
/// ```
/// use icu::locid::subtags::Language;
///
/// assert!(matches!(
/// Language::und().to_option(),
/// None));
/// assert!(matches!(
/// Language::from_bytes(b"uk").unwrap().to_option(),
/// Some(_)));
/// ```
#[inline]
pub fn to_option(self) -> Option<Language> {
if !self.is_empty() {
Some(self)
} else {
None
}
}
}

impl FromStr for Language {
Expand Down Expand Up @@ -243,7 +270,7 @@ impl<'l> From<&'l Language> for &'l str {

impl From<Language> for TinyAsciiStr<3> {
fn from(input: Language) -> Self {
input.0.into()
input.0
}
}

Expand Down
12 changes: 6 additions & 6 deletions provider/cldr/src/transform/locale_canonicalizer/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,21 @@ impl From<&cldr_serde::aliases::Resource> for AliasesV1 {
continue;
}

let maybe_lang: Option<TinyAsciiStr<3>> = langid.language.into();
if let Some(lang) = maybe_lang {
if let Some(lang) = langid.language.to_option() {
let lang_str: TinyAsciiStr<3> = lang.into();
if langid.region.is_none() && langid.variants.is_empty() {
// Relatively few aliases exist for two character language identifiers,
// so we store them separately to not slow down canonicalization of
// common identifiers.
if lang.len() == 2 {
language_len2.push((lang.resize(), replacement));
if lang_str.len() == 2 {
language_len2.push((lang_str.resize(), replacement));
} else {
language_len3.push((lang, replacement));
language_len3.push((lang_str, replacement));
}
} else if let Some(region) = langid.region {
// All current language-region aliases are for "sgn", so we store them
// separately to not slow down canonicalization of common identifiers.
if lang == "sgn" {
if lang_str == "sgn" {
sgn_region.push((region.into(), replacement));
} else {
language.push((langid, replacement));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl From<&cldr_serde::likely_subtags::Resource> for LikelySubtagsV1 {
};

for entry in other.supplemental.likely_subtags.iter() {
if let Some(lang) = entry.0.language.into() {
if let Some(lang) = entry.0.language.to_option().map(Into::into) {
if let Some(script) = entry.0.script {
language_script.insert((lang, script.into()), extract_result(entry));
} else if let Some(region) = entry.0.region {
Expand Down

0 comments on commit b0f1608

Please sign in to comment.