[][src]Macro http_tools::handle_fn

macro_rules! handle_fn {
    ($handler:ident, $filter:expr) => { ... };
}

Help reduce boilerplate of filtering on multiple handlers

The handle_fn macro can be used to simplify the code when testing multiple handlers. The first argument is the identifier of a handler function, and the second argument is an expression that represents a Filter. If the filters succeed then the handler function will be called and passed the argument of the filter and the macro will return the result of the handler function.

Example

#[macro_use] extern crate http_tools;
use http_tools::request::{Extension, Filter};
use http::request::{Request};
use http::response::{Builder, Response};
 
fn handle(req : &Request<()>) -> Response<()> {
    handle_fn!(post_handler, req.filter()
        .filter_path("/")
        .filter_scheme("https")
        .filter_method("POST"));
 
    handle_fn!(get_handler, req.filter()
        .filter_path("/")
        .filter_scheme("https")
        .filter_method("GET"));
 
    Builder::new().status(405).body(()).unwrap()
}

The macro expands into the following:

This example is not tested
// handle_fn($handler, $filter)
match $filter {
    Some(item) => return $handler(item),
    _ => (),
}