Function dptree::filter

source · []
pub fn filter<'a, Pred, Input, Output, FnArgs, Descr>(
    pred: Pred
) -> Handler<'a, Input, Output, Descr> where
    Asyncify<Pred>: Injectable<Input, bool, FnArgs> + Send + Sync + 'a,
    Input: Send + 'a,
    Output: 'a,
    Descr: HandlerDescription
Expand description

Constructs a handler that filters input with the predicate pred.

pred has an access to all values that are stored in the input container. If it returns true, a continuation of the handler will be called, otherwise the handler returns ControlFlow::Continue.

Examples found in repository?
examples/web_server.rs (line 28)
27
28
29
30
fn smiles_handler() -> WebHandler {
    dptree::filter(|req: &'static str| req.starts_with("/smile"))
        .endpoint(|| async { "🙃".to_owned() })
}
More examples
Hide additional examples
examples/descr_locations.rs (line 97)
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
fn main() {
    #[rustfmt::skip]
    let some_tree: Handler<DependencyMap, _, _> = dptree::entry()
        .branch(
            dptree::filter(|| true)
                .endpoint(|| async {})
        )
        .branch(
            dptree::filter_async(|| async { true })
                .chain(dptree::filter_map(|| Some(1) ))
                .endpoint(|| async {})
        );

    get_locations(&some_tree).iter().for_each(|(name, loc)| println!("{name: <12} @ {loc}"));
}