Skip to main content

a3s_boot/pipeline/
middleware.rs

1use crate::{BootRequest, BootResponse, BoxFuture, Result};
2use std::future::Future;
3
4/// Result of running a middleware.
5pub enum MiddlewareOutcome {
6    Continue(BootRequest),
7    Respond(BootResponse),
8}
9
10impl MiddlewareOutcome {
11    pub fn next(request: BootRequest) -> Self {
12        Self::Continue(request)
13    }
14
15    pub fn response(response: BootResponse) -> Self {
16        Self::Respond(response)
17    }
18}
19
20/// Request middleware that runs before guards, interceptors, pipes, and handlers.
21pub trait Middleware: Send + Sync + 'static {
22    fn handle(&self, request: BootRequest) -> BoxFuture<'static, Result<MiddlewareOutcome>>;
23}
24
25impl<F, Fut> Middleware for F
26where
27    F: Fn(BootRequest) -> Fut + Send + Sync + 'static,
28    Fut: Future<Output = Result<MiddlewareOutcome>> + Send + 'static,
29{
30    fn handle(&self, request: BootRequest) -> BoxFuture<'static, Result<MiddlewareOutcome>> {
31        Box::pin(self(request))
32    }
33}