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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pub mod errors;
use std::iter::Peekable;
pub use self::errors::ParserError;
use crate::subtags;
use crate::LanguageIdentifier;
static SEPARATORS: &[char] = &['-', '_'];
pub fn parse_language_identifier_from_iter<'a>(
iter: &mut Peekable<impl Iterator<Item = &'a str>>,
allow_extension: bool,
) -> Result<LanguageIdentifier, ParserError> {
let mut position = 0;
let mut language = None;
let mut script = None;
let mut region = None;
let mut variants = vec![];
while let Some(subtag) = iter.next() {
if position == 0 {
language = subtags::parse_language_subtag(subtag)?;
position = 1;
} else if position == 1 {
if let Ok(s) = subtags::parse_script_subtag(subtag) {
script = Some(s);
position = 2;
} else if let Ok(s) = subtags::parse_region_subtag(subtag) {
region = Some(s);
position = 3;
} else {
variants.push(subtags::parse_variant_subtag(subtag)?);
position = 3;
}
} else if position == 2 {
if let Ok(s) = subtags::parse_region_subtag(subtag) {
region = Some(s);
position = 3;
} else {
variants.push(subtags::parse_variant_subtag(subtag)?);
position = 3;
}
} else {
variants.push(subtags::parse_variant_subtag(subtag)?);
}
if allow_extension {
if let Some(st_peek) = iter.peek() {
if st_peek.len() == 1 {
break;
}
}
}
}
let variants = if variants.is_empty() {
None
} else {
variants.sort();
variants.dedup();
Some(variants.into_boxed_slice())
};
Ok(LanguageIdentifier {
language,
script,
region,
variants,
})
}
pub fn parse_language_identifier(t: &str) -> Result<LanguageIdentifier, ParserError> {
let mut iter = t.split(|c| SEPARATORS.contains(&c)).peekable();
parse_language_identifier_from_iter(&mut iter, false)
}