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
use std::fmt;
use std::str::FromStr;
use hyper::header::HeaderValue;
use tc_error::*;
mod client;
mod server;
pub use client::*;
pub use server::*;
trait Accept: Default + FromStr {
fn parse_header(header: Option<&HeaderValue>) -> TCResult<Self> {
let header = if let Some(header) = header {
header
.to_str()
.map_err(|e| TCError::bad_request("invalid Accept-Encoding header", e))?
} else {
return Ok(Self::default());
};
let accept = header.split(',');
let mut quality = 0.;
let mut encoding = None;
for opt in accept {
if opt.contains(';') {
let opt: Vec<&str> = opt.split(';').collect();
if opt.len() != 2 {
return Err(TCError::bad_request(
"invalid encoding specified in Accept-Encoding header",
opt.join(";"),
));
}
let format = opt[0].parse();
let q = opt[1].parse().map_err(|e| {
TCError::bad_request("invalid quality value in Accept-Encoding header", e)
})?;
if q > quality {
if let Ok(format) = format {
encoding = Some(format);
quality = q;
}
}
} else {
if let Ok(format) = opt.parse() {
if encoding.is_none() {
encoding = Some(format);
quality = 1.;
}
}
}
}
Ok(encoding.unwrap_or_default())
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
enum Encoding {
Json,
Tbon,
}
impl Default for Encoding {
fn default() -> Self {
Self::Json
}
}
impl FromStr for Encoding {
type Err = TCError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.trim() {
"application/json" => Ok(Self::Json),
"application/tbon" => Ok(Self::Tbon),
_ => Err(TCError::bad_request("encoding not supported", s)),
}
}
}
impl Accept for Encoding {}
impl fmt::Display for Encoding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
Self::Json => "application/json",
Self::Tbon => "application/tbon",
})
}
}