rust_webx_core/pipeline.rs
1//! Pipeline behavior trait: IPipelineBehavior.
2
3use crate::error::Result;
4use std::any::Any;
5use std::future::Future;
6use std::pin::Pin;
7
8/// Type-erased continuation function for pipeline behaviors.
9///
10/// Carries a boxed request to the next behavior (or terminal handler).
11pub type BoxedNextFn = Box<dyn FnOnce(Box<dyn Any + Send>) -> BoxedPipelineFuture + Send>;
12
13/// Boxed future returned by a pipeline behavior step.
14pub type BoxedPipelineFuture = Pin<Box<dyn Future<Output = Result<Box<dyn Any + Send>>> + Send>>;
15
16/// Pipeline behavior that wraps around request handling.
17///
18/// Multiple behaviors form a chain. Each behavior can:
19/// - Inspect or modify the request before passing it on
20/// - Inspect or modify the response after it returns
21/// - Short-circuit and skip the rest of the chain (by not calling `next`)
22///
23/// Behaviors resolve dependencies via constructor injection (registered as
24/// Singleton in the DI container), following the MediatR pattern.
25///
26/// Analogous to MediatR's IPipelineBehavior<TRequest, TResponse>.
27#[async_trait::async_trait]
28pub trait IPipelineBehavior: Send + Sync {
29 async fn handle(
30 &self,
31 req: Box<dyn Any + Send>,
32 next: BoxedNextFn,
33 ) -> Result<Box<dyn Any + Send>>;
34}