Better handling of invalid route.rs files

This commit is contained in:
Tristan D. 2025-04-17 11:31:29 +02:00
parent c7d4ba974b
commit b7dd6f9379
Signed by: tristan
SSH key fingerprint: SHA256:9oFM1J63hYWJjCnLG6C0fxBS15rwNcWwdQNMOHYKJ/4
3 changed files with 27 additions and 26 deletions

View file

@ -172,36 +172,37 @@ pub fn route_registrations(
let method_registrations = methods_for_route(route_path); let method_registrations = methods_for_route(route_path);
if method_registrations.is_empty() { if !method_registrations.is_empty() {
return quote! { let first_method = &method_registrations[0];
compile_error!(concat!( let first_method_ident = format_ident!("{}", first_method);
"No routes defined in your route.rs's !\n",
"ensure that at least one `pub async fn` named after an HTTP verb is defined. (e.g. get, post, put, delete)" let mod_path_tokens = generate_mod_path_tokens(&mod_path);
));
let mut builder = quote! {
axum::routing::#first_method_ident(#root_namespace_ident::#mod_path_tokens::#first_method_ident)
}; };
}
let first_method = &method_registrations[0]; for method in &method_registrations[1..] {
let first_method_ident = format_ident!("{}", first_method); let method_ident = format_ident!("{}", method);
let mod_path_tokens = generate_mod_path_tokens(&mod_path); builder = quote! {
#builder.#method_ident(#root_namespace_ident::#mod_path_tokens::#method_ident)
};
}
let mut builder = quote! { let registration = quote! {
axum::routing::#first_method_ident(#root_namespace_ident::#mod_path_tokens::#first_method_ident) router = router.route(#axum_path, #builder);
};
for method in &method_registrations[1..] {
let method_ident = format_ident!("{}", method);
builder = quote! {
#builder.#method_ident(#root_namespace_ident::#mod_path_tokens::#method_ident)
}; };
route_registrations.push(registration);
} }
}
let registration = quote! { if route_registrations.is_empty() {
router = router.route(#axum_path, #builder); return quote! {
compile_error!(concat!(
"No routes defined in your route.rs's !\n",
"Ensure that at least one `pub async fn` named after an HTTP verb is defined. (e.g. get, post, put, delete)"
));
}; };
route_registrations.push(registration);
} }
TokenStream::from_iter(route_registrations) TokenStream::from_iter(route_registrations)

View file

@ -4,14 +4,14 @@ use std::{
}; };
use syn::{ use syn::{
parse::{Parse, ParseStream},
parse_file,
Ident, Ident,
Item, Item,
LitStr, LitStr,
Result, Result,
Token, Token,
Visibility, Visibility,
parse::{Parse, ParseStream},
parse_file,
}; };
#[derive(Debug)] #[derive(Debug)]

View file

@ -1,5 +1,5 @@
error: No routes defined in your route.rs's ! error: No routes defined in your route.rs's !
ensure that at least one `pub async fn` named after an HTTP verb is defined. (e.g. get, post, put, delete) Ensure that at least one `pub async fn` named after an HTTP verb is defined. (e.g. get, post, put, delete)
--> tests/failures/no_routes.rs:6:1 --> tests/failures/no_routes.rs:6:1
| |
6 | #[folder_router("../../../../tests/failures/no_routes", AppState)] 6 | #[folder_router("../../../../tests/failures/no_routes", AppState)]