1//! This function parses Accept-Language string into a list of language tags that
2//! can be later passed to language negotiation functions.
3//!
4//! # Example:
5//!
6//! ```
7//! use fluent_langneg::negotiate_languages;
8//! use fluent_langneg::NegotiationStrategy;
9//! use fluent_langneg::parse_accepted_languages;
10//! use fluent_langneg::convert_vec_str_to_langids_lossy;
11//! use unic_langid::LanguageIdentifier;
12//!
13//! let requested = parse_accepted_languages("de-AT;0.9,de-DE;0.8,de;0.7;en-US;0.5");
14//! let available = convert_vec_str_to_langids_lossy(&["fr", "pl", "de", "en-US"]);
15//! let default: LanguageIdentifier = "en-US".parse().expect("Failed to parse a langid.");
16//!
17//! let supported = negotiate_languages(
18//! &requested,
19//! &available,
20//! Some(&default),
21//! NegotiationStrategy::Filtering
22//! );
23//!
24//! let expected = convert_vec_str_to_langids_lossy(&["de", "en-US"]);
25//! assert_eq!(supported,
26//! expected.iter().map(|t| t.as_ref()).collect::<Vec<&LanguageIdentifier>>());
27//! ```
28//!
29//! This function ignores the weights associated with the locales, since Fluent Locale
30//! language negotiation only uses the order of locales, not the weights.
31//!
32
33use unic_langid::LanguageIdentifier;
34
35pub fn parse(s: &str) -> Vec<LanguageIdentifier> {
36 simpl Iterator.split(',')
37 .map(|t: &str| t.trim().split(';').nth(0).unwrap())
38 .filter(|t: &&str| !t.is_empty())
39 .filter_map(|t: &str| t.parse().ok())
40 .collect()
41}
42