rouille/find_route.rs
1// Copyright (c) 2016 The Rouille developers
2// Licensed under the Apache License, Version 2.0
3// <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
6// at your option. All files in the project carrying such
7// notice may not be copied, modified, or distributed except
8// according to those terms.
9
10/// Evaluates each parameter until one of them evaluates to something else
11/// than a 404 error code.
12///
13/// This macro supposes that each route returns a `Response`.
14///
15/// # Example
16///
17/// ```
18/// # #[macro_use] extern crate rouille;
19/// # fn main() {
20/// use rouille::{Request, Response};
21///
22/// fn handle_request_a(_: &Request) -> Response {
23/// # panic!()
24/// // ...
25/// }
26///
27/// fn handle_request_b(_: &Request) -> Response {
28/// # panic!()
29/// // ...
30/// }
31///
32/// fn handle_request_c(_: &Request) -> Response {
33/// # panic!()
34/// // ...
35/// }
36///
37/// # let request = return;
38/// // First calls `handle_request_a`. If it returns anything else than a 404 error, then the
39/// // `response` will contain the return value.
40/// //
41/// // Instead if `handle_request_a` returned a 404 error, then `handle_request_b` is tried.
42/// // If `handle_request_b` also returns a 404 error, then `handle_request_c` is tried.
43/// let response = find_route!(
44/// handle_request_a(request),
45/// handle_request_b(request),
46/// handle_request_c(request)
47/// );
48/// # }
49/// ```
50///
51#[macro_export]
52macro_rules! find_route {
53 ($($handler:expr),+) => ({
54 let mut response = $crate::Response::empty_404();
55 $(
56 if response.status_code == 404 {
57 response = $handler;
58 }
59 )+
60 response
61 });
62}