Skip to main content

caelix_core/
interceptor.rs

1use crate::{BoxFuture, HttpResponse, RequestContext, Result};
2
3/// Public Caelix type `Next`.
4pub struct Next<'a> {
5    inner: Box<dyn FnOnce() -> BoxFuture<'a, Result<HttpResponse>> + Send + 'a>,
6}
7
8impl<'a> Next<'a> {
9    /// Runs the `new` public API operation.
10    pub fn new(f: impl FnOnce() -> BoxFuture<'a, Result<HttpResponse>> + Send + 'a) -> Self {
11        Self { inner: Box::new(f) }
12    }
13
14    /// Runs the `run` public API operation.
15    pub fn run(self) -> BoxFuture<'a, Result<HttpResponse>> {
16        (self.inner)()
17    }
18}
19
20/// Public Caelix extension trait `Interceptor`.
21pub trait Interceptor: Send + Sync + 'static {
22    /// Public Caelix API.
23    fn intercept<'a>(
24        &'a self,
25        ctx: &'a RequestContext,
26        next: Next<'a>,
27    ) -> BoxFuture<'a, Result<HttpResponse>>;
28}