| 1 |
use HeaderValue; |
| 2 |
|
| 3 |
//pub use self::charset::Charset; |
| 4 |
//pub use self::encoding::Encoding; |
| 5 |
pub(crate) use self::entity::{EntityTag, EntityTagRange}; |
| 6 |
pub(crate) use self::flat_csv::{FlatCsv, SemiColon}; |
| 7 |
pub(crate) use self::fmt::fmt; |
| 8 |
pub(crate) use self::http_date::HttpDate; |
| 9 |
pub(crate) use self::iter::IterExt; |
| 10 |
//pub use language_tags::LanguageTag; |
| 11 |
//pub use self::quality_value::{Quality, QualityValue}; |
| 12 |
pub(crate) use self::seconds::Seconds; |
| 13 |
pub(crate) use self::value_string::HeaderValueString; |
| 14 |
|
| 15 |
//mod charset; |
| 16 |
pub(crate) mod csv; |
| 17 |
//mod encoding; |
| 18 |
mod entity; |
| 19 |
mod flat_csv; |
| 20 |
mod fmt; |
| 21 |
mod http_date; |
| 22 |
mod iter; |
| 23 |
//mod quality_value; |
| 24 |
mod seconds; |
| 25 |
mod value_string; |
| 26 |
|
| 27 |
macro_rules! error_type { |
| 28 |
($name:ident) => { |
| 29 |
#[doc(hidden)] |
| 30 |
pub struct $name { |
| 31 |
_inner: (), |
| 32 |
} |
| 33 |
|
| 34 |
impl ::std::fmt::Debug for $name { |
| 35 |
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 36 |
f.debug_struct(stringify!($name)).finish() |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl ::std::fmt::Display for $name { |
| 41 |
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 42 |
f.write_str(stringify!($name)) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl ::std::error::Error for $name {} |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
macro_rules! derive_header { |
| 51 |
($type:ident(_), name: $name:ident) => { |
| 52 |
impl crate::Header for $type { |
| 53 |
fn name() -> &'static ::http::header::HeaderName { |
| 54 |
&::http::header::$name |
| 55 |
} |
| 56 |
|
| 57 |
fn decode<'i, I>(values: &mut I) -> Result<Self, ::Error> |
| 58 |
where |
| 59 |
I: Iterator<Item = &'i ::http::header::HeaderValue>, |
| 60 |
{ |
| 61 |
::util::TryFromValues::try_from_values(values).map($type) |
| 62 |
} |
| 63 |
|
| 64 |
fn encode<E: Extend<::HeaderValue>>(&self, values: &mut E) { |
| 65 |
values.extend(::std::iter::once((&self.0).into())); |
| 66 |
} |
| 67 |
} |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
/// A helper trait for use when deriving `Header`. |
| 72 |
pub(crate) trait TryFromValues: Sized { |
| 73 |
/// Try to convert from the values into an instance of `Self`. |
| 74 |
fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error> |
| 75 |
where |
| 76 |
Self: Sized, |
| 77 |
I: Iterator<Item = &'i HeaderValue>; |
| 78 |
} |
| 79 |
|
| 80 |
impl TryFromValues for HeaderValue { |
| 81 |
fn try_from_values<'i, I>(values: &mut I) -> Result<Self, ::Error> |
| 82 |
where |
| 83 |
I: Iterator<Item = &'i HeaderValue>, |
| 84 |
{ |
| 85 |
values.next().cloned().ok_or_else(|| ::Error::invalid()) |
| 86 |
} |
| 87 |
} |