Skip to content

Commit

Permalink
Making clippy happy and adding one more test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
snktd committed Aug 22, 2023
1 parent 2bc861b commit fed1622
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 9 additions & 9 deletions experimental/displaynames/src/displaynames.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,23 +406,24 @@ impl LocaleDisplayNamesFormatter {

/// Returns the display name of a locale.
/// This implementation is based on the algorithm described in
/// https://www.unicode.org/reports/tr35/tr35-general.html#locale_display_name_algorithm
/// `<https://www.unicode.org/reports/tr35/tr35-general.html#locale_display_name_algorithm>`
///
// TODO: Make this return a writeable instead of using alloc
pub fn of<'a, 'b: 'a, 'c: 'a>(&'b self, locale: &'c Locale) -> Cow<'a, str> {
let longest_matching_identifier = self.find_longest_matching_subtag(&locale);
let longest_matching_identifier = self.find_longest_matching_subtag(locale);

// Step - 1: Construct a locale display name string (LDN).
// Find the displayname for the longest_matching_subtag which was derived above.
let ldn = self.get_locale_display_name(&locale, &longest_matching_identifier);
let ldn = self.get_locale_display_name(locale, &longest_matching_identifier);

// Step - 2: Construct a vector of longest qualifying substrings (LQS).
// Find the displayname for the remaining locale if exists.
let lqs = self.get_longest_qualifying_substrings(&locale, &longest_matching_identifier);
let lqs = self.get_longest_qualifying_substrings(locale, &longest_matching_identifier);

// Step - 3: Return the displayname based on the size of LQS.
let mut result = Cow::Borrowed(ldn);
if lqs.len() > 0 {
#[allow(clippy::indexing_slicing)] // indexes in range
if !lqs.is_empty() {
let mut output = String::with_capacity(
result.len() + " (".len() + lqs.iter().map(|s| ", ".len() + s.len()).sum::<usize>()
- ", ".len()
Expand All @@ -435,7 +436,7 @@ impl LocaleDisplayNamesFormatter {
output.push_str(", ");
output.push_str(lqs);
}
output.push_str(")");
output.push(')');
result = Cow::Owned(output);
}
result
Expand Down Expand Up @@ -473,7 +474,7 @@ impl LocaleDisplayNamesFormatter {
}
}
}
return (locale.id.language, None, None).into();
(locale.id.language, None, None).into()
}

fn get_locale_display_name<'a>(
Expand Down Expand Up @@ -614,7 +615,6 @@ impl LocaleDisplayNamesFormatter {
.unwrap_or(variant_key.as_str()),
);
}

return lqs;
lqs
}
}
4 changes: 4 additions & 0 deletions experimental/displaynames/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ fn test_concatenate() {
input_1: &Locale::from_str("es-419-fonipa").unwrap(),
expected: "Latin American Spanish (IPA Phonetics)",
},
TestCase {
input_1: &Locale::from_str("es-Latn-419").unwrap(),
expected: "Spanish (Latin, Latin America)",
},
TestCase {
input_1: &locale!("xx-YY"),
expected: "xx (YY)",
Expand Down

0 comments on commit fed1622

Please sign in to comment.