use crate::quota::Quota;
use axum_core::extract::FromRef;
use axum_core::response::IntoResponse;
use http::request::Parts;
use std::convert::Infallible;
pub trait QuotaSource<S>: Send + Sync + Sized + 'static {
type Rejection: IntoResponse;
fn resolve(
parts: &Parts,
state: &S,
) -> impl std::future::Future<Output = Result<Quota, Self::Rejection>> + Send;
}
impl<S, Q> QuotaSource<S> for Q
where
Q: FromRef<S> + Into<Quota> + Send + Sync + 'static,
S: Sync,
{
type Rejection = Infallible;
fn resolve(
_parts: &Parts,
state: &S,
) -> impl std::future::Future<Output = Result<Quota, Self::Rejection>> + Send {
let quota = Q::from_ref(state).into();
async move { Ok(quota) }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FixedQuota(pub Quota);
impl From<FixedQuota> for Quota {
fn from(value: FixedQuota) -> Self {
value.0
}
}