rama_http_backend/server/layer/upgrade/
layer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use super::{service::UpgradeHandler, UpgradeService, Upgraded};
use rama_core::{matcher::Matcher, Context, Layer, Service};
use rama_http_types::Request;
use std::{convert::Infallible, fmt, sync::Arc};

/// UpgradeLayer is a middleware that can be used to upgrade a request.
///
/// See [`UpgradeService`] for more details.
///
/// [`UpgradeService`]: crate::server::layer::upgrade::UpgradeService
pub struct UpgradeLayer<S, O> {
    handlers: Vec<Arc<UpgradeHandler<S, O>>>,
}

impl<S, O> UpgradeLayer<S, O> {
    /// Create a new upgrade layer.
    pub fn new<M, R, H>(matcher: M, responder: R, handler: H) -> Self
    where
        M: Matcher<S, Request>,
        R: Service<S, Request, Response = (O, Context<S>, Request), Error = O> + Clone,
        H: Service<S, Upgraded, Response = (), Error = Infallible> + Clone,
    {
        Self {
            handlers: vec![Arc::new(UpgradeHandler::new(matcher, responder, handler))],
        }
    }

    /// Add an extra upgrade handler to the layer.
    pub fn on<M, R, H>(mut self, matcher: M, responder: R, handler: H) -> Self
    where
        M: Matcher<S, Request>,
        R: Service<S, Request, Response = (O, Context<S>, Request), Error = O> + Clone,
        H: Service<S, Upgraded, Response = (), Error = Infallible> + Clone,
    {
        self.handlers
            .push(Arc::new(UpgradeHandler::new(matcher, responder, handler)));
        self
    }
}

impl<S, O> fmt::Debug for UpgradeLayer<S, O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UpgradeLayer")
            .field("handlers", &self.handlers)
            .finish()
    }
}

impl<S, O> Clone for UpgradeLayer<S, O> {
    fn clone(&self) -> Self {
        Self {
            handlers: self.handlers.clone(),
        }
    }
}

impl<S, State, O> Layer<S> for UpgradeLayer<State, O> {
    type Service = UpgradeService<S, State, O>;

    fn layer(&self, inner: S) -> Self::Service {
        UpgradeService::new(self.handlers.clone(), inner)
    }
}