generic_async_http_client/
header.rs

1use std::borrow::Borrow;
2use std::convert::TryFrom;
3
4use crate::imp;
5
6/// A HTTP Header Name
7///
8/// It can be converted from `&[u8]` and `&str`.
9/// You can obtain `&str` and `&[u8]` references and compare with str.
10/// ```
11/// # use std::convert::TryInto;
12/// # use generic_async_http_client::HeaderName;
13/// let hn: HeaderName = "test".try_into().unwrap();
14/// assert!(hn=="test");
15/// let s: &str = hn.as_ref();
16/// assert!(s.is_ascii());
17/// let b: &[u8] = hn.as_ref();
18/// assert!(b.contains(&b's'));
19/// ```
20#[derive(Clone, PartialEq, Eq, Hash, Debug)]
21#[repr(transparent)]
22pub struct HeaderName(imp::HeaderName);
23impl<'a> TryFrom<&'a str> for HeaderName {
24    type Error = imp::Error;
25    #[cfg_attr(
26        not(any(feature = "use_hyper", feature = "use_async_h1")),
27        allow(unused_variables)
28    )]
29    #[inline]
30    fn try_from(t: &'a str) -> Result<HeaderName, Self::Error> {
31        #[cfg(feature = "use_async_h1")]
32        return Ok(HeaderName(imp::HeaderName::from_string(t.to_string())?));
33        #[cfg(feature = "use_hyper")]
34        return Ok(HeaderName(imp::HeaderName::try_from(t)?));
35        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
36        return Ok(HeaderName(imp::HeaderName(t.to_string())));
37    }
38}
39impl<'a> TryFrom<&'a [u8]> for HeaderName {
40    type Error = imp::Error;
41    #[cfg_attr(
42        not(any(feature = "use_hyper", feature = "use_async_h1")),
43        allow(unused_variables)
44    )]
45    #[inline]
46    fn try_from(t: &'a [u8]) -> Result<Self, Self::Error> {
47        #[cfg(feature = "use_async_h1")]
48        return Ok(HeaderName(imp::HeaderName::from_bytes(t.to_vec())?));
49        #[cfg(feature = "use_hyper")]
50        return Ok(HeaderName(imp::HeaderName::from_bytes(t)?));
51        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
52        return t.to_vec().try_into();
53    }
54}
55impl TryFrom<Vec<u8>> for HeaderName {
56    type Error = imp::Error;
57    #[cfg_attr(
58        not(any(feature = "use_hyper", feature = "use_async_h1")),
59        allow(unused_variables)
60    )]
61    #[inline]
62    fn try_from(t: Vec<u8>) -> Result<Self, Self::Error> {
63        #[cfg(feature = "use_async_h1")]
64        return Ok(HeaderName(imp::HeaderName::from_bytes(t)?));
65        #[cfg(feature = "use_hyper")]
66        return Ok(HeaderName(imp::HeaderName::from_bytes(&t)?));
67        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
68        return Ok(HeaderName(imp::HeaderName(String::from_utf8(t).unwrap())));
69    }
70}
71
72impl From<HeaderName> for imp::HeaderName {
73    #[inline]
74    fn from(t: HeaderName) -> Self {
75        t.0
76    }
77}
78impl<'a> From<&'a imp::HeaderName> for &'a HeaderName {
79    #[inline]
80    fn from(t: &'a imp::HeaderName) -> Self {
81        //safe because repr(transparent)
82        unsafe { std::mem::transmute(t) }
83    }
84}
85impl AsRef<str> for HeaderName {
86    #[inline]
87    fn as_ref(&self) -> &str {
88        #[cfg(feature = "use_hyper")]
89        return self.0.as_ref();
90        #[cfg(feature = "use_async_h1")]
91        return self.0.as_str();
92        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
93        return self.0 .0.as_str();
94    }
95}
96
97impl AsRef<[u8]> for HeaderName {
98    #[inline]
99    fn as_ref(&self) -> &[u8] {
100        #[cfg(feature = "use_hyper")]
101        return self.0.as_ref();
102        #[cfg(feature = "use_async_h1")]
103        return self.0.as_str().as_bytes();
104        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
105        return self.0 .0.as_bytes();
106    }
107}
108
109impl Borrow<str> for HeaderName {
110    #[inline]
111    fn borrow(&self) -> &str {
112        #[cfg(feature = "use_hyper")]
113        return self.as_ref();
114        #[cfg(feature = "use_async_h1")]
115        return self.0.as_str();
116        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
117        return self.0 .0.as_str();
118    }
119}
120impl PartialEq<str> for HeaderName {
121    #[cfg_attr(
122        not(any(feature = "use_hyper", feature = "use_async_h1")),
123        allow(unused_variables)
124    )]
125    #[inline]
126    fn eq(&self, other: &str) -> bool {
127        #[cfg(feature = "use_hyper")]
128        return self.0 == other;
129        #[cfg(feature = "use_async_h1")]
130        return self.0.as_str() == other;
131        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
132        return self.0 .0 == other;
133    }
134}
135impl PartialEq<&str> for HeaderName {
136    #[inline]
137    fn eq(&self, other: &&str) -> bool {
138        self.eq(*other)
139    }
140}
141
142/// A HTTP Header Value
143///
144/// It can be converted from `&[u8]` and `&str`.
145/// You can obtain a `&[u8]` reference and compare with str and `&[u8]`.
146/// You can also convert it to `String` if it is valid utf-8.
147/// ```
148/// # use std::convert::TryInto;
149/// # use generic_async_http_client::HeaderValue;
150/// let hv: HeaderValue = b"test"[..].try_into().unwrap();
151/// assert!(hv=="test");
152/// assert!(hv.as_ref().contains(&b's'));
153/// let val: String = hv.try_into().unwrap();
154/// ```
155#[derive(Debug, Clone)]
156#[repr(transparent)]
157pub struct HeaderValue(imp::HeaderValue);
158
159impl HeaderValue {
160    /// Try to parse the HeaderValue as some Type (implementing FromStr)
161    /// ```
162    /// # use std::convert::TryInto;
163    /// # use generic_async_http_client::HeaderValue;
164    /// let hv: HeaderValue = b"4"[..].try_into().unwrap();
165    /// let four: u32 = hv.parse().unwrap();
166    /// ```
167    pub fn parse<T: std::str::FromStr>(&self) -> Option<T> {
168        self.as_str().ok()?.parse::<T>().ok()
169    }
170    // Get a `&str` reference of this HeaderValue
171    pub fn as_str(&self) -> Result<&str, std::str::Utf8Error> {
172        #[cfg(feature = "use_async_h1")]
173        return Ok(self.0.as_str());
174        #[cfg(not(feature = "use_async_h1"))]
175        std::str::from_utf8(self.as_ref())
176    }
177}
178impl<'a> TryFrom<&'a str> for HeaderValue {
179    type Error = imp::Error;
180    #[inline]
181    fn try_from(t: &'a str) -> Result<Self, Self::Error> {
182        Ok(HeaderValue(imp::HeaderValue::try_from(t)?))
183    }
184}
185impl<'a> TryFrom<&'a [u8]> for HeaderValue {
186    type Error = imp::Error;
187    #[cfg_attr(
188        not(any(feature = "use_hyper", feature = "use_async_h1")),
189        allow(unused_variables)
190    )]
191    #[inline]
192    fn try_from(t: &'a [u8]) -> Result<Self, Self::Error> {
193        #[cfg(feature = "use_async_h1")]
194        return Ok(HeaderValue(imp::HeaderValue::from_bytes(t.to_vec())?));
195        #[cfg(feature = "use_hyper")]
196        return Ok(HeaderValue(imp::HeaderValue::from_bytes(t)?));
197        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
198        return Ok(HeaderValue(imp::HeaderValue(t.to_vec())));
199    }
200}
201
202impl From<HeaderValue> for imp::HeaderValue {
203    #[inline]
204    fn from(t: HeaderValue) -> Self {
205        t.0
206    }
207}
208impl From<imp::HeaderValue> for HeaderValue {
209    #[inline]
210    fn from(t: imp::HeaderValue) -> Self {
211        HeaderValue(t)
212    }
213}
214impl<'a> From<&'a imp::HeaderValue> for &'a HeaderValue {
215    #[inline]
216    fn from(t: &'a imp::HeaderValue) -> Self {
217        //safe because repr(transparent)
218        unsafe { std::mem::transmute(t) }
219    }
220}
221impl AsRef<[u8]> for HeaderValue {
222    #[inline]
223    fn as_ref(&self) -> &[u8] {
224        #[cfg(feature = "use_hyper")]
225        return self.0.as_ref();
226        #[cfg(feature = "use_async_h1")]
227        return self.0.as_str().as_bytes();
228        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
229        return self.0 .0.as_ref();
230    }
231}
232impl std::convert::TryInto<String> for HeaderValue {
233    type Error = std::string::FromUtf8Error;
234    #[cfg_attr(feature = "use_async_h1", allow(unreachable_code))]
235    fn try_into(self) -> Result<String, Self::Error> {
236        #[cfg(feature = "use_async_h1")]
237        return Ok(self.0.as_str().to_string());
238        String::from_utf8(self.as_ref().to_vec())
239    }
240}
241impl std::convert::TryInto<String> for &HeaderValue {
242    type Error = std::string::FromUtf8Error;
243    #[cfg_attr(feature = "use_async_h1", allow(unreachable_code))]
244    fn try_into(self) -> Result<String, Self::Error> {
245        #[cfg(feature = "use_async_h1")]
246        return Ok(self.0.as_str().to_string());
247        String::from_utf8(self.as_ref().to_vec())
248    }
249}
250impl PartialEq<str> for HeaderValue {
251    #[cfg_attr(
252        not(any(feature = "use_hyper", feature = "use_async_h1")),
253        allow(unused_variables)
254    )]
255    #[inline]
256    fn eq(&self, other: &str) -> bool {
257        #[cfg(feature = "use_hyper")]
258        return self.0 == other.as_bytes();
259        #[cfg(feature = "use_async_h1")]
260        return self.0.as_str() == other;
261        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
262        return self.0 .0 == other.as_bytes();
263    }
264}
265impl PartialEq<&str> for HeaderValue {
266    #[inline]
267    fn eq(&self, other: &&str) -> bool {
268        self.eq(*other)
269    }
270}
271
272impl PartialEq<[u8]> for HeaderValue {
273    #[cfg_attr(
274        not(any(feature = "use_hyper", feature = "use_async_h1")),
275        allow(unused_variables)
276    )]
277    #[inline]
278    fn eq(&self, other: &[u8]) -> bool {
279        #[cfg(feature = "use_hyper")]
280        return self.0 == other;
281        #[cfg(feature = "use_async_h1")]
282        return self.as_ref() == other;
283        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
284        return self.0 .0 == other;
285    }
286}
287impl PartialEq<&[u8]> for HeaderValue {
288    #[inline]
289    fn eq(&self, other: &&[u8]) -> bool {
290        self.eq(*other)
291    }
292}