Library for constructing a pipeline of middleware functions that have the option to call the next
in the chain if they so desire before returning back to the previous function call.
This allows for the programmer to perform work on the given context both before and after invoking the
next function in the chain.
Example
use libmw::prelude::*;
fn standalone_func(ctx: &mut dyn PipelineContext, next: Pipeline) -> Result<(), PipelineError> {
println!("before in handler func");
next.invoke(ctx)?;
println!("after in handler func");
Ok(())
}
struct Context {}
impl PipelineContext for Context {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
fn main() {
let mut builder = PipelineBuilder::new();
builder.with(standalone_func);
builder.when(
|_ctx| {
false
},
|builder| {
builder.with(|ctx, next| {
println!("branch handler 1 before");
next.invoke(ctx)?;
println!("branch handler 1 after");
Ok(())
});
builder.with(|ctx, next| {
println!("branch handler 2 before");
next.invoke(ctx)?;
println!("branch handler 2 after");
Ok(())
});
},
);
builder.with(|ctx, next| {
println!("before in closure");
next.invoke(ctx)?;
println!("after in closure");
Ok(())
});
let pipeline = builder.assemble();
let mut context = Context { };
let result = pipeline.invoke(&mut context);
}