Fix runtime error on root route

This commit is contained in:
Tristan D. 2025-03-09 20:17:01 +01:00
parent 11b201004a
commit 752727ed40
Signed by: tristan
SSH key fingerprint: SHA256:9oFM1J63hYWJjCnLG6C0fxBS15rwNcWwdQNMOHYKJ/4
8 changed files with 39 additions and 23 deletions

4
Cargo.lock generated
View file

@ -101,7 +101,7 @@ dependencies = [
[[package]] [[package]]
name = "axum-controller" name = "axum-controller"
version = "0.2.0" version = "0.2.1"
dependencies = [ dependencies = [
"axum", "axum",
"axum-controller-macros", "axum-controller-macros",
@ -114,7 +114,7 @@ dependencies = [
[[package]] [[package]]
name = "axum-controller-macros" name = "axum-controller-macros"
version = "0.2.0" version = "0.2.1"
dependencies = [ dependencies = [
"axum", "axum",
"prettyplease", "prettyplease",

View file

@ -12,4 +12,4 @@ keywords = ["axum", "controller", "macro", "routing"]
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
readme = "README.md" readme = "README.md"
repository = "https://git.vlt81.de/vault81/axum-controller" repository = "https://git.vlt81.de/vault81/axum-controller"
version = "0.2.0" version = "0.2.1"

View file

@ -15,7 +15,7 @@ version.workspace = true
prettyplease = "0.2" prettyplease = "0.2"
proc-macro2 = "1" proc-macro2 = "1"
quote = "1" quote = "1"
syn = { version = "2", features = ["parsing"] } syn = { version = "2", features = ["extra-traits", "parsing", "printing"] }
[dev-dependencies] [dev-dependencies]
axum = { version = "0.8", features = [] } axum = { version = "0.8", features = [] }

View file

@ -130,8 +130,9 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream {
.into_iter() .into_iter()
.map(move |route| { .map(move |route| {
quote! { quote! {
.typed_route(#struct_name :: #route) .typed_route(#struct_name :: #route)
}
}
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -139,6 +140,24 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream {
.nest(#route, __nested_router) .nest(#route, __nested_router)
}; };
let nested_router_qoute = quote! {
axum::Router::new()
#nesting_call
};
let unnested_router_quote = quote! {
__nested_router
};
let root_route_expr: syn::ExprLit = syn::parse_quote!("/");
let maybe_nesting_call = if let syn::Expr::Lit(lit) = route {
if lit.eq(&root_route_expr) {
unnested_router_quote
} else {
nested_router_qoute
}
} else {
nested_router_qoute
};
let middleware_calls = args let middleware_calls = args
.middlewares .middlewares
.clone() .clone()
@ -158,8 +177,7 @@ pub fn controller(attr: TokenStream, item: TokenStream) -> TokenStream {
.with_state(state) .with_state(state)
; ;
axum::Router::new() #maybe_nesting_call
#nesting_call
} }
} }
}; };

View file

@ -13,7 +13,7 @@ version.workspace = true
[dependencies] [dependencies]
axum-controller-macros = { path = "../axum-controller-macros", version = "0.2.0" } axum-controller-macros = { path = "../axum-controller-macros", version = "0.2.0" }
axum-typed-routing = { path = "../vendor/axum-typed-routing", version = "0.2.0" } axum-typed-routing = { path = "../vendor/axum-typed-routing", version = "0.2.0", features = [ "aide"] }
[dev-dependencies] [dev-dependencies]
axum = "0.8" axum = "0.8"

View file

@ -90,17 +90,17 @@
''; '';
}; };
packages = { packages = {
default = pkgs.callPackage ./package.nix { }; # default = pkgs.callPackage ./package.nix { };
}; };
}) // { }) // {
hydraJobs = hydraJobs =
let let
system = "x86_64-linux"; system = "x86_64-linux";
packages = self.packages."${system}"; # packages = self.packages."${system}";
devShells = self.devShells."${system}"; devShells = self.devShells."${system}";
in in
{ {
inherit packages devShells; inherit devShells;
}; };
}; };
} }

View file

@ -1,20 +1,20 @@
#![allow(unused)] #![allow(unused)]
use axum::extract::{State, Json}; use axum::extract::{Json, State};
use axum_typed_routing::{TypedRouter, route}; use axum_typed_routing::{route, TypedRouter};
#[route(GET "/item/:id?amount&offset")] #[route(GET "/item/:id?amount&offset")]
async fn item_handler( async fn item_handler(
id: u32, id: u32,
amount: Option<u32>, amount: Option<u32>,
offset: Option<u32>, offset: Option<u32>,
State(state): State<String>, State(state): State<String>,
Json(json): Json<u32>, Json(json): Json<u32>,
) -> String { ) -> String {
todo!("handle request") todo!("handle request")
} }
fn main() { fn main() {
let router: axum::Router = axum::Router::new() let router: axum::Router = axum::Router::new()
.typed_route(item_handler) .typed_route(item_handler)
.with_state("state".to_string()); .with_state("state".to_string());
} }

View file

@ -113,7 +113,6 @@ async fn test_wildcard() {
assert_eq!(response.json::<String>(), "foo/bar"); assert_eq!(response.json::<String>(), "foo/bar");
} }
#[cfg(feature = "aide")] #[cfg(feature = "aide")]
mod aide_support { mod aide_support {
use super::*; use super::*;
@ -231,4 +230,3 @@ mod aide_support {
.unwrap() .unwrap()
} }
} }