Expand description
An implementation of the chain (tree) of responsibility pattern.
use dptree::prelude::*;
type WebHandler = Endpoint<'static, DependencyMap, String>;
#[rustfmt::skip]
#[tokio::main]
async fn main() {
let web_server = dptree::entry()
.branch(smiles_handler())
.branch(sqrt_handler())
.branch(not_found_handler());
assert_eq!(
web_server.dispatch(dptree::deps!["/smile"]).await,
ControlFlow::Break("🙃".to_owned())
);
assert_eq!(
web_server.dispatch(dptree::deps!["/sqrt 16"]).await,
ControlFlow::Break("4".to_owned())
);
assert_eq!(
web_server.dispatch(dptree::deps!["/lol"]).await,
ControlFlow::Break("404 Not Found".to_owned())
);
}
fn smiles_handler() -> WebHandler {
dptree::filter(|req: &'static str| req.starts_with("/smile"))
.endpoint(|| async { "🙃".to_owned() })
}
fn sqrt_handler() -> WebHandler {
dptree::filter_map(|req: &'static str| {
if req.starts_with("/sqrt") {
let (_, n) = req.split_once(' ')?;
n.parse::<f64>().ok()
} else {
None
}
})
.endpoint(|n: f64| async move { format!("{}", n.sqrt()) })
}
fn not_found_handler() -> WebHandler {
dptree::endpoint(|| async { "404 Not Found".to_owned() })
}For a high-level overview, please see README.md.
Modules
Macros
Filters an enumeration, passing its payload forwards.
Constructs DependencyMap with a list of dependencies.
Structs
An instance that receives an input and decides whether to break a chain or pass the value further.
Traits
Handler description.
Functions
Constructs a handler that has no further handlers in a chain.
Constructs an entry point handler.
Constructs a handler that filters input with the predicate pred.
The asynchronous version of filter.
filter_async with a custom description.
Constructs a handler that optionally passes a value of a new type further.
The asynchronous version of filter_map.
filter_map_async with a custom description.
filter_map with a custom description.
filter with a custom description.
Constructs a handler from a function.
from_fn with a custom description.
Constructs a handler that inspects current state. Like map but does not
add return value of f to the container.
The asynchronous version of inspect.
inspect_async with a custom description.
inspect with a custom description.
Constructs a handler that passes a value of a new type further.
map_async with a custom description.
map with a custom description.
Type Definitions
A continuation representing the rest of a handler chain.
A handler with no further handlers in a chain.
An output type produced by a handler.