diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-20 22:21:56 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-20 22:21:56 +0800 |
| commit | ab6be7968b25afb57fc428695693484ad8576718 (patch) | |
| tree | e4af27964f195a18a678844dbe71c0aaa182b5dc /gen/src | |
| parent | 6b22f7b7694fce530f84ba94c65c057450cca626 (diff) | |
Refactor code to use modern Rust idioms and fix clippy lints
Diffstat (limited to 'gen/src')
| -rw-r--r-- | gen/src/env.rs | 33 | ||||
| -rw-r--r-- | gen/src/gen_commands_file.rs | 4 | ||||
| -rw-r--r-- | gen/src/gen_compile_info.rs | 4 | ||||
| -rw-r--r-- | gen/src/gen_completions_entries.rs | 4 | ||||
| -rw-r--r-- | gen/src/gen_iscc_script.rs | 4 | ||||
| -rw-r--r-- | gen/src/gen_mod_files.rs | 6 | ||||
| -rw-r--r-- | gen/src/gen_override_renderer.rs | 17 | ||||
| -rw-r--r-- | gen/src/gen_renderers_file.rs | 4 | ||||
| -rw-r--r-- | gen/src/gen_specific_renderer.rs | 27 | ||||
| -rw-r--r-- | gen/src/resolve_types.rs | 4 |
10 files changed, 51 insertions, 56 deletions
diff --git a/gen/src/env.rs b/gen/src/env.rs index c45830e..c622fba 100644 --- a/gen/src/env.rs +++ b/gen/src/env.rs @@ -5,17 +5,13 @@ pub fn get_author() -> Result<String, Box<dyn std::error::Error>> { let cargo_toml_content = std::fs::read_to_string(cargo_toml_path)?; let cargo_toml: toml::Value = toml::from_str(&cargo_toml_content)?; - if let Some(package) = cargo_toml.get("package") { - if let Some(authors) = package.get("authors") { - if let Some(authors_array) = authors.as_array() { - if let Some(first_author) = authors_array.get(0) { - if let Some(author_str) = first_author.as_str() { + if let Some(package) = cargo_toml.get("package") + && let Some(authors) = package.get("authors") + && let Some(authors_array) = authors.as_array() + && let Some(first_author) = authors_array.first() + && let Some(author_str) = first_author.as_str() { return Ok(author_str.to_string()); } - } - } - } - } Err("Author not found in Cargo.toml".into()) } @@ -25,13 +21,11 @@ pub fn get_site() -> Result<String, Box<dyn std::error::Error>> { let cargo_toml_content = std::fs::read_to_string(cargo_toml_path)?; let cargo_toml: toml::Value = toml::from_str(&cargo_toml_content)?; - if let Some(package) = cargo_toml.get("package") { - if let Some(homepage) = package.get("homepage") { - if let Some(site_str) = homepage.as_str() { + if let Some(package) = cargo_toml.get("package") + && let Some(homepage) = package.get("homepage") + && let Some(site_str) = homepage.as_str() { return Ok(site_str.to_string()); } - } - } Err("Homepage not found in Cargo.toml".into()) } @@ -85,15 +79,12 @@ pub fn get_version() -> String { Err(_) => return "unknown".to_string(), }; - if let Some(workspace) = cargo_toml.get("workspace") { - if let Some(package) = workspace.get("package") { - if let Some(version) = package.get("version") { - if let Some(version_str) = version.as_str() { + if let Some(workspace) = cargo_toml.get("workspace") + && let Some(package) = workspace.get("package") + && let Some(version) = package.get("version") + && let Some(version_str) = version.as_str() { return version_str.to_string(); } - } - } - } "unknown".to_string() } diff --git a/gen/src/gen_commands_file.rs b/gen/src/gen_commands_file.rs index c90e356..1401578 100644 --- a/gen/src/gen_commands_file.rs +++ b/gen/src/gen_commands_file.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::Path; use just_fmt::pascal_case; use just_template::{Template, tmpl, tmpl_param}; @@ -6,7 +6,7 @@ use just_template::{Template, tmpl, tmpl_param}; use crate::constants::{COMMAND_LIST, COMMAND_LIST_TEMPLATE, COMMANDS_PATH, REGISTRY_TOML}; /// Generate registry file from Registry.toml configuration using just_template -pub async fn generate_commands_file(repo_root: &PathBuf) { +pub async fn generate_commands_file(repo_root: &Path) { let template_path = repo_root.join(COMMAND_LIST_TEMPLATE); let output_path = repo_root.join(COMMAND_LIST); let config_path = repo_root.join(REGISTRY_TOML); diff --git a/gen/src/gen_compile_info.rs b/gen/src/gen_compile_info.rs index 02666ac..0d88c2a 100644 --- a/gen/src/gen_compile_info.rs +++ b/gen/src/gen_compile_info.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::Path; use just_template::{Template, tmpl_param}; @@ -8,7 +8,7 @@ use crate::{ }; /// Generate compile info using just_template -pub async fn generate_compile_info(repo_root: &PathBuf) { +pub async fn generate_compile_info(repo_root: &Path) { // Read the template code let template_code = tokio::fs::read_to_string(repo_root.join(COMPILE_INFO_RS_TEMPLATE)) .await diff --git a/gen/src/gen_completions_entries.rs b/gen/src/gen_completions_entries.rs index 7f780b7..1c8ca51 100644 --- a/gen/src/gen_completions_entries.rs +++ b/gen/src/gen_completions_entries.rs @@ -1,10 +1,10 @@ use just_template::{Template, tmpl}; -use std::path::PathBuf; +use std::path::Path; use crate::constants::{COMPLETIONS, COMPLETIONS_PATH, COMPLETIONS_TEMPLATE}; /// Generate completions file from comp directory using just_template -pub async fn generate_completions_file(repo_root: &PathBuf) { +pub async fn generate_completions_file(repo_root: &Path) { let template_path = repo_root.join(COMPLETIONS_TEMPLATE); let output_path = repo_root.join(COMPLETIONS); let comps_dir = repo_root.join(COMPLETIONS_PATH); diff --git a/gen/src/gen_iscc_script.rs b/gen/src/gen_iscc_script.rs index eabaab3..0169065 100644 --- a/gen/src/gen_iscc_script.rs +++ b/gen/src/gen_iscc_script.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::Path; use just_template::{Template, tmpl_param}; @@ -8,7 +8,7 @@ use crate::{ }; /// Generate Inno Setup installer script (Windows only) using just_template -pub async fn generate_installer_script(repo_root: &PathBuf) { +pub async fn generate_installer_script(repo_root: &Path) { let template_path = repo_root.join(SETUP_JV_CLI_ISS_TEMPLATE); let output_path = repo_root.join(SETUP_JV_CLI_ISS); diff --git a/gen/src/gen_mod_files.rs b/gen/src/gen_mod_files.rs index d0b0800..1d20f43 100644 --- a/gen/src/gen_mod_files.rs +++ b/gen/src/gen_mod_files.rs @@ -1,9 +1,9 @@ -use std::path::PathBuf; +use std::path::Path; use crate::constants::REGISTRY_TOML; /// Generate collect files from directory structure -pub async fn generate_collect_files(repo_root: &PathBuf) { +pub async fn generate_collect_files(repo_root: &Path) { // Read and parse the TOML configuration let config_path = repo_root.join(REGISTRY_TOML); let config_content = tokio::fs::read_to_string(&config_path).await.unwrap(); @@ -38,7 +38,7 @@ pub async fn generate_collect_files(repo_root: &PathBuf) { // Get the directory path for this collect type // e.g., for "src/renderers.rs", we want "src/renderers/" - let output_parent = output_path.parent().unwrap_or_else(|| repo_root.as_path()); + let output_parent = output_path.parent().unwrap_or(repo_root); let dir_path = output_parent.join(&dir_name); // Collect all .rs files in the directory (excluding the output file itself) diff --git a/gen/src/gen_override_renderer.rs b/gen/src/gen_override_renderer.rs index cb78e01..7ed0f86 100644 --- a/gen/src/gen_override_renderer.rs +++ b/gen/src/gen_override_renderer.rs @@ -1,4 +1,7 @@ -use std::{collections::HashSet, path::PathBuf}; +use std::{ + collections::HashSet, + path::{Path, PathBuf}, +}; use just_template::{Template, tmpl}; use regex::Regex; @@ -12,7 +15,7 @@ use crate::{ resolve_types::resolve_type_paths, }; -pub async fn generate_override_renderer(repo_root: &PathBuf) { +pub async fn generate_override_renderer(repo_root: &Path) { let template_path = repo_root.join(OVERRIDE_RENDERER_ENTRY_TEMPLATE); let output_path = repo_root.join(OVERRIDE_RENDERER_ENTRY); let all_possible_types = collect_all_possible_types(&PathBuf::from(COMMANDS_PATH)).await; @@ -45,7 +48,7 @@ pub async fn generate_override_renderer(repo_root: &PathBuf) { } /// Generate override renderers list file from Registry.toml configuration using just_template -pub async fn generate_override_renderers_list(repo_root: &PathBuf) { +pub async fn generate_override_renderers_list(repo_root: &Path) { let template_path = repo_root.join(OVERRIDE_RENDERERS_TEMPLATE); let output_path = repo_root.join(OVERRIDE_RENDERERS); let config_path = repo_root.join(REGISTRY_TOML); @@ -103,9 +106,9 @@ pub async fn generate_override_renderers_list(repo_root: &PathBuf) { ); } -pub async fn collect_all_possible_types(dir: &PathBuf) -> HashSet<String> { +pub async fn collect_all_possible_types(dir: &Path) -> HashSet<String> { let mut all_types = HashSet::new(); - let mut dirs_to_visit = vec![dir.clone()]; + let mut dirs_to_visit = vec![dir.to_path_buf()]; while let Some(current_dir) = dirs_to_visit.pop() { let entries_result = fs::read_dir(¤t_dir).await; @@ -130,7 +133,7 @@ pub async fn collect_all_possible_types(dir: &PathBuf) -> HashSet<String> { let path = entry.path(); if path.is_dir() { - dirs_to_visit.push(path); + dirs_to_visit.push(path.to_path_buf()); continue; } @@ -159,7 +162,7 @@ pub async fn collect_all_possible_types(dir: &PathBuf) -> HashSet<String> { all_types } -pub fn get_output_types(code: &String) -> Option<Vec<String>> { +pub fn get_output_types(code: &str) -> Option<Vec<String>> { let mut output_types = Vec::new(); // Find all cmd_output! macros diff --git a/gen/src/gen_renderers_file.rs b/gen/src/gen_renderers_file.rs index 7ac1853..353db71 100644 --- a/gen/src/gen_renderers_file.rs +++ b/gen/src/gen_renderers_file.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::Path; use just_template::{Template, tmpl}; @@ -7,7 +7,7 @@ use crate::constants::{ }; /// Generate renderer list file from Registry.toml configuration using just_template -pub async fn generate_renderers_file(repo_root: &PathBuf) { +pub async fn generate_renderers_file(repo_root: &Path) { let template_path = repo_root.join(OVERRIDE_RENDERER_DISPATCHER_TEMPLATE); let output_path = repo_root.join(OVERRIDE_RENDERER_DISPATCHER); let config_path = repo_root.join(REGISTRY_TOML); diff --git a/gen/src/gen_specific_renderer.rs b/gen/src/gen_specific_renderer.rs index c68dcec..f83f943 100644 --- a/gen/src/gen_specific_renderer.rs +++ b/gen/src/gen_specific_renderer.rs @@ -1,4 +1,7 @@ -use std::{collections::HashMap, path::PathBuf}; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, +}; use just_template::{Template, tmpl}; use regex::Regex; @@ -11,7 +14,7 @@ use crate::{ const RENDERER_TYPE_PREFIX: &str = "crate::"; /// Generate specific renderer matching file using just_template -pub async fn generate_specific_renderer(repo_root: &PathBuf) { +pub async fn generate_specific_renderer(repo_root: &Path) { // Matches: HashMap<RendererTypeFullName, OutputTypeFullName> let mut renderer_matches: HashMap<String, String> = HashMap::new(); @@ -49,14 +52,12 @@ pub async fn generate_specific_renderer(repo_root: &PathBuf) { fn collect_renderers(dir_path: &PathBuf, matches: &mut HashMap<String, String>) { if let Ok(entries) = std::fs::read_dir(dir_path) { - for entry in entries { - if let Ok(entry) = entry { - let path = entry.path(); - if path.is_dir() { - collect_renderers(&path, matches); - } else if path.is_file() && path.extension().map_or(false, |ext| ext == "rs") { - process_rs_file(&path, matches); - } + for entry in entries.flatten() { + let path = entry.path(); + if path.is_dir() { + collect_renderers(&path, matches); + } else if path.is_file() && path.extension().is_some_and(|ext| ext == "rs") { + process_rs_file(&path, matches); } } } @@ -78,14 +79,14 @@ fn process_rs_file(file_path: &PathBuf, matches: &mut HashMap<String, String>) { let full_renderer_type = build_full_renderer_type(file_path, &renderer_type); let full_output_type = resolve_type_paths(&content, vec![output_type]) .unwrap() - .get(0) + .first() .unwrap() .clone(); matches.insert(full_renderer_type, full_output_type); } -fn build_full_renderer_type(file_path: &PathBuf, renderer_type: &str) -> String { +fn build_full_renderer_type(file_path: &Path, renderer_type: &str) -> String { let relative_path = file_path .strip_prefix(std::env::current_dir().unwrap()) .unwrap_or(file_path); @@ -110,7 +111,7 @@ fn build_full_renderer_type(file_path: &PathBuf, renderer_type: &str) -> String format!("{}{}::{}", RENDERER_TYPE_PREFIX, module_path, renderer_type) } -pub fn get_renderer_types(code: &String) -> Option<(String, String)> { +pub fn get_renderer_types(code: &str) -> Option<(String, String)> { let renderer_re = Regex::new(r"#\[result_renderer\(([^)]+)\)\]").unwrap(); let func_re = diff --git a/gen/src/resolve_types.rs b/gen/src/resolve_types.rs index 6079abc..dbb8dbc 100644 --- a/gen/src/resolve_types.rs +++ b/gen/src/resolve_types.rs @@ -1,12 +1,12 @@ use regex::Regex; -pub fn resolve_type_paths(code: &String, type_names: Vec<String>) -> Option<Vec<String>> { +pub fn resolve_type_paths(code: &str, type_names: Vec<String>) -> Option<Vec<String>> { let mut type_mappings = std::collections::HashMap::new(); // Extract all use statements let use_re = Regex::new(r"use\s+([^;]*(?:\{[^}]*\}[^;]*)*);").ok()?; let mut use_statements = Vec::new(); - for cap in use_re.captures_iter(&code) { + for cap in use_re.captures_iter(code) { use_statements.push(cap[1].to_string()); } |
