use rama_core::telemetry::tracing;
use rama_http_types::{HeaderName, HeaderValue, Method};
use crate::{Error, HeaderDecode, HeaderEncode, TypedHeader};
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct AccessControlRequestMethod(pub Method);
impl TypedHeader for AccessControlRequestMethod {
fn name() -> &'static HeaderName {
&::rama_http_types::header::ACCESS_CONTROL_REQUEST_METHOD
}
}
impl HeaderDecode for AccessControlRequestMethod {
fn decode<'i, I: Iterator<Item = &'i HeaderValue>>(values: &mut I) -> Result<Self, Error> {
values
.next()
.and_then(|value| Method::from_bytes(value.as_bytes()).ok())
.map(AccessControlRequestMethod)
.ok_or_else(Error::invalid)
}
}
impl HeaderEncode for AccessControlRequestMethod {
fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
let s = match self.0 {
Method::GET => "GET",
Method::POST => "POST",
Method::PUT => "PUT",
Method::DELETE => "DELETE",
Method::HEAD => "HEAD",
Method::OPTIONS => "OPTIONS",
Method::CONNECT => "CONNECT",
Method::PATCH => "PATCH",
Method::TRACE => "TRACE",
_ => {
match HeaderValue::from_str(self.0.as_ref()) {
Ok(value) => values.extend(::std::iter::once(value)),
Err(err) => {
tracing::debug!(
"failed to encode access-control-request-method value as header: {err}"
);
}
}
return;
}
};
values.extend(::std::iter::once(HeaderValue::from_static(s)));
}
}
impl From<Method> for AccessControlRequestMethod {
fn from(method: Method) -> Self {
Self(method)
}
}
impl From<AccessControlRequestMethod> for Method {
fn from(method: AccessControlRequestMethod) -> Self {
method.0
}
}