rama_core/inspect/
request.rs

1use crate::{Context, Service};
2
3/// A special kind of [`Service`] which has access only to the Request,
4/// but not to the Response.
5///
6/// Useful in case you want to explicitly
7/// restrict this acccess or because the Response would
8/// anyway not yet be produced at the point this inspector would be layered.
9pub trait RequestInspector<StateIn, RequestIn>: Send + Sync + 'static {
10    /// The type of error returned by the service.
11    type Error: Send + 'static;
12    type RequestOut: Send + 'static;
13    type StateOut: Clone + Send + Sync + 'static;
14
15    /// Inspect the request, modify it if needed or desired, and return it.
16    fn inspect_request(
17        &self,
18        ctx: Context<StateIn>,
19        req: RequestIn,
20    ) -> impl Future<Output = Result<(Context<Self::StateOut>, Self::RequestOut), Self::Error>> + Send + '_;
21}
22
23impl<S, StateIn, StateOut, RequestIn, RequestOut> RequestInspector<StateIn, RequestIn> for S
24where
25    S: Service<StateIn, RequestIn, Response = (Context<StateOut>, RequestOut)>,
26    RequestIn: Send + 'static,
27    RequestOut: Send + 'static,
28    StateIn: Clone + Send + Sync + 'static,
29    StateOut: Clone + Send + Sync + 'static,
30{
31    type Error = S::Error;
32    type RequestOut = RequestOut;
33    type StateOut = StateOut;
34
35    fn inspect_request(
36        &self,
37        ctx: Context<StateIn>,
38        req: RequestIn,
39    ) -> impl Future<Output = Result<(Context<Self::StateOut>, Self::RequestOut), Self::Error>> + Send + '_
40    {
41        self.serve(ctx, req)
42    }
43}