Expand description
An implementation of the chain (tree) of responsibility pattern.
use dptree::prelude::*;
type WebHandler = Endpoint<'static, 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§
- description
- Built-in handler description types.
- di
- An implementation of dependency injection.
- prelude
- Commonly used items.
Macros§
- case
- Filters an enumeration, passing its payload forwards.
- deps
- Constructs
DependencyMap
with a list of dependencies.
Structs§
- Handler
- An instance that receives an input and decides whether to break a chain or pass the value further.
- Type
- A run-time representation of a type. Used only for run-time type inference and checking of handler chains.
Enums§
- Handler
Signature - A run-time handler signature. Used only for run-time type inference and checking of handler chains.
Traits§
- Handler
Description - Handler description.
Functions§
- endpoint
- Constructs a handler that has no further handlers in a chain.
- entry
- Constructs an entry point handler.
- filter
- Constructs a handler that filters input with the predicate
pred
. - filter_
async - The asynchronous version of
filter
. - filter_
async_ with_ description filter_async
with a custom description.- filter_
map - Constructs a handler that optionally passes a value of a new type further.
- filter_
map_ async - The asynchronous version of
filter_map
. - filter_
map_ async_ with_ description filter_map_async
with a custom description.- filter_
map_ with_ description filter_map
with a custom description.- filter_
with_ description filter
with a custom description.- from_fn
- Constructs a handler from a function.
- from_
fn_ with_ description from_fn
with a custom description.- inspect
- Constructs a handler that inspects current state. Like
map
but does not add return value off
to the container. - inspect_
async - The asynchronous version of
inspect
. - inspect_
async_ with_ description inspect_async
with a custom description.- inspect_
with_ description inspect
with a custom description.- map
- Constructs a handler that passes a value of a new type further.
- map_
async - The asynchronous version of
map
. - map_
async_ with_ description map_async
with a custom description.- map_
with_ description map
with a custom description.- type_
check - Performs run-time type checking.
Type Aliases§
- Cont
- A continuation representing the rest of a handler chain.
- Endpoint
- A handler with no further handlers in a chain.
- Handler
Result - An output type produced by a handler.