use std::{
borrow::Cow,
collections::BTreeSet,
error::Error,
fmt::{self, Display, Formatter},
};
use http::{HeaderMap, HeaderValue, Method, header::ALLOW};
use super::{HttpPolicy, PolicyError, sealed::Sealed};
#[derive(Debug, Clone, Eq)]
pub struct AllowedMethods {
methods: Cow<'static, [Method]>,
}
impl PartialEq for AllowedMethods {
fn eq(&self, other: &Self) -> bool {
self.canonical_names() == other.canonical_names()
}
}
impl AllowedMethods {
pub fn new(methods: impl IntoIterator<Item = Method>) -> Result<Self, AllowedMethodsError> {
let methods = methods.into_iter().collect::<Vec<_>>();
if methods.is_empty() {
return Err(AllowedMethodsError::Empty);
}
Ok(Self {
methods: Cow::Owned(methods),
})
}
pub const fn from_static(methods: &'static [Method]) -> Self {
assert!(!methods.is_empty(), "allowed method set must not be empty");
Self {
methods: Cow::Borrowed(methods),
}
}
fn canonical_names(&self) -> BTreeSet<&str> {
self.methods.iter().map(Method::as_str).collect()
}
fn header_value(&self) -> Result<HeaderValue, PolicyError> {
self.canonical_names()
.into_iter()
.collect::<Vec<_>>()
.join(", ")
.parse()
.map_err(|_| PolicyError::new("allowed methods are not a header value"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AllowedMethodsError {
Empty,
}
impl Display for AllowedMethodsError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => formatter.write_str("allowed method set must not be empty"),
}
}
}
impl Error for AllowedMethodsError {}
#[derive(Debug, Clone, Copy, Default)]
pub struct MethodNotAllowed;
impl Sealed for MethodNotAllowed {}
impl HttpPolicy for MethodNotAllowed {
type Input = AllowedMethods;
const STATUS: u16 = 405;
const NAME: &'static str = "method_not_allowed";
const REQUIRED_HEADERS: &'static [&'static str] = &["allow"];
fn headers(input: Self::Input) -> Result<HeaderMap, PolicyError> {
let mut headers = HeaderMap::new();
headers.insert(ALLOW, input.header_value()?);
Ok(headers)
}
}