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
use crate::Error;
use bytes::Bytes;
use http::{
header::HeaderName as HttpHeaderName, HeaderMap as HttpHeaderMap,
HeaderValue as HttpHeaderValue,
};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::str::FromStr;
#[derive(Eq, PartialEq, Hash, Debug, Clone)]
pub struct HeaderName {
inner: String,
}
#[derive(Eq, PartialEq, Hash, Debug, Clone)]
pub struct HeaderValue {
inner: Bytes,
}
#[derive(Eq, PartialEq, Debug, Clone, Default)]
pub struct HeaderMap {
map: HashMap<HeaderName, HeaderValue>,
}
pub struct IntoIter {
inner: std::collections::hash_map::IntoIter<HeaderName, HeaderValue>,
}
impl ToString for HeaderName {
fn to_string(&self) -> std::string::String {
self.inner.clone()
}
}
impl From<String> for HeaderName {
fn from(string: String) -> Self {
HeaderName { inner: string }
}
}
impl TryFrom<HeaderName> for HttpHeaderName {
type Error = Error;
fn try_from(
header: HeaderName,
) -> std::result::Result<Self, <Self as std::convert::TryFrom<HeaderName>>::Error> {
Ok(HttpHeaderName::from_str(&header.to_string())?)
}
}
impl From<HttpHeaderName> for HeaderName {
fn from(header: HttpHeaderName) -> Self {
HeaderName::from(header.to_string())
}
}
impl From<String> for HeaderValue {
fn from(string: String) -> Self {
HeaderValue {
inner: Bytes::from(string),
}
}
}
impl TryFrom<HeaderValue> for HttpHeaderValue {
type Error = Error;
fn try_from(
value: HeaderValue,
) -> std::result::Result<Self, <Self as std::convert::TryFrom<HeaderValue>>::Error> {
Ok(HttpHeaderValue::from_bytes(&value.inner[..])?)
}
}
impl From<HttpHeaderValue> for HeaderValue {
fn from(value: HttpHeaderValue) -> Self {
HeaderValue {
inner: Bytes::copy_from_slice(value.as_bytes()),
}
}
}
impl From<&str> for HeaderValue {
fn from(string: &str) -> Self {
Self::from(string.to_owned())
}
}
impl TryFrom<HeaderMap> for HttpHeaderMap {
type Error = Error;
fn try_from(
headers: HeaderMap,
) -> std::result::Result<Self, <Self as std::convert::TryFrom<HeaderMap>>::Error> {
let mut result = HttpHeaderMap::new();
for (key, value) in headers {
result.append(
HttpHeaderName::try_from(key)?,
HttpHeaderValue::try_from(value)?,
);
}
Ok(result)
}
}
impl IntoIterator for HeaderMap {
type Item = (HeaderName, HeaderValue);
type IntoIter = IntoIter;
fn into_iter(self) -> <Self as std::iter::IntoIterator>::IntoIter {
IntoIter {
inner: self.map.into_iter(),
}
}
}
impl HeaderMap {
pub fn new() -> Self {
HeaderMap {
map: HashMap::new(),
}
}
pub fn insert<T: Into<HeaderName>, U: Into<HeaderValue>>(
&mut self,
key: T,
value: U,
) -> Option<HeaderValue> {
self.map.insert(key.into(), value.into())
}
}
impl Iterator for IntoIter {
type Item = (HeaderName, HeaderValue);
fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {
self.inner.next()
}
}