Skip to main content

waveless_runtime/services/upgrade/
capture.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4use crate::*;
5
6/// TODO: add documentation.
7#[derive(Clone, Constructor, Debug)]
8pub struct UpgradeCapture<S>
9where
10    S: Service<RequestCx, Response = ResponseCx, Error = RequestError>,
11{
12    inner: S,
13}
14
15pub struct UpgradeCaptureLayer;
16
17impl<S> Layer<S> for UpgradeCaptureLayer
18where
19    S: Service<RequestCx, Response = ResponseCx, Error = RequestError>,
20{
21    type Service = UpgradeCapture<S>;
22
23    fn layer(&self, inner: S) -> Self::Service {
24        UpgradeCapture { inner }
25    }
26}
27
28impl<S> Service<RequestCx> for UpgradeCapture<S>
29where
30    S: Service<RequestCx, Response = ResponseCx, Error = RequestError> + Clone + Send + 'static,
31    S::Future: Send + 'static,
32    S::Response: Send + 'static,
33    S::Error: Send + 'static,
34{
35    type Response = S::Response;
36
37    type Error = S::Error;
38
39    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
40
41    fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
42        Poll::Ready(Ok(()))
43    }
44
45    fn call(&mut self, cx: RequestCx) -> Self::Future {
46        let mut inner = self.inner.to_owned();
47
48        Box::pin(async move {
49            let RequestCx { endpoint, .. } = &cx;
50
51            match endpoint.id().as_str() {
52                CONN_UPGRADE_WEBSOCKETS_ENDPOINT_ID => WebSocketsSvc.call(cx).await,
53                _ => inner.call(cx).await,
54            }
55        })
56    }
57}