use std::fmt;
use super::{Limit, into_response::ErrorIntoResponseFn, policy::UnlimitedPolicy};
use crate::Layer;
pub struct LimitLayer<P, F = ()> {
policy: P,
error_into_response: F,
}
impl<P> LimitLayer<P> {
pub const fn new(policy: P) -> Self {
LimitLayer {
policy,
error_into_response: (),
}
}
pub fn with_error_into_response_fn<F>(self, f: F) -> LimitLayer<P, ErrorIntoResponseFn<F>> {
LimitLayer {
policy: self.policy,
error_into_response: ErrorIntoResponseFn(f),
}
}
}
impl LimitLayer<UnlimitedPolicy> {
pub fn unlimited() -> Self {
Self::new(UnlimitedPolicy::default())
}
}
impl<P: fmt::Debug, F: fmt::Debug> std::fmt::Debug for LimitLayer<P, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LimitLayer")
.field("policy", &self.policy)
.field("self.error_into_response", &self.error_into_response)
.finish()
}
}
impl<P, F> Clone for LimitLayer<P, F>
where
P: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Self {
policy: self.policy.clone(),
error_into_response: self.error_into_response.clone(),
}
}
}
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_response: self.error_into_response.clone(),
}
}
fn into_layer(self, service: T) -> Self::Service {
Limit {
inner: service,
policy: self.policy,
error_into_response: self.error_into_response,
}
}
}