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