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
use std::fmt;
#[allow(unused)]
use std::ascii::AsciiExt;
use header::{Header, Raw, parsing};
#[derive(Clone, PartialEq, Debug)]
pub enum Pragma {
NoCache,
Ext(String),
}
impl Header for Pragma {
fn header_name() -> &'static str {
static NAME: &'static str = "Pragma";
NAME
}
fn parse_header(raw: &Raw) -> ::Result<Pragma> {
parsing::from_one_raw_str(raw).and_then(|s: String| {
let slice = &s.to_ascii_lowercase()[..];
match slice {
"no-cache" => Ok(Pragma::NoCache),
_ => Ok(Pragma::Ext(s)),
}
})
}
fn fmt_header(&self, f: &mut ::header::Formatter) -> fmt::Result {
f.fmt_line(self)
}
}
impl fmt::Display for Pragma {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Pragma::NoCache => "no-cache",
Pragma::Ext(ref string) => &string[..],
})
}
}
#[test]
fn test_parse_header() {
let a: Pragma = Header::parse_header(&"no-cache".into()).unwrap();
let b = Pragma::NoCache;
assert_eq!(a, b);
let c: Pragma = Header::parse_header(&"FoObar".into()).unwrap();
let d = Pragma::Ext("FoObar".to_owned());
assert_eq!(c, d);
let e: ::Result<Pragma> = Header::parse_header(&"".into());
assert_eq!(e.ok(), None);
}