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 Err(imp::Error {});
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 Err(imp::Error {});
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 Err(imp::Error {});
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 "";
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 &[];
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 "";
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 false;
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    {
169        self.as_str().ok()?.parse::<T>().ok()
170    }
171    // Get a `&str` reference of this HeaderValue
172    pub fn as_str(&self) -> Result<&str, std::str::Utf8Error>
173    {
174        #[cfg(feature = "use_async_h1")]
175        return Ok(self.0.as_str());
176        #[cfg(not(feature = "use_async_h1"))]
177        std::str::from_utf8(self.as_ref())
178    }
179}
180impl<'a> TryFrom<&'a str> for HeaderValue {
181    type Error = imp::Error;
182    #[inline]
183    fn try_from(t: &'a str) -> Result<Self, Self::Error> {
184        Ok(HeaderValue(imp::HeaderValue::try_from(t)?))
185    }
186}
187impl<'a> TryFrom<&'a [u8]> for HeaderValue {
188    type Error = imp::Error;
189    #[cfg_attr(
190        not(any(feature = "use_hyper", feature = "use_async_h1")),
191        allow(unused_variables)
192    )]
193    #[inline]
194    fn try_from(t: &'a [u8]) -> Result<Self, Self::Error> {
195        #[cfg(feature = "use_async_h1")]
196        return Ok(HeaderValue(imp::HeaderValue::from_bytes(t.to_vec())?));
197        #[cfg(feature = "use_hyper")]
198        return Ok(HeaderValue(imp::HeaderValue::from_bytes(t)?));
199        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
200        return Err(imp::Error {});
201    }
202}
203
204impl From<HeaderValue> for imp::HeaderValue {
205    #[inline]
206    fn from(t: HeaderValue) -> Self {
207        t.0
208    }
209}
210impl From<imp::HeaderValue> for HeaderValue {
211    #[inline]
212    fn from(t: imp::HeaderValue) -> Self {
213        HeaderValue(t)
214    }
215}
216impl<'a> From<&'a imp::HeaderValue> for &'a HeaderValue {
217    #[inline]
218    fn from(t: &'a imp::HeaderValue) -> Self {
219        //safe because repr(transparent)
220        unsafe { std::mem::transmute(t) }
221    }
222}
223impl AsRef<[u8]> for HeaderValue {
224    #[inline]
225    fn as_ref(&self) -> &[u8] {
226        #[cfg(feature = "use_hyper")]
227        return self.0.as_ref();
228        #[cfg(feature = "use_async_h1")]
229        return self.0.as_str().as_bytes();
230        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
231        return &[];
232    }
233}
234impl std::convert::TryInto<String> for HeaderValue {
235    type Error = std::string::FromUtf8Error;
236    #[cfg_attr(feature = "use_async_h1", allow(unreachable_code))]
237    fn try_into(self) -> Result<String, Self::Error> {
238        #[cfg(feature = "use_async_h1")]
239        return Ok(self.0.as_str().to_string());
240        String::from_utf8(self.as_ref().to_vec())
241    }
242}
243impl std::convert::TryInto<String> for &HeaderValue {
244    type Error = std::string::FromUtf8Error;
245    #[cfg_attr(feature = "use_async_h1", allow(unreachable_code))]
246    fn try_into(self) -> Result<String, Self::Error> {
247        #[cfg(feature = "use_async_h1")]
248        return Ok(self.0.as_str().to_string());
249        String::from_utf8(self.as_ref().to_vec())
250    }
251}
252impl PartialEq<str> for HeaderValue {
253    #[cfg_attr(
254        not(any(feature = "use_hyper", feature = "use_async_h1")),
255        allow(unused_variables)
256    )]
257    #[inline]
258    fn eq(&self, other: &str) -> bool {
259        #[cfg(feature = "use_hyper")]
260        return self.0 == other.as_bytes();
261        #[cfg(feature = "use_async_h1")]
262        return self.0.as_str() == other;
263        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
264        return false;
265    }
266}
267impl PartialEq<&str> for HeaderValue {
268    #[inline]
269    fn eq(&self, other: &&str) -> bool {
270        self.eq(*other)
271    }
272}
273
274impl PartialEq<[u8]> for HeaderValue {
275    #[cfg_attr(
276        not(any(feature = "use_hyper", feature = "use_async_h1")),
277        allow(unused_variables)
278    )]
279    #[inline]
280    fn eq(&self, other: &[u8]) -> bool {
281        #[cfg(feature = "use_hyper")]
282        return self.0 == other;
283        #[cfg(feature = "use_async_h1")]
284        return self.as_ref() == other;
285        #[cfg(not(any(feature = "use_hyper", feature = "use_async_h1")))]
286        return false;
287    }
288}
289impl PartialEq<&[u8]> for HeaderValue {
290    #[inline]
291    fn eq(&self, other: &&[u8]) -> bool {
292        self.eq(*other)
293    }
294}