Skip to main content

waveless_runtime/services/auth/
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 AuthCapture<S>
9where
10    S: Service<RequestCx, Response = ResponseCx, Error = RequestError>,
11{
12    inner: S,
13}
14
15pub struct AuthCaptureLayer;
16
17impl<S> Layer<S> for AuthCaptureLayer
18where
19    S: Service<RequestCx, Response = ResponseCx, Error = RequestError>,
20{
21    type Service = AuthCapture<S>;
22
23    fn layer(&self, inner: S) -> Self::Service {
24        AuthCapture { inner }
25    }
26}
27
28impl<S> Service<RequestCx> for AuthCapture<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                LOGIN_ENDPOINT_ID => LoginSvc.call(cx).await,
53                SIGNUP_ENDPOINT_ID => SignUpSvc.call(cx).await,
54                LOGOUT_ENDPOINT_ID => LogoutSvc.call(cx).await,
55                LOGOUT_ALL_ENDPOINT_ID => LogoutSvc.call(cx).await,
56                _ => inner.call(cx).await,
57            }
58        })
59    }
60}