rustio_core/
middleware.rs1use std::future::Future;
9use std::pin::Pin;
10use std::sync::Arc;
11
12use crate::error::Error;
13use crate::http::{Request, Response};
14
15type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
16
17pub type MiddlewareFn =
18 Arc<dyn Fn(Request, Next) -> BoxFuture<Result<Response, Error>> + Send + Sync>;
19
20pub struct Next {
21 inner: Box<dyn FnOnce(Request) -> BoxFuture<Result<Response, Error>> + Send>,
22}
23
24impl Next {
25 pub(crate) fn new(
26 inner: Box<dyn FnOnce(Request) -> BoxFuture<Result<Response, Error>> + Send>,
27 ) -> Self {
28 Self { inner }
29 }
30
31 pub async fn run(self, req: Request) -> Result<Response, Error> {
32 (self.inner)(req).await
33 }
34}