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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::fmt;
use mime::{self, Mime};
#[derive(Clone, Debug, PartialEq)]
pub struct ContentType(Mime);
impl ContentType {
#[inline]
pub fn json() -> ContentType {
ContentType(mime::APPLICATION_JSON)
}
#[inline]
pub fn text() -> ContentType {
ContentType(mime::TEXT_PLAIN)
}
#[inline]
pub fn text_utf8() -> ContentType {
ContentType(mime::TEXT_PLAIN_UTF_8)
}
#[inline]
pub fn html() -> ContentType {
ContentType(mime::TEXT_HTML)
}
#[inline]
pub fn xml() -> ContentType {
ContentType(mime::TEXT_XML)
}
#[inline]
pub fn form_url_encoded() -> ContentType {
ContentType(mime::APPLICATION_WWW_FORM_URLENCODED)
}
#[inline]
pub fn jpeg() -> ContentType {
ContentType(mime::IMAGE_JPEG)
}
#[inline]
pub fn png() -> ContentType {
ContentType(mime::IMAGE_PNG)
}
#[inline]
pub fn octet_stream() -> ContentType {
ContentType(mime::APPLICATION_OCTET_STREAM)
}
}
impl ::Header for ContentType {
const NAME: &'static ::HeaderName = &::http::header::CONTENT_TYPE;
fn decode(values: &mut ::Values) -> Option<Self> {
values
.next()?
.to_str()
.ok()?
.parse()
.ok()
.map(ContentType)
}
fn encode(&self, values: &mut ::ToValues) {
let value = self
.0
.as_ref()
.parse()
.expect("Mime is always a valid HeaderValue");
values.append(value);
}
}
impl From<mime::Mime> for ContentType {
fn from(m: mime::Mime) -> ContentType {
ContentType(m)
}
}
impl From<ContentType> for mime::Mime {
fn from(ct: ContentType) -> mime::Mime {
ct.0
}
}
impl fmt::Display for ContentType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[cfg(test)]
mod tests {
use super::ContentType;
use super::super::test_decode;
#[test]
fn json() {
assert_eq!(
test_decode::<ContentType>(&["application/json"]),
Some(ContentType::json()),
);
}
}