1use crate::Error;
2use bytes::Bytes;
3use http::{
4 header::HeaderName as HttpHeaderName, HeaderMap as HttpHeaderMap,
5 HeaderValue as HttpHeaderValue,
6};
7use std::collections::HashMap;
8use std::convert::TryFrom;
9use std::str::FromStr;
10
11#[derive(Eq, PartialEq, Hash, Debug, Clone)]
12pub struct HeaderName {
13 inner: String,
14}
15
16#[derive(Eq, PartialEq, Hash, Debug, Clone)]
17pub struct HeaderValue {
18 inner: Bytes,
19}
20
21#[derive(Eq, PartialEq, Debug, Clone, Default)]
22pub struct HeaderMap {
23 map: HashMap<HeaderName, HeaderValue>,
24}
25
26pub struct IntoIter {
27 inner: std::collections::hash_map::IntoIter<HeaderName, HeaderValue>,
28}
29
30impl ToString for HeaderName {
31 fn to_string(&self) -> std::string::String {
32 self.inner.clone()
33 }
34}
35
36impl From<String> for HeaderName {
37 fn from(string: String) -> Self {
38 HeaderName { inner: string }
39 }
40}
41
42impl TryFrom<HeaderName> for HttpHeaderName {
43 type Error = Error;
44 fn try_from(
45 header: HeaderName,
46 ) -> std::result::Result<Self, <Self as std::convert::TryFrom<HeaderName>>::Error> {
47 Ok(HttpHeaderName::from_str(&header.to_string())?)
48 }
49}
50
51impl From<HttpHeaderName> for HeaderName {
52 fn from(header: HttpHeaderName) -> Self {
53 HeaderName::from(header.to_string())
54 }
55}
56
57impl From<String> for HeaderValue {
58 fn from(string: String) -> Self {
59 HeaderValue {
60 inner: Bytes::from(string),
61 }
62 }
63}
64
65impl TryFrom<HeaderValue> for HttpHeaderValue {
66 type Error = Error;
67 fn try_from(
68 value: HeaderValue,
69 ) -> std::result::Result<Self, <Self as std::convert::TryFrom<HeaderValue>>::Error> {
70 Ok(HttpHeaderValue::from_bytes(&value.inner[..])?)
71 }
72}
73
74impl From<HttpHeaderValue> for HeaderValue {
75 fn from(value: HttpHeaderValue) -> Self {
76 HeaderValue {
77 inner: Bytes::copy_from_slice(value.as_bytes()),
78 }
79 }
80}
81
82impl From<&str> for HeaderValue {
83 fn from(string: &str) -> Self {
84 Self::from(string.to_owned())
85 }
86}
87
88impl TryFrom<HeaderMap> for HttpHeaderMap {
89 type Error = Error;
90 fn try_from(
91 headers: HeaderMap,
92 ) -> std::result::Result<Self, <Self as std::convert::TryFrom<HeaderMap>>::Error> {
93 let mut result = HttpHeaderMap::new();
94 for (key, value) in headers {
95 result.append(
96 HttpHeaderName::try_from(key)?,
97 HttpHeaderValue::try_from(value)?,
98 );
99 }
100
101 Ok(result)
102 }
103}
104
105impl IntoIterator for HeaderMap {
106 type Item = (HeaderName, HeaderValue);
107 type IntoIter = IntoIter;
108 fn into_iter(self) -> <Self as std::iter::IntoIterator>::IntoIter {
109 IntoIter {
110 inner: self.map.into_iter(),
111 }
112 }
113}
114
115impl HeaderMap {
116 pub fn new() -> Self {
117 HeaderMap {
118 map: HashMap::new(),
119 }
120 }
121
122 pub fn insert<T: Into<HeaderName>, U: Into<HeaderValue>>(
123 &mut self,
124 key: T,
125 value: U,
126 ) -> Option<HeaderValue> {
127 self.map.insert(key.into(), value.into())
128 }
129}
130
131impl Iterator for IntoIter {
132 type Item = (HeaderName, HeaderValue);
133 fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {
134 self.inner.next()
135 }
136}