headers/common/
access_control_request_method.rs1use http::{HeaderName, HeaderValue, Method};
2
3use crate::{Error, Header};
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
29pub struct AccessControlRequestMethod(Method);
30
31impl Header for AccessControlRequestMethod {
32    fn name() -> &'static HeaderName {
33        &::http::header::ACCESS_CONTROL_REQUEST_METHOD
34    }
35
36    fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, Error> {
37        values
38            .next()
39            .and_then(|value| Method::from_bytes(value.as_bytes()).ok())
40            .map(AccessControlRequestMethod)
41            .ok_or_else(Error::invalid)
42    }
43
44    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
45        let s = match self.0 {
47            Method::GET => "GET",
48            Method::POST => "POST",
49            Method::PUT => "PUT",
50            Method::DELETE => "DELETE",
51            _ => {
52                let val = HeaderValue::from_str(self.0.as_ref())
53                    .expect("Methods are also valid HeaderValues");
54                values.extend(::std::iter::once(val));
55                return;
56            }
57        };
58
59        values.extend(::std::iter::once(HeaderValue::from_static(s)));
60    }
61}
62
63impl From<Method> for AccessControlRequestMethod {
64    fn from(method: Method) -> AccessControlRequestMethod {
65        AccessControlRequestMethod(method)
66    }
67}
68
69impl From<AccessControlRequestMethod> for Method {
70    fn from(method: AccessControlRequestMethod) -> Method {
71        method.0
72    }
73}