headers/common/
allow.rs

1use std::iter::FromIterator;
2
3use http::Method;
4
5use util::FlatCsv;
6
7/// `Allow` header, defined in [RFC7231](http://tools.ietf.org/html/rfc7231#section-7.4.1)
8///
9/// The `Allow` header field lists the set of methods advertised as
10/// supported by the target resource.  The purpose of this field is
11/// strictly to inform the recipient of valid request methods associated
12/// with the resource.
13///
14/// # ABNF
15///
16/// ```text
17/// Allow = #method
18/// ```
19///
20/// # Example values
21/// * `GET, HEAD, PUT`
22/// * `OPTIONS, GET, PUT, POST, DELETE, HEAD, TRACE, CONNECT, PATCH, fOObAr`
23/// * ``
24///
25/// # Examples
26///
27/// ```
28/// # extern crate headers;
29/// extern crate http;
30/// use headers::Allow;
31/// use http::Method;
32///
33/// let allow = vec![Method::GET, Method::POST]
34///     .into_iter()
35///     .collect::<Allow>();
36/// ```
37#[derive(Clone, Debug, PartialEq)]
38pub struct Allow(FlatCsv);
39
40derive_header! {
41    Allow(_),
42    name: ALLOW
43}
44
45impl Allow {
46    /// Returns an iterator over `Method`s contained within.
47    pub fn iter<'a>(&'a self) -> impl Iterator<Item = Method> + 'a {
48        self.0.iter().filter_map(|s| s.parse().ok())
49    }
50}
51
52impl FromIterator<Method> for Allow {
53    fn from_iter<I>(iter: I) -> Self
54    where
55        I: IntoIterator<Item = Method>,
56    {
57        let flat = iter
58            .into_iter()
59            .map(|method| {
60                method
61                    .as_str()
62                    .parse::<::HeaderValue>()
63                    .expect("Method is a valid HeaderValue")
64            })
65            .collect();
66        Allow(flat)
67    }
68}