rama_http_headers/common/
allow.rs

1use std::iter::FromIterator;
2
3use rama_http_types::{HeaderValue, Method};
4
5use crate::util::FlatCsv;
6
7/// `Allow` header, defined in [RFC7231](https://datatracker.ietf.org/doc/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/// use rama_http_headers::Allow;
29/// use rama_http_types::Method;
30///
31/// let allow = vec![Method::GET, Method::POST]
32///     .into_iter()
33///     .collect::<Allow>();
34/// ```
35#[derive(Clone, Debug, PartialEq)]
36pub struct Allow(FlatCsv);
37
38derive_header! {
39    Allow(_),
40    name: ALLOW
41}
42
43impl Allow {
44    /// Returns an iterator over `Method`s contained within.
45    pub fn iter(&self) -> impl Iterator<Item = Method> + '_ {
46        self.0.iter().filter_map(|s| s.parse().ok())
47    }
48}
49
50impl FromIterator<Method> for Allow {
51    fn from_iter<I>(iter: I) -> Self
52    where
53        I: IntoIterator<Item = Method>,
54    {
55        let flat = iter
56            .into_iter()
57            .map(|method| {
58                method
59                    .as_str()
60                    .parse::<HeaderValue>()
61                    .expect("Method is a valid HeaderValue")
62            })
63            .collect();
64        Allow(flat)
65    }
66}