tower/filter/
layer.rs

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