Added state type to router macro

This commit is contained in:
Martin Berg Alstad 2024-06-28 19:28:57 +02:00
parent 173bbc2ca5
commit e0baff8625

View File

@ -25,9 +25,17 @@ macro_rules! router {
$body $body
} }
}; };
($body:expr; $state:ty) => {
pub(crate) fn router() -> axum::Router<$state> {
$body
}
};
($route:expr, $router:expr) => { ($route:expr, $router:expr) => {
router!(axum::Router::new().nest($route, $router)); 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),* $(,)?) => { ($($method:ident $route:expr => $func:expr),* $(,)?) => {
router!($crate::routes!($($method $route => $func),*)); router!($crate::routes!($($method $route => $func),*));
}; };
@ -66,6 +74,7 @@ macro_rules! join_routes {
#[cfg(all(test, feature = "axum"))] #[cfg(all(test, feature = "axum"))]
mod tests { mod tests {
use axum::extract::State;
use axum::Router; use axum::Router;
async fn index() {} 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<String>| async {}
),
String
);
}
#[test] #[test]
fn test_routes() { fn test_routes() {
let _router: Router<()> = routes!( let _router: Router<()> = routes!(