pub fn filter_map<'a, Projection, Input, Output, NewType, Args, Descr>(
    proj: Projection
) -> Handler<'a, Input, Output, Descr> where
    Input: Clone,
    Asyncify<Projection>: Injectable<Input, Option<NewType>, Args> + Send + Sync + 'a,
    Input: Insert<NewType> + Send + 'a,
    Output: 'a,
    Descr: HandlerDescription,
    NewType: Send
Expand description

Constructs a handler that optionally passes a value of a new type further.

If the proj function returns Some(v) then v will be added to the container and passed further in a handler chain. If the function returns None, then the handler will return ControlFlow::Continue with the old container.

Examples found in repository?
examples/web_server.rs (lines 33-40)
32
33
34
35
36
37
38
39
40
41
42
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()) })
}
More examples
Hide additional examples
examples/descr_locations.rs (line 102)
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}"));
}