From 2aa7bda3cb21ce6c052b82e08bcab79a625d04f2 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Sun, 31 May 2026 02:42:52 +0800 Subject: Enhance code quality across the entire codebase --- dev_tools/src/lib.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'dev_tools/src/lib.rs') 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) { 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) { 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) { 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) -> Result<(), i32> { let shell = if cfg!(target_os = "windows") { "powershell" @@ -84,6 +97,7 @@ pub fn run_cmd(cmd: impl Into) -> Result<(), i32> { } } +#[must_use] pub fn cargo_tomls() -> Vec { let mut cargo_tomls = Vec::new(); let mut dirs = vec![std::path::PathBuf::from(".")]; -- cgit