diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-31 02:42:52 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-05-31 17:19:20 +0800 |
| commit | 2aa7bda3cb21ce6c052b82e08bcab79a625d04f2 (patch) | |
| tree | f10b89007fc67ca1a948f34abe6869b49296b932 /dev_tools | |
| parent | 3aa409a55e4f2f0ab41b0949cc06eb13c2da4a43 (diff) | |
Enhance code quality across the entire codebase
Diffstat (limited to 'dev_tools')
| -rw-r--r-- | dev_tools/Cargo.toml | 7 | ||||
| -rw-r--r-- | dev_tools/src/bin/ci.rs | 16 | ||||
| -rw-r--r-- | dev_tools/src/bin/docsify-sidebar-gen.rs | 16 | ||||
| -rw-r--r-- | dev_tools/src/bin/refresh-docs.rs | 8 | ||||
| -rw-r--r-- | dev_tools/src/bin/refresh-feature-mod.rs | 2 | ||||
| -rw-r--r-- | dev_tools/src/bin/test-examples.rs | 4 | ||||
| -rw-r--r-- | dev_tools/src/lib.rs | 30 |
7 files changed, 54 insertions, 29 deletions
diff --git a/dev_tools/Cargo.toml b/dev_tools/Cargo.toml index 56f89d3..280a50c 100644 --- a/dev_tools/Cargo.toml +++ b/dev_tools/Cargo.toml @@ -2,6 +2,13 @@ name = "tools" version = "0.1.0" edition = "2024" +authors = ["Weicao-CatilGrass"] +description = "Development tools for mingling" +license = "MIT OR Apache-2.0" +repository = "https://github.com/catilgrass/mingling" +readme = "../README.md" +keywords = ["cli", "development", "tools"] +categories = ["command-line-interface", "development-tools"] [dependencies] just_template = "0.1.3" diff --git a/dev_tools/src/bin/ci.rs b/dev_tools/src/bin/ci.rs index 3169b95..86b930c 100644 --- a/dev_tools/src/bin/ci.rs +++ b/dev_tools/src/bin/ci.rs @@ -1,4 +1,4 @@ -use std::io::Write; +use std::io::Write as _; use std::process::exit; use tools::{cargo_tomls, eprintln_cargo_style, println_cargo_style, run_cmd}; @@ -26,7 +26,7 @@ fn main() { } if let Err(exit_code) = ci() { - restore_workspace().unwrap(); + restore_workspace(needs_commit_temp).unwrap(); exit(exit_code) } @@ -39,7 +39,7 @@ fn main() { let _ = run_cmd!("git status"); if needs_commit_temp { - restore_workspace().unwrap(); + restore_workspace(true).unwrap(); } exit(1) } @@ -47,14 +47,16 @@ fn main() { println_cargo_style!("Done: All check passed!"); if needs_commit_temp { - restore_workspace().unwrap(); + restore_workspace(true).unwrap(); } } -fn restore_workspace() -> Result<(), i32> { +fn restore_workspace(undo_commit: bool) -> Result<(), i32> { run_cmd!("git reset --hard --quiet")?; - run_cmd!("git reset --soft HEAD~1 --quiet")?; - run_cmd!("git reset --quiet")?; + if undo_commit { + run_cmd!("git reset --soft HEAD~1 --quiet")?; + run_cmd!("git reset --quiet")?; + } Ok(()) } diff --git a/dev_tools/src/bin/docsify-sidebar-gen.rs b/dev_tools/src/bin/docsify-sidebar-gen.rs index e0f9370..ccd2641 100644 --- a/dev_tools/src/bin/docsify-sidebar-gen.rs +++ b/dev_tools/src/bin/docsify-sidebar-gen.rs @@ -1,4 +1,5 @@ use std::collections::BTreeMap; +use std::fmt::Write; use std::path::Path; use tools::println_cargo_style; @@ -56,7 +57,7 @@ fn gen_translation_sidebars() { } } -/// Build sidebar content: scan .md files in pages_dir and return a formatted sidebar string +/// Build sidebar content: scan .md files in `pages_dir` and return a formatted sidebar string fn build_sidebar_content(base_dir: &Path, pages_dir: &Path, sidebar_head: &str) -> String { let mut lines = String::from(sidebar_head); @@ -95,7 +96,7 @@ fn build_sidebar_content(base_dir: &Path, pages_dir: &Path, sidebar_head: &str) // Append root-level files for f in &root_files { - lines.push_str(&format!("* [{}]({})\n", f.title, f.link)); + let _ = writeln!(lines, "* [{}]({})", f.title, f.link); } // Append subdirectory groups @@ -104,9 +105,9 @@ fn build_sidebar_content(base_dir: &Path, pages_dir: &Path, sidebar_head: &str) sorted_entries.sort_by(|a, b| a.link.cmp(&b.link)); // Directory header with 2-space indent - lines.push_str(&format!("* {}\n", dir_name)); + let _ = writeln!(lines, "* {dir_name}"); for f in &sorted_entries { - lines.push_str(&format!(" * [{}]({})\n", f.title, f.link)); + let _ = writeln!(lines, " * [{}]({})", f.title, f.link); } } @@ -160,9 +161,10 @@ fn extract_title(path: &Path) -> String { } } // Fallback: use file stem - path.file_stem() - .map(|s| s.to_string_lossy().to_string()) - .unwrap_or_else(|| "Untitled".to_string()) + path.file_stem().map_or_else( + || "Untitled".to_string(), + |s| s.to_string_lossy().to_string(), + ) } fn find_git_repo() -> Option<std::path::PathBuf> { diff --git a/dev_tools/src/bin/refresh-docs.rs b/dev_tools/src/bin/refresh-docs.rs index ffa80a2..71143d1 100644 --- a/dev_tools/src/bin/refresh-docs.rs +++ b/dev_tools/src/bin/refresh-docs.rs @@ -49,7 +49,7 @@ fn gen_example_doc_module() { let template_str = template.to_string(); let template_str = template_str .lines() - .map(|line| line.trim_end()) + .map(str::trim_end) .collect::<Vec<_>>() .join("\n") + "\n"; @@ -91,19 +91,19 @@ impl ExampleContent { let cargo_toml = cargo_toml .lines() - .map(|line| format!("/// {}", line)) + .map(|line| format!("/// {line}")) .collect::<Vec<_>>() .join("\n"); let header = header .lines() - .map(|line| format!("/// {}", line)) + .map(|line| format!("/// {line}")) .collect::<Vec<_>>() .join("\n"); let code = code .lines() - .map(|line| format!("/// {}", line)) + .map(|line| format!("/// {line}")) .collect::<Vec<_>>() .join("\n"); diff --git a/dev_tools/src/bin/refresh-feature-mod.rs b/dev_tools/src/bin/refresh-feature-mod.rs index 6265e15..2255dbc 100644 --- a/dev_tools/src/bin/refresh-feature-mod.rs +++ b/dev_tools/src/bin/refresh-feature-mod.rs @@ -41,7 +41,7 @@ fn gen_feature_module() { let template_str = template.to_string(); let template_str = template_str .lines() - .map(|line| line.trim_end()) + .map(str::trim_end) .collect::<Vec<_>>() .join("\n") + "\n"; diff --git a/dev_tools/src/bin/test-examples.rs b/dev_tools/src/bin/test-examples.rs index 21abaef..ddf5f7c 100644 --- a/dev_tools/src/bin/test-examples.rs +++ b/dev_tools/src/bin/test-examples.rs @@ -75,7 +75,7 @@ fn run_all_tests(config: &TestConfig) -> (usize, usize) { /// Build the example binary, return true on success fn build_example(example_name: &str) -> bool { - let manifest = format!("examples/{}/Cargo.toml", example_name); + let manifest = format!("examples/{example_name}/Cargo.toml"); run_cmd!("cargo build --manifest-path {}", manifest).is_ok() } @@ -132,7 +132,7 @@ fn run_single_test(example_name: &str, test_case: &TestCase) -> bool { fn get_binary_name(example_name: &str) -> String { let base = example_name; if cfg!(target_os = "windows") { - format!("{}.exe", base) + format!("{base}.exe") } else { base.to_string() } diff --git a/dev_tools/src/lib.rs b/dev_tools/src/lib.rs index 1e62a2d..59eed0a 100644 --- a/dev_tools/src/lib.rs +++ b/dev_tools/src/lib.rs @@ -30,6 +30,11 @@ macro_rules! eprintln_cargo_style { }; } +/// Print a message in cargo style format, with bold green prefix. +/// +/// # Panics +/// +/// Panics if the prefix (text before the first `:`) exceeds 12 characters. pub fn println_cargo_style(str: impl Into<String>) { let s = str.into(); let (prefix, content) = if let Some(pos) = s.find(':') { @@ -38,16 +43,15 @@ pub fn println_cargo_style(str: impl Into<String>) { s[pos + 1..].trim_start().to_string(), ) } else { - ("".to_string(), s.trim().to_string()) + (String::new(), s.trim().to_string()) }; - if prefix.len() > 12 { - panic!( - "prefix length exceeds 12: '{}' has length {}", - prefix, - prefix.len() - ); - } + assert!( + prefix.len() <= 12, + "prefix length exceeds 12: '{}' has length {}", + prefix, + prefix.len() + ); let padding = " ".repeat(12 - prefix.len()); @@ -63,6 +67,15 @@ pub fn eprintln_cargo_style(str: impl Into<String>) { println!("{}: {}", "error".bold().bright_red(), str.into()); } +/// Run a shell command and return its exit status. +/// +/// # Panics +/// +/// Panics if the shell command cannot be spawned (e.g. the shell binary is not found). +/// +/// # Errors +/// +/// Returns `Err` with the exit code if the command finishes with a non-zero exit code. pub fn run_cmd(cmd: impl Into<String>) -> Result<(), i32> { let shell = if cfg!(target_os = "windows") { "powershell" @@ -84,6 +97,7 @@ pub fn run_cmd(cmd: impl Into<String>) -> Result<(), i32> { } } +#[must_use] pub fn cargo_tomls() -> Vec<std::path::PathBuf> { let mut cargo_tomls = Vec::new(); let mut dirs = vec![std::path::PathBuf::from(".")]; |
