Skip to main content

tower_request_guard/
layer.rs

1use crate::guard::RequestGuard;
2use crate::service::RequestGuardService;
3use std::sync::Arc;
4use tower_layer::Layer;
5
6/// Tower Layer that applies request validation.
7#[derive(Clone)]
8pub struct RequestGuardLayer {
9    pub(crate) guard: Arc<RequestGuard>,
10}
11
12impl RequestGuardLayer {
13    /// Create a new layer from a [`RequestGuard`].
14    pub fn new(guard: RequestGuard) -> Self {
15        Self {
16            guard: Arc::new(guard),
17        }
18    }
19}
20
21impl<S> Layer<S> for RequestGuardLayer {
22    type Service = RequestGuardService<S>;
23
24    fn layer(&self, inner: S) -> Self::Service {
25        RequestGuardService {
26            inner,
27            guard: self.guard.clone(),
28        }
29    }
30}