diff options
author | Melody Horn / boringcactus <melody@boringcactus.com> | 2021-06-13 13:39:02 -0600 |
---|---|---|
committer | Melody Horn / boringcactus <melody@boringcactus.com> | 2021-06-13 13:39:02 -0600 |
commit | cc7d316e588c21de1023d6a76d4ea5e7b893977a (patch) | |
tree | 47586de87551cf41756fc9f1f02a30b439251a60 /src | |
parent | 6a2f8ab0b2c7028636ddf06a7dd776f5f8b3ace6 (diff) | |
download | tosin-cc7d316e588c21de1023d6a76d4ea5e7b893977a.tar.gz tosin-cc7d316e588c21de1023d6a76d4ea5e7b893977a.zip |
macro based url map definition
Diffstat (limited to 'src')
-rw-r--r-- | src/contrib/admin/site/urls.rs | 9 | ||||
-rw-r--r-- | src/urls.rs | 59 |
2 files changed, 62 insertions, 6 deletions
diff --git a/src/contrib/admin/site/urls.rs b/src/contrib/admin/site/urls.rs index ef23d55..d6537bc 100644 --- a/src/contrib/admin/site/urls.rs +++ b/src/contrib/admin/site/urls.rs @@ -1,7 +1,8 @@ -use crate::http::{Filter, Reply}; -use crate::urls::{UrlMap, path}; +use crate::http::Reply; +use crate::urls::{UrlMap, url_map}; pub fn urls() -> UrlMap { - path::end().map(|| Reply::into_response("bruh")) - .boxed() + url_map! { + => || Reply::into_response("bruh"), + } } diff --git a/src/urls.rs b/src/urls.rs index 45720b6..80eba9e 100644 --- a/src/urls.rs +++ b/src/urls.rs @@ -1,5 +1,60 @@ -pub use warp::path; - use crate::http::Response; +pub use crate::url_map; 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) => { + ::warp::path::end().map($view) + }; + (@one $head:tt $(/ $tail:tt)* => $view:expr) => { + ::warp::path!($head $(/ $tail)*).map($view) + }; + (@one $head:tt $(/ $tail:tt)* $child:expr) => { + ::warp::path!($head $(/ $tail)*).and($child) + }; +} + +#[macro_export] +macro_rules! url_map { + ($($body:tt)*) => {{ + use ::warp::Filter; + $crate::__url_map_inner!(@root $($body)*) + .boxed() + }}; +} |