mod authentication;
mod header;
mod mandatory;
mod retry;
use std::{
error::Error,
fmt::{self, Debug, Display, Formatter},
};
use http::HeaderMap;
use crate::diagnostic::DiagnosticType;
pub use authentication::{
BasicChallenge, BasicChallengeError, BasicUnauthorized, BearerChallenge, BearerChallengeError,
BearerUnauthorized,
};
pub use header::{AllowedMethods, AllowedMethodsError, MethodNotAllowed};
pub(crate) use mandatory::mandatory_headers;
pub use retry::{RetryAfter, RetryAfterError, RetryAfterPolicy};
mod sealed {
use http::HeaderMap;
use super::PolicyResponseIssue;
pub trait Sealed {
fn validate_response_headers(_headers: &HeaderMap) -> Result<(), PolicyResponseIssue> {
Ok(())
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PolicyResponseIssue {
pub(crate) header: &'static str,
pub(crate) expected: &'static str,
}
pub(crate) fn validate_typed_response_headers<P: HttpPolicy>(
headers: &HeaderMap,
) -> Result<(), PolicyResponseIssue> {
<P as sealed::Sealed>::validate_response_headers(headers)
}
pub(crate) fn validate_named_response_headers(
policy: &str,
headers: &HeaderMap,
) -> Result<(), PolicyResponseIssue> {
if policy == BasicUnauthorized::NAME {
validate_typed_response_headers::<BasicUnauthorized>(headers)
} else if policy == BearerUnauthorized::NAME {
validate_typed_response_headers::<BearerUnauthorized>(headers)
} else {
Ok(())
}
}
pub trait HttpPolicy: sealed::Sealed + Debug + Send + Sync + 'static {
type Input: Debug + Send + Sync + 'static;
const STATUS: u16;
const NAME: &'static str;
const REQUIRED_HEADERS: &'static [&'static str];
fn headers(input: Self::Input) -> Result<HeaderMap, PolicyError>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct Fixed<const STATUS: u16>;
impl<const STATUS: u16> sealed::Sealed for Fixed<STATUS> {}
impl<const STATUS: u16> HttpPolicy for Fixed<STATUS> {
type Input = ();
const STATUS: u16 = STATUS;
const NAME: &'static str = "fixed";
const REQUIRED_HEADERS: &'static [&'static str] = &[];
fn headers(_input: Self::Input) -> Result<HeaderMap, PolicyError> {
Ok(HeaderMap::new())
}
}
pub trait HttpProblemType: DiagnosticType {
type Policy: HttpPolicy;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PolicyError {
message: &'static str,
}
impl PolicyError {
pub(super) const fn new(message: &'static str) -> Self {
Self { message }
}
}
impl Display for PolicyError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
formatter.write_str(self.message)
}
}
impl Error for PolicyError {}