From cf634d8a8b60de8ee041c6b47a9a31800cfdc69f Mon Sep 17 00:00:00 2001 From: peoplemiau1 Date: Tue, 26 May 2026 15:03:40 +0300 Subject: [PATCH] feat: add fmt command --- Cargo.lock | 10 -------- Cargo.toml | 2 +- rust-toolchain.toml | 2 ++ src/cli/fmt.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/cli/help.rs | 1 + src/cli/mod.rs | 1 + src/main.rs | 2 ++ 7 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 rust-toolchain.toml create mode 100644 src/cli/fmt.rs diff --git a/Cargo.lock b/Cargo.lock index 78b8f24..6efead2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -777,15 +777,6 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" -[[package]] -name = "openssl-src" -version = "300.6.0+3.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8e8cbfd3a4a8c8f089147fd7aaa33cf8c7450c4d09f8f80698a0cf093abeff4" -dependencies = [ - "cc", -] - [[package]] name = "openssl-sys" version = "0.9.115" @@ -794,7 +785,6 @@ checksum = "158fe5b292746440aa6e7a7e690e55aeb72d41505e2804c23c6973ad0e9c9781" dependencies = [ "cc", "libc", - "openssl-src", "pkg-config", "vcpkg", ] diff --git a/Cargo.toml b/Cargo.toml index 74158bb..d43e50e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ serde_json = "1" ctrlc = "3" [target.'cfg(not(windows))'.dependencies] -openssl = { version = "0.10", features = ["vendored"] } +openssl = { version = "0.10" } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" diff --git a/src/cli/fmt.rs b/src/cli/fmt.rs new file mode 100644 index 0000000..1c1ef64 --- /dev/null +++ b/src/cli/fmt.rs @@ -0,0 +1,56 @@ +use crate::utils::log::error; +use crate::utils::text::{colored, BOLD_GREEN}; +use glob::glob; +use std::process::Command; + +pub fn fmt(_args: &[String]) -> i32 { + let patterns = [ + "src/**/*.c", + "src/**/*.cpp", + "src/**/*.h", + "src/**/*.hpp", + "tests/**/*.c", + "tests/**/*.cpp", + "tests/**/*.h", + "tests/**/*.hpp", + ]; + + let mut files = Vec::new(); + for pattern in patterns { + if let Ok(paths) = glob(pattern) { + for path in paths.flatten() { + if let Some(s) = path.to_str() { + files.push(s.to_string()); + } + } + } + } + + if files.is_empty() { + println!(" {} No files to format", colored("Format", BOLD_GREEN)); + return 0; + } + + println!( + " {} {} files", + colored("Formatting", BOLD_GREEN), + files.len() + ); + + let status = Command::new("clang-format").arg("-i").args(&files).status(); + + match status { + Ok(s) if s.success() => { + println!(" {} successful", colored("Format", BOLD_GREEN)); + 0 + } + Ok(s) => { + error(&format!("clang-format failed with status: {}", s)); + 1 + } + Err(e) => { + error(&format!("Failed to execute clang-format: {}", e)); + 1 + } + } +} diff --git a/src/cli/help.rs b/src/cli/help.rs index a3254d8..0829ae1 100644 --- a/src/cli/help.rs +++ b/src/cli/help.rs @@ -14,6 +14,7 @@ pub fn help() -> i32 { println!(" run [--profile] Build and run the project (default: --debug)"); println!(" tree Display the dependency tree"); println!(" test Run project tests (alias: tests)"); + println!(" fmt Format all C/C++ source files using clang-format"); println!(" clean Remove the target directory"); println!(" gen Generate IDE integration files"); printc("FLAGS:", BOLD_GREEN); diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 5ed19b4..501f846 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -3,6 +3,7 @@ pub mod build; pub mod clean; pub mod flag_update; pub mod flags; +pub mod fmt; pub mod r#gen; pub mod help; pub mod init; diff --git a/src/main.rs b/src/main.rs index 805763b..2fec5d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#![feature(let_chains)] use crate::utils::log::error; mod cli; mod config; @@ -24,6 +25,7 @@ fn main() { "run" => cli::run::run(rest), "tree" => cli::tree::tree(rest), "test" | "tests" => cli::test::test(rest), + "fmt" => cli::fmt::fmt(rest), "clean" => cli::clean::clean(rest), "gen" => cli::r#gen::r#gen(rest), "--version" => {