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
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::fmt::{Formatter, Display, Result};
use hyper::error::{self, Error};
use hyper::header::{HeaderFormat, Header};
use FieldMap;
const ST_HEADER_NAME: &'static str = "ST";
const ST_ALL_VALUE: &'static str = "ssdp:all";
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum ST {
All,
Target(FieldMap),
}
impl Header for ST {
fn header_name() -> &'static str {
ST_HEADER_NAME
}
fn parse_header(raw: &[Vec<u8>]) -> error::Result<Self> {
if raw.len() != 1 {
return Err(Error::Header);
}
if &raw[0][..] == ST_ALL_VALUE.as_bytes() {
Ok(ST::All)
} else {
FieldMap::parse_bytes(&raw[0][..]).map(ST::Target).ok_or(Error::Header)
}
}
}
impl HeaderFormat for ST {
fn fmt_header(&self, fmt: &mut Formatter) -> Result {
match *self {
ST::All => try!(fmt.write_str(ST_ALL_VALUE)),
ST::Target(ref n) => try!(Display::fmt(n, fmt)),
};
Ok(())
}
}
#[cfg(test)]
mod tests {
use hyper::header::Header;
use FieldMap;
use super::ST;
#[test]
fn positive_all() {
let st_all_header = &[b"ssdp:all"[..].to_vec()];
match ST::parse_header(st_all_header) {
Ok(ST::All) => (),
_ => panic!("Failed To Match ST::All Header"),
}
}
#[test]
fn positive_field_upnp() {
let st_upnp_root_header = &[b"upnp:some_identifier"[..].to_vec()];
match ST::parse_header(st_upnp_root_header) {
Ok(ST::Target(FieldMap::UPnP(_))) => (),
_ => panic!("Failed To Match ST::Target Header To FieldMap::UPnP"),
}
}
#[test]
fn positive_field_urn() {
let st_urn_root_header = &[b"urn:some_identifier"[..].to_vec()];
match ST::parse_header(st_urn_root_header) {
Ok(ST::Target(FieldMap::URN(_))) => (),
_ => panic!("Failed To Match ST::Target Header To FieldMap::URN"),
}
}
#[test]
fn positive_field_uuid() {
let st_uuid_root_header = &[b"uuid:some_identifier"[..].to_vec()];
match ST::parse_header(st_uuid_root_header) {
Ok(ST::Target(FieldMap::UUID(_))) => (),
_ => panic!("Failed To Match ST::Target Header To FieldMap::UUID"),
}
}
#[test]
#[should_panic]
fn negative_multiple_headers() {
let st_multiple_headers = &[b"uuid:some_identifier"[..].to_vec(), b"ssdp:all"[..].to_vec()];
ST::parse_header(st_multiple_headers).unwrap();
}
}