Skip to main content

rustio_core/
middleware.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::sync::Arc;
4
5use crate::error::Error;
6use crate::http::{Request, Response};
7
8type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
9
10pub type MiddlewareFn =
11    Arc<dyn Fn(Request, Next) -> BoxFuture<Result<Response, Error>> + Send + Sync>;
12
13pub struct Next {
14    inner: Box<dyn FnOnce(Request) -> BoxFuture<Result<Response, Error>> + Send>,
15}
16
17impl Next {
18    pub(crate) fn new(
19        inner: Box<dyn FnOnce(Request) -> BoxFuture<Result<Response, Error>> + Send>,
20    ) -> Self {
21        Self { inner }
22    }
23
24    pub async fn run(self, req: Request) -> Result<Response, Error> {
25        (self.inner)(req).await
26    }
27}