aboutsummaryrefslogtreecommitdiff
path: root/src/urls.rs
blob: 2668835e0fffbd217a402c74ff9e298bba26bf6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::http::Response;
pub use crate::url_map;

pub use warp::path as warp_path;

pub type UrlMap = warp::filters::BoxedFilter<(Response,)>;

#[doc(hidden)]
#[macro_export]
macro_rules! __url_map_inner {
    (@root => $view:expr, $($rest:tt)*) => {{
        let chain = $crate::__url_map_inner!(@one => $view);
        $crate::__url_map_inner!(chain @rest $($rest)*)
    }};
    (@root $head:tt $(/ $tail:tt)* => $view:expr, $($rest:tt)*) => {{
        let chain = $crate::__url_map_inner!(@one $head $(/ $tail)* => $view);
        $crate::__url_map_inner!(chain @rest $($rest)*)
    }};
    (@root $head:tt $(/ $tail:tt)* $child:expr, $($rest:tt)*) => {{
        let chain = $crate::__url_map_inner!(@one $head $(/ $tail)* $child);
        $crate::__url_map_inner!(chain @rest $($rest)*)
    }};

    ($chain:ident @rest => $view:expr, $($rest:tt)*) => {{
        let chain = $chain
            .or($crate::__url_map_inner!(@one => $view))
            .unify();
        $crate::__url_map_inner!(chain @rest $($rest)*)
    }};
    ($chain:ident @rest $head:tt $(/ $tail:tt)* => $view:expr, $($rest:tt)*) => {{
        let chain = $chain
            .or($crate::__url_map_inner!(@one $head $(/ $tail)* => $view))
            .unify();
        $crate::__url_map_inner!(chain @rest $($rest)*)
    }};
    ($chain:ident @rest $head:tt $(/ $tail:tt)* $child:expr, $($rest:tt)*) => {{
        let chain = $chain
            .or($crate::__url_map_inner!(@one $head $(/ $tail)* $child))
            .unify();
        $crate::__url_map_inner!(chain @rest $($rest)*)
    }};
    ($chain:ident @rest) => { $chain };

    (@one => $view:expr) => {
        $crate::urls::warp_path::end().map($view)
    };
    (@one $head:tt $(/ $tail:tt)* => $view:expr) => {
        $crate::urls::warp_path!($head $(/ $tail)*).map($view)
    };
    (@one $head:tt $(/ $tail:tt)* $child:expr) => {
        $crate::urls::warp_path!($head $(/ $tail)*).and($child)
    };
}

#[macro_export]
macro_rules! url_map {
    ($($body:tt)*) => {{
        use $crate::http::Filter;
        $crate::__url_map_inner!(@root $($body)*)
            .boxed()
    }};
}