axum_help/filter/
layer.rs

1use super::{AsyncFilterEx, FilterEx};
2use tower::Layer;
3
4/// Conditionally dispatch requests to the inner service based on a synchronous [predicate](super::Predicate).
5///
6/// This [`Layer`] produces instances of the [`FilterEx`] service.
7#[derive(Debug)]
8pub struct FilterExLayer<U: Clone> {
9    predicate: U,
10}
11
12impl<U: Clone> Clone for FilterExLayer<U> {
13    fn clone(&self) -> Self {
14        Self {
15            predicate: self.predicate.clone(),
16        }
17    }
18}
19
20impl<U: Clone> FilterExLayer<U> {
21    pub fn new(predicate: U) -> Self {
22        Self { predicate }
23    }
24}
25
26impl<U: Clone, S> Layer<S> for FilterExLayer<U> {
27    type Service = FilterEx<S, U>;
28
29    fn layer(&self, inner: S) -> Self::Service {
30        FilterEx::new(inner, self.predicate.clone())
31    }
32}
33
34/// Conditionally dispatch requests to the inner service based on an asynchronous [predicate](super::AsyncPredicate)
35///
36/// This [`Layer`] produces instances of the [`AsyncFilterEx`] service.
37#[derive(Debug)]
38pub struct AsyncFilterExLayer<U> {
39    predicate: U,
40}
41
42impl<U: Clone> Clone for AsyncFilterExLayer<U> {
43    fn clone(&self) -> Self {
44        Self {
45            predicate: self.predicate.clone(),
46        }
47    }
48}
49
50impl<U> AsyncFilterExLayer<U> {
51    pub fn new(predicate: U) -> Self {
52        Self { predicate }
53    }
54}
55
56impl<U: Clone, S> Layer<S> for AsyncFilterExLayer<U> {
57    type Service = AsyncFilterEx<S, U>;
58
59    fn layer(&self, inner: S) -> Self::Service {
60        AsyncFilterEx::new(inner, self.predicate.clone())
61    }
62}