use super::{Limit, into_output::ErrorIntoOutputFn, policy::UnlimitedPolicy};
use crate::Layer;
#[derive(Debug, Clone)]
pub struct LimitLayer<P, F = ()> {
policy: P,
error_into_output: F,
}
impl<P> LimitLayer<P> {
pub const fn new(policy: P) -> Self {
Self {
policy,
error_into_output: (),
}
}
pub fn with_error_into_response_fn<F>(self, f: F) -> LimitLayer<P, ErrorIntoOutputFn<F>> {
LimitLayer {
policy: self.policy,
error_into_output: ErrorIntoOutputFn(f),
}
}
}
impl LimitLayer<UnlimitedPolicy> {
#[must_use]
pub fn unlimited() -> Self {
Self::new(UnlimitedPolicy::default())
}
}
impl<T, P, F> Layer<T> for LimitLayer<P, F>
where
P: Clone,
F: Clone,
{
type Service = Limit<T, P, F>;
fn layer(&self, service: T) -> Self::Service {
Limit {
inner: service,
policy: self.policy.clone(),
error_into_output: self.error_into_output.clone(),
}
}
fn into_layer(self, service: T) -> Self::Service {
Limit {
inner: service,
policy: self.policy,
error_into_output: self.error_into_output,
}
}
}