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
use std::str::FromStr;
use std::fmt;
#[derive(Clone, PartialEq, Debug)]
pub struct Language {
pub primary: String,
pub sub: Option<String>
}
impl FromStr for Language {
type Err = ::Error;
fn from_str(s: &str) -> ::Result<Language> {
let mut i = s.split("-");
let p = i.next();
let s = i.next();
match (p, s) {
(Some(p), Some(s)) => Ok(Language {
primary: p.to_owned(),
sub: Some(s.to_owned())
}),
(Some(p), _) => Ok(Language {
primary: p.to_owned(),
sub: None
}),
_ => Err(::Error::Header)
}
}
}
impl fmt::Display for Language {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(f.write_str(&self.primary[..]));
match self.sub {
Some(ref s) => write!(f, "-{}", s),
None => Ok(())
}
}
}