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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
pub use impls::accept::Accept;
pub use impls::accept_encoding::AcceptEncoding;
pub use impls::allow::Allow;
pub use impls::auth_scheme::AuthScheme;
pub use impls::authorization::Authorization;
pub use impls::content_coding::ContentCoding;
pub use impls::content_encoding::ContentEncoding;
pub use impls::content_length::ContentLength;
pub use impls::content_type::ContentType;
pub use impls::credentials::Credentials;
pub use impls::host::Host;
pub use impls::http_date::HttpDate;
pub use impls::proxy_authorization::ProxyAuthorization;
pub use impls::quality::{Quality, QualityItem};
pub use impls::retry_after::RetryAfter;
pub use impls::token68::Token68;

macro_rules! header {
    // #rule
    ($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)*) => {
        $(#[$a])*
        #[derive(Clone, Debug, PartialEq)]
        pub struct $id(pub ::std::vec::Vec<$item>);
        header!(@deref $id => Vec<$item>);
        header!(@derefmut $id => Vec<$item>);
        impl $crate::Header for $id {
            #[inline]
            fn name() -> &'static $crate::http::header::HeaderName {
                &$n
            }

            #[inline]
            fn from_values(
                values: &mut $crate::http::header::ValueIter<$crate::http::header::HeaderValue>,
            ) -> ::std::result::Result<::std::option::Option<$id>, $crate::Error>
            {
                $crate::util::parse_comma_delimited(values).map(|r| r.map($id))
            }

            #[inline]
            fn to_values(&self, values: &mut $crate::ToValues) {
                $crate::util::encode_comma_delimited(&self.0, values);
            }
        }
    };
    // 1#rule
    ($(#[$a:meta])*($id:ident, $n:expr) => ($item:ty)+) => {
        $(#[$a])*
        #[derive(Clone, Debug, PartialEq)]
        pub struct $id(::std::vec::Vec<$item>);
        header!(@deref $id => Vec<$item>);

        impl $id {
            #[inline]
            pub fn new(values: Vec<$item>) -> ::std::result::Result<$id, $crate::Error> {
                if values.is_empty() {
                    Err($crate::Error::too_few_values())
                } else {
                    Ok($id(values))
                }
            }
        }

        impl $crate::Header for $id {
            #[inline]
            fn name() -> &'static $crate::http::header::HeaderName {
                &$n
            }

            #[inline]
            fn from_values(
                values: &mut $crate::http::header::ValueIter<$crate::http::header::HeaderValue>,
            ) -> ::std::result::Result<::std::option::Option<$id>, $crate::Error>
            {
                match $crate::util::parse_comma_delimited(values)? {
                    Some(values) => $id::new(values).map(Some),
                    None => Ok(None),
                }
            }

            #[inline]
            fn to_values(&self, values: &mut $crate::ToValues) {
                $crate::util::encode_comma_delimited(&self.0, values);
            }
        }

        impl ::std::convert::From<$item> for $id {
            #[inline]
            fn from(value: $item) -> $id {
                $id(vec![value])
            }
        }
    };
    // single value
    ($(#[$a:meta])*($id:ident, $n:expr) => [$value:ty]) => {
        $(#[$a])*
        #[derive(Clone, Debug, PartialEq)]
        pub struct $id(pub $value);
        header!(@deref $id => $value);
        header!(@derefmut  $id => $value);
        impl $crate::Header for $id {
            #[inline]
            fn name() -> &'static $crate::http::header::HeaderName {
                &$n
            }

            #[inline]
            fn from_values(
                values: &mut $crate::http::header::ValueIter<$crate::http::header::HeaderValue>,
            ) -> ::std::result::Result<::std::option::Option<$id>, $crate::Error>
            {
                $crate::util::parse_single_value(values).map(|r| r.map($id))
            }

            #[inline]
            fn to_values(&self, values: &mut $crate::ToValues) {
                $crate::util::encode_single_value(&self.0, values);
            }
        }
    };
    (@deref $id:ident => $t:ty) => {
        impl ::std::ops::Deref for $id {
            type Target = $t;

            #[inline]
            fn deref(&self) -> &$t {
                &self.0
            }
        }
    };
    (@derefmut $id:ident => $t:ty) => {
        impl ::std::ops::DerefMut for $id {
            #[inline]
            fn deref_mut(&mut self) -> &mut $t {
                &mut self.0
            }
        }
    };
}

macro_rules! token {
    (
        $(#[$attr:meta])* $name:ident => {
            $(
                $(#[$variant_attr:meta])*
                $variant:ident => $s:expr => [$($alias:expr),*],
            )*
        }
    ) => {
        #[derive(Debug, Clone, PartialEq, Eq)]
        #[allow(non_camel_case_types)]
        enum Inner {
            $(
                $variant,
            )*
            Other(String),
        }

        $(#[$attr])*
        #[derive(Debug, Clone, PartialEq, Eq)]
        pub struct $name(Inner);

        impl $name {
            $(
                $(#[$variant_attr])*
                pub const $variant: $name = $name(Inner::$variant);
            )*

            /// Constructs a new instance of this value from a string.
            ///
            /// An error is returned if the string is not a valid token.
            pub fn new(s: &str) -> ::std::result::Result<$name, $crate::Error> {
                $(
                    if s.eq_ignore_ascii_case($s) {
                        return Ok($name(Inner::$variant));
                    }

                    $(
                        if s.eq_ignore_ascii_case($alias) {
                            return Ok($name(Inner::$variant));
                        }
                    )*
                )*

                if $crate::util::is_token(s) {
                    Ok($name(Inner::Other(s.to_ascii_lowercase())))
                } else {
                    Err($crate::Error::invalid_value())
                }
            }

            /// Returns the string representation of this token.
            pub fn as_str(&self) -> &str {
                match self.0 {
                    $(
                        Inner::$variant => $s,
                    )*
                    Inner::Other(ref s) => s,
                }
            }
        }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                fmt.write_str(self.as_str())
            }
        }

        impl ::std::str::FromStr for $name {
            type Err = $crate::Error;

            fn from_str(s: &str) -> ::std::result::Result<$name, $crate::Error> {
                $name::new(s)
            }
        }
    }
}

mod accept;
mod accept_encoding;
mod allow;
mod auth_scheme;
mod authorization;
mod content_coding;
mod content_encoding;
mod content_length;
mod content_type;
mod credentials;
mod host;
mod http_date;
mod proxy_authorization;
mod quality;
mod retry_after;
mod token68;