use std::{fmt, sync::Arc, time::Duration};
use axol_http::{request::RequestPartsRef, typed_headers::AccessControlMaxAge};
#[must_use]
#[derive(Clone)]
pub enum MaxAge {
Exact(Option<Duration>),
Fn(super::CorsPredicate<Duration>),
}
impl Default for MaxAge {
fn default() -> Self {
Self::Exact(None)
}
}
impl MaxAge {
pub fn exact(max_age: Duration) -> Self {
Self::Exact(Some(max_age))
}
pub fn dynamic<F>(f: F) -> Self
where
F: Fn(&str, RequestPartsRef<'_>) -> Duration + Send + Sync + 'static,
{
Self::Fn(Arc::new(f))
}
pub(super) fn to_header(
&self,
origin: Option<&str>,
parts: RequestPartsRef<'_>,
) -> Option<AccessControlMaxAge> {
let max_age = match self {
Self::Exact(v) => (*v)?,
Self::Fn(c) => c(origin?, parts),
};
Some(max_age.into())
}
}
impl From<Duration> for MaxAge {
fn from(max_age: Duration) -> Self {
Self::exact(max_age)
}
}
impl fmt::Debug for MaxAge {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Exact(arg0) => f.debug_tuple("Exact").field(arg0).finish(),
Self::Fn(_) => f.debug_tuple("Fn").finish(),
}
}
}