Fix runtime error on root route

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

4
Cargo.lock generated
View file

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

View file

@ -12,4 +12,4 @@ keywords = ["axum", "controller", "macro", "routing"]
license = "AGPL-3.0-or-later"
readme = "README.md"
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"
proc-macro2 = "1"
quote = "1"
syn = { version = "2", features = ["parsing"] }
syn = { version = "2", features = ["extra-traits", "parsing", "printing"] }
[dev-dependencies]
axum = { version = "0.8", features = [] }

View file

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

View file

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

View file

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

View file

@ -25,9 +25,9 @@ mod parsing;
/// ```
/// - `METHOD` is the HTTP method, such as `GET`, `POST`, `PUT`, etc.
/// - `PATH` is the path of the route, with optional path parameters and query parameters,
/// e.g. `/item/:id?amount&offset`.
/// e.g. `/item/:id?amount&offset`.
/// - `STATE` is the type of axum-state, passed to the handler. This is optional, and if not
/// specified, the state type is guessed based on the parameters of the handler.
/// specified, the state type is guessed based on the parameters of the handler.
///
/// # Example
/// ```
@ -97,6 +97,7 @@ pub fn route(attr: TokenStream, mut item: TokenStream) -> TokenStream {
/// - `security` is the OpenApi security requirements.
/// - `responses` are the OpenApi responses.
/// - `transform` is a closure that takes an `TransformOperation` and returns an `TransformOperation`.
///
/// This may override the other options. (see the crate `aide` for more information).
///
/// # Example

View file

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

View file

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