Fix pedantic clippy lints

This commit is contained in:
Tristan D. 2025-04-15 02:37:58 +02:00
parent 9aa37c1519
commit 1e6649d52a
Signed by: tristan
SSH key fingerprint: SHA256:9oFM1J63hYWJjCnLG6C0fxBS15rwNcWwdQNMOHYKJ/4
6 changed files with 27 additions and 25 deletions

View file

@ -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"

View file

@ -1,5 +1,5 @@
use axum::{extract::Path, response::IntoResponse};
pub async fn get(Path(path): Path<String>) -> impl IntoResponse {
format!("Requested file path: {}", path)
format!("Requested file path: {path}")
}

View file

@ -1,5 +1,5 @@
use axum::{extract::Path, response::IntoResponse};
pub async fn get(Path(id): Path<String>) -> impl IntoResponse {
format!("User ID: {}", id)
format!("User ID: {id}")
}

View file

@ -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

View file

@ -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

View file

@ -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::<String>()
));
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<String>) {
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}");
}
}
}