Crate dptree

Source
Expand description

An implementation of the chain (tree) of responsibility pattern.

[examples/web_server.rs]

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§

HandlerSignature
A run-time handler signature. Used only for run-time type inference and checking of handler chains.

Traits§

HandlerDescription
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 of f 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.
HandlerResult
An output type produced by a handler.