diff --git a/Cargo.toml b/Cargo.toml index c6efafa..6cb5f2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,6 @@ glob = "0.3" anyhow = "1.0.98" axum = "0.8.3" tokio = { version = "1.44.2", features = ["full"] } + +[lints.clippy] +pedantic = "warn" diff --git a/examples/advanced/api/files/[...path]/route.rs b/examples/advanced/api/files/[...path]/route.rs index d88cae6..785166f 100644 --- a/examples/advanced/api/files/[...path]/route.rs +++ b/examples/advanced/api/files/[...path]/route.rs @@ -1,5 +1,5 @@ use axum::{extract::Path, response::IntoResponse}; pub async fn get(Path(path): Path) -> impl IntoResponse { - format!("Requested file path: {}", path) + format!("Requested file path: {path}") } diff --git a/examples/advanced/api/users/[id]/route.rs b/examples/advanced/api/users/[id]/route.rs index 1508802..410f216 100644 --- a/examples/advanced/api/users/[id]/route.rs +++ b/examples/advanced/api/users/[id]/route.rs @@ -1,5 +1,5 @@ use axum::{extract::Path, response::IntoResponse}; pub async fn get(Path(id): Path) -> impl IntoResponse { - format!("User ID: {}", id) + format!("User ID: {id}") } diff --git a/examples/advanced/server.rs b/examples/advanced/server.rs index 6689b53..639203c 100644 --- a/examples/advanced/server.rs +++ b/examples/advanced/server.rs @@ -13,7 +13,7 @@ struct MyFolderRouter(); pub async fn server() -> anyhow::Result<()> { // Create app state let app_state = AppState { - _foo: "".to_string(), + _foo: String::new(), }; // Use the init fn generated above diff --git a/examples/simple/main.rs b/examples/simple/main.rs index 8f1be8e..e53e17b 100644 --- a/examples/simple/main.rs +++ b/examples/simple/main.rs @@ -14,7 +14,7 @@ struct MyFolderRouter(); async fn main() -> anyhow::Result<()> { // Create app state let app_state = AppState { - _foo: "".to_string(), + _foo: String::new(), }; // Use the init fn generated above diff --git a/src/lib.rs b/src/lib.rs index a706c60..f71eada 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,6 +177,7 @@ impl ModuleDir { } } } + /// Creates an Axum router module tree & creation function /// by scanning a directory for `route.rs` files. /// @@ -200,7 +201,7 @@ pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream { let state_type = args.state_type; // Get the project root directory - let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); + let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or("./".to_string()); let base_dir = Path::new(&manifest_dir).join(&base_path); // Collect route files @@ -216,18 +217,15 @@ pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream { }); } - fn replace_special_chars(input: &str) -> String { - input - .chars() - .map(|c| if c.is_alphanumeric() { c } else { '_' }) - .collect() - } - // Build module tree let mut root = ModuleDir::new(&format!( "__folder_router_{}", - replace_special_chars(&base_path) + base_path + .chars() + .map(|c| if c.is_alphanumeric() { c } else { '_' }) + .collect::() )); + for (route_path, rel_path) in &routes { add_to_module_tree(&mut root, rel_path, route_path); } @@ -235,7 +233,10 @@ pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream { // Generate module tree let root_mod_ident = format_ident!("{}", root.name); - let base_path_lit = LitStr::new(base_dir.to_str().unwrap(), proc_macro2::Span::call_site()); + let base_path_lit = LitStr::new( + base_dir.to_str().unwrap_or("./"), + proc_macro2::Span::call_site(), + ); let mod_hierarchy = generate_module_hierarchy(&root); // Generate route registrations @@ -314,15 +315,13 @@ pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream { /// it returns: `vec!["get"]` fn methods_for_route(route_path: &PathBuf) -> Vec<&'static str> { // Read the file content - let file_content = match fs::read_to_string(route_path) { - Ok(content) => content, - Err(_) => return Vec::new(), + let Ok(file_content) = fs::read_to_string(route_path) else { + return Vec::new(); }; // Parse the file content into a syn syntax tree - let file = match parse_file(&file_content) { - Ok(file) => file, - Err(_) => return Vec::new(), + let Ok(file) = parse_file(&file_content) else { + return Vec::new(); }; // Define HTTP methods we're looking for @@ -433,9 +432,9 @@ fn normalize_module_name(name: &str) -> String { if name.starts_with('[') && name.ends_with(']') { let inner = &name[1..name.len() - 1]; if let Some(stripped) = inner.strip_prefix("...") { - format!("___{}", stripped) + format!("___{stripped}") } else { - format!("__{}", inner) + format!("__{inner}") } } else { name.replace(['-', '.'], "_") @@ -469,12 +468,12 @@ fn path_to_module_path(rel_path: &Path) -> (String, Vec) { if segment.starts_with('[') && segment.ends_with(']') { let param = &segment[1..segment.len() - 1]; if let Some(stripped) = param.strip_prefix("...") { - axum_path.push_str(&format!("/{{*{}}}", stripped)); + axum_path = format!("/{{*{stripped}}}"); } else { - axum_path.push_str(&format!("/{{:{}}}", param)); + axum_path = format!("/{{:{param}}}"); } } else { - axum_path.push_str(&format!("/{}", segment)); + axum_path = format!("/{segment}"); } } }