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
80
81
82
83
84
85
86
87
88
89
use header::QualityItem;
use std::str::FromStr;
use std::fmt;
#[derive(Clone, PartialEq, Debug)]
pub struct Language{
primary: String,
sub: Option<String>
}
impl FromStr for Language {
type Err = ();
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_string(),
sub: Some(s.to_string())}),
(Some(p),_) => Ok(Language{primary: p.to_string(), sub: None}),
_ => Err(())
}
}
}
impl fmt::Display for Language {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{}", self.primary));
match self.sub {
Some(ref s) => write!(f, "-{}", s),
None => Ok(())
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct AcceptLanguage(pub Vec<QualityItem<Language>>);
impl_list_header!(AcceptLanguage,
"Accept-Language",
Vec<QualityItem<Language>>);
#[cfg(test)]
mod tests {
use header::{Header, qitem, Quality, QualityItem};
use super::*;
#[test]
fn test_parse_header() {
let a: AcceptLanguage = Header::parse_header(
[b"en-us;q=1.0, en;q=0.5, fr".to_vec()].as_slice()).unwrap();
let b = AcceptLanguage(vec![
qitem(Language{primary: "en".to_string(), sub: Some("us".to_string())}),
QualityItem::new(Language{primary: "en".to_string(), sub: None},
Quality(500)),
qitem(Language{primary: "fr".to_string(), sub: None}),
]);
assert_eq!(format!("{}", a), format!("{}", b));
assert_eq!(a, b);
}
#[test]
fn test_display() {
assert_eq!("en".to_string(),
format!("{}", Language{primary: "en".to_string(),
sub: None}));
assert_eq!("en-us".to_string(),
format!("{}", Language{primary: "en".to_string(),
sub: Some("us".to_string())}));
}
#[test]
fn test_from_str() {
assert_eq!(Language { primary: "en".to_string(), sub: None },
"en".parse().unwrap());
assert_eq!(Language { primary: "en".to_string(),
sub: Some("us".to_string()) },
"en-us".parse().unwrap());
}
}
bench_header!(bench, AcceptLanguage,
{ vec![b"en-us;q=1.0, en;q=0.5, fr".to_vec()] });