From e0baff8625f4d2d74f35293dd6e6a344d6a7a503 Mon Sep 17 00:00:00 2001 From: Martin Berg Alstad Date: Fri, 28 Jun 2024 19:28:57 +0200 Subject: [PATCH] Added state type to router macro --- src/axum/router.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/axum/router.rs b/src/axum/router.rs index 75f7b7e..c719244 100644 --- a/src/axum/router.rs +++ b/src/axum/router.rs @@ -25,9 +25,17 @@ macro_rules! router { $body } }; + ($body:expr; $state:ty) => { + pub(crate) fn router() -> axum::Router<$state> { + $body + } + }; ($route:expr, $router:expr) => { router!(axum::Router::new().nest($route, $router)); }; + ($route:expr, $router:expr, $state:ty) => { + router!(axum::Router::new().nest($route, $router); $state); + }; ($($method:ident $route:expr => $func:expr),* $(,)?) => { router!($crate::routes!($($method $route => $func),*)); }; @@ -66,6 +74,7 @@ macro_rules! join_routes { #[cfg(all(test, feature = "axum"))] mod tests { + use axum::extract::State; use axum::Router; async fn index() {} @@ -94,6 +103,18 @@ mod tests { ); } + #[test] + fn test_nested_router_with_state() { + router!( + "/simplify", + routes!( + get "/:exp" => || async {}, + get "/table/:exp" => |_state: State| async {} + ), + String + ); + } + #[test] fn test_routes() { let _router: Router<()> = routes!(