From cc7d316e588c21de1023d6a76d4ea5e7b893977a Mon Sep 17 00:00:00 2001 From: Melody Horn / boringcactus Date: Sun, 13 Jun 2021 13:39:02 -0600 Subject: macro based url map definition --- src/contrib/admin/site/urls.rs | 9 ++++--- src/urls.rs | 59 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 6 deletions(-) (limited to 'src') 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() + }}; +} -- cgit v1.2.3