From bba4ee465e32fca5f80e8cbb4c58894698eaaed4 Mon Sep 17 00:00:00 2001 From: SkyGrel19 Date: Sat, 28 Sep 2024 13:49:13 +0300 Subject: [PATCH] Update bcp47 function to work by ref Use `IntoIterator` remove vec! from examples --- src/bcp47.rs | 42 +++++++++++++++++++++--------------------- src/posix.rs | 28 ++++++++++++++-------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/bcp47.rs b/src/bcp47.rs index 61b7528..72b354e 100644 --- a/src/bcp47.rs +++ b/src/bcp47.rs @@ -53,7 +53,7 @@ use language_tags::LanguageTag; /// If no match is found, [`None`] is returned. /// /// The returned locale is guaranteed to EXACTLY match one of the available locales. -/// For example, `best_matching_locale(&["EN"].iter(), &["en"].iter())` will return `Some("EN")`. +/// For example, `best_matching_locale(&["EN"], &["en"])` will return `Some("EN")`. /// /// # Examples /// @@ -61,42 +61,42 @@ use language_tags::LanguageTag; /// use locale_match::bcp47::best_matching_locale; /// /// -/// let available_locales = vec!["en-US", "en-GB", "ru-UA", "fr-FR", "it"]; -/// let user_locales = vec!["ru-RU", "ru", "en-US", "en"]; +/// let available_locales = ["en-US", "en-GB", "ru-UA", "fr-FR", "it"]; +/// let user_locales = ["ru-RU", "ru", "en-US", "en"]; /// -/// let best_match = best_matching_locale(available_locales.iter(), user_locales.iter()); +/// let best_match = best_matching_locale(&available_locales, &user_locales); /// /// // "ru-UA" is the best match for the highest-priority user locale "ru-RU" -/// assert_eq!(best_match, Some("ru-UA".to_string())); +/// assert_eq!(best_match, Some("ru-UA")); /// /// -/// let available_locales = vec!["en", "pt-BR", "pt-PT", "es"]; -/// let user_locales = vec!["pt", "en"]; +/// let available_locales = ["en", "pt-BR", "pt-PT", "es"]; +/// let user_locales = ["pt", "en"]; /// -/// let best_match = best_matching_locale(available_locales.iter(), user_locales.iter()); +/// let best_match = best_matching_locale(&available_locales, &user_locales); /// /// // "pt-BR" is the first best match for the highest-priority user locale "pt" -/// assert_eq!(best_match, Some("pt-BR".to_string())); +/// assert_eq!(best_match, Some("pt-BR")); /// /// -/// let available_locales = vec!["zh", "zh-cmn", "zh-cmn-Hans"]; -/// let user_locales = vec!["zh-Hans"]; +/// let available_locales = ["zh", "zh-cmn", "zh-cmn-Hans"]; +/// let user_locales = ["zh-Hans"]; /// -/// let best_match = best_matching_locale(available_locales.iter(), user_locales.iter()); +/// let best_match = best_matching_locale(&available_locales, &user_locales); /// /// // Empty extended language subtag in "zh-Hans" matches any extended language, e.g. "cmn" -/// assert_eq!(best_match, Some("zh-cmn-Hans".to_string())); +/// assert_eq!(best_match, Some("zh-cmn-Hans")); /// ``` -pub fn best_matching_locale(available_locales: impl Iterator, user_locales: impl Iterator) -> Option +pub fn best_matching_locale<'a, 'b, T1, T2>(available_locales: impl IntoIterator, user_locales: impl IntoIterator) -> Option<&'a str> where - T1: AsRef, - T2: AsRef + T1: AsRef + 'a, + T2: AsRef + 'b { - let available_tags = available_locales + let available_tags = available_locales.into_iter() .filter_map(|l| LanguageTag::parse(l.as_ref()).ok().map(|tag| (l, tag))) - .collect::>(); + .collect::>(); - user_locales + user_locales.into_iter() .filter_map(|locale| LanguageTag::parse(locale.as_ref()).ok()) .find_map(|user_tag| available_tags.iter() @@ -121,7 +121,7 @@ where score }) ) - .map(|(aval_locale, _)| aval_locale.as_ref().to_string()) + .map(|&(aval_locale, _)| aval_locale.as_ref()) } #[cfg(test)] @@ -132,7 +132,7 @@ mod tests { fn test_best_matching_locale() { fn assert_best_match(available_locales: &[&str], user_locales: &[&str], expected: Option<&str>) { - assert_eq!(best_matching_locale(available_locales.iter(), user_locales.iter()).as_deref(), expected); + assert_eq!(best_matching_locale(available_locales, user_locales), expected); } // One best match diff --git a/src/posix.rs b/src/posix.rs index 9604467..af07712 100644 --- a/src/posix.rs +++ b/src/posix.rs @@ -52,7 +52,7 @@ /// If no match is found, [`None`] is returned. /// /// The returned locale is guaranteed to EXACTLY match one of the available locales. -/// For example, `best_matching_locale(&["EN"].iter(), &["en"].iter())` will return `Some("EN")`. +/// For example, `best_matching_locale(&["EN"], &["en"])` will return `Some("EN")`. /// /// # Examples /// @@ -60,42 +60,42 @@ /// use locale_match::posix::best_matching_locale; /// /// -/// let available_locales = vec!["en_US", "en_GB", "ru_UA", "fr_FR", "it"]; -/// let user_locales = vec!["ru_RU", "ru", "en_US", "en"]; +/// let available_locales = ["en_US", "en_GB", "ru_UA", "fr_FR", "it"]; +/// let user_locales = ["ru_RU", "ru", "en_US", "en"]; /// -/// let best_match = best_matching_locale(available_locales.iter(), user_locales.iter()); +/// let best_match = best_matching_locale(&available_locales, &user_locales); /// /// // "ru_UA" is the best match for the highest-priority user locale "ru_RU" /// assert_eq!(best_match, Some("ru_UA")); /// /// -/// let available_locales = vec!["en", "pt_BR", "pt_PT", "es"]; -/// let user_locales = vec!["pt", "en"]; +/// let available_locales = ["en", "pt_BR", "pt_PT", "es"]; +/// let user_locales = ["pt", "en"]; /// -/// let best_match = best_matching_locale(available_locales.iter(), user_locales.iter()); +/// let best_match = best_matching_locale(&available_locales, &user_locales); /// /// // "pt_BR" is the first best match for the highest-priority user locale "pt" /// assert_eq!(best_match, Some("pt_BR")); /// /// -/// let available_locales = vec!["fr", "fr_FR", "fr_CA.UTF-8"]; -/// let user_locales = vec!["fr.UTF-8"]; +/// let available_locales = ["fr", "fr_FR", "fr_CA.UTF-8"]; +/// let user_locales = ["fr.UTF-8"]; /// -/// let best_match = best_matching_locale(available_locales.iter(), user_locales.iter()); +/// let best_match = best_matching_locale(&available_locales, &user_locales); /// /// // Empty territory in "fr.UTF-8" matches any territory, e.g. "CA" /// assert_eq!(best_match, Some("fr_CA.UTF-8")); /// ``` -pub fn best_matching_locale<'a, 'b, T1, T2>(available_locales: impl Iterator, user_locales: impl Iterator) -> Option<&'a str> +pub fn best_matching_locale<'a, 'b, T1, T2>(available_locales: impl IntoIterator, user_locales: impl IntoIterator) -> Option<&'a str> where T1: AsRef + 'a, T2: AsRef + 'b { - let available_parsed_locales = available_locales + let available_parsed_locales = available_locales.into_iter() .map(|l| PosixLocale::parse(l.as_ref())) .collect::>(); - user_locales + user_locales.into_iter() .map(|locale| PosixLocale::parse(locale.as_ref())) .find_map(|user_locale| available_parsed_locales.iter() @@ -160,7 +160,7 @@ mod tests { fn test_best_matching_locale() { fn assert_best_match(available_locales: &[&str], user_locales: &[&str], expected: Option<&str>) { - assert_eq!(best_matching_locale(available_locales.iter(), user_locales.iter()).as_deref(), expected); + assert_eq!(best_matching_locale(available_locales, user_locales), expected); } // One best match