1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
pub mod errors;
pub use self::errors::ParserError;
use crate::subtags;
use crate::LanguageIdentifier;
pub fn parse_language_identifier(t: &str) -> Result<LanguageIdentifier, ParserError> {
let mut position = 0;
let mut language = None;
let mut script = None;
let mut region = None;
let mut variants = vec![];
for subtag in t.split(|c| ['-', '_'].contains(&c)) {
if position == 0 {
language = subtags::parse_language_subtag(subtag)?;
position = 1;
continue;
}
if position == 1 {
position = 2;
if let Ok(s) = subtags::parse_script_subtag(subtag) {
script = Some(s);
continue;
}
}
if position == 2 {
position = 3;
if let Ok(s) = subtags::parse_region_subtag(subtag) {
region = Some(s);
continue;
}
}
if position == 3 {
variants.push(subtags::parse_variant_subtag(subtag)?);
}
}
variants.sort();
Ok(LanguageIdentifier {
language,
script,
region,
variants,
})
}