From 185403436b455fd03049ae436b9990f26307908e Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 13 Nov 2025 01:05:56 +0300 Subject: [PATCH] eh: replaced the manual directory traversal with walkdir Signed-off-by: NotAShelf Change-Id: I537cc9b6ab7110c47f9884f1793b07596a6a6964 --- Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 1 + eh/Cargo.toml | 5 +++-- eh/src/util.rs | 30 +++++++++++++++--------------- xtask/Cargo.toml | 4 ++-- 5 files changed, 50 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5893630..c1fcbfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,6 +79,7 @@ dependencies = [ "thiserror", "tracing", "tracing-subscriber", + "walkdir", "yansi", ] @@ -174,6 +175,15 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -298,6 +308,25 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi-util" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" +dependencies = [ + "windows-sys", +] + [[package]] name = "windows-link" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 699f488..e2149c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ regex = "1.12.2" thiserror = "2.0.17" tracing = "0.1.41" tracing-subscriber = "0.3.20" +walkdir = "2.5.0" yansi = "1.0.1" [profile.release] diff --git a/eh/Cargo.toml b/eh/Cargo.toml index bda9168..eba03dd 100644 --- a/eh/Cargo.toml +++ b/eh/Cargo.toml @@ -7,8 +7,8 @@ authors.workspace = true rust-version.workspace = true [lib] -name = "eh" -crate-type = ["lib"] +crate-type = [ "lib" ] +name = "eh" [dependencies] clap.workspace = true @@ -16,4 +16,5 @@ regex.workspace = true thiserror.workspace = true tracing.workspace = true tracing-subscriber.workspace = true +walkdir.workspace = true yansi.workspace = true diff --git a/eh/src/util.rs b/eh/src/util.rs index d7cb5ae..3ecdeec 100644 --- a/eh/src/util.rs +++ b/eh/src/util.rs @@ -5,6 +5,7 @@ use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; use tracing::{info, warn}; +use walkdir::WalkDir; use yansi::Paint; pub trait HashExtractor { @@ -54,21 +55,20 @@ impl NixFileFixer for DefaultNixFileFixer { } fn find_nix_files(&self) -> Result> { - let mut files = Vec::new(); - let mut stack = vec![PathBuf::from(".")]; - while let Some(dir) = stack.pop() { - let entries = fs::read_dir(&dir)?; - for entry in entries.flatten() { - let path = entry.path(); - if path.is_dir() { - stack.push(path); - } else if let Some(ext) = path.extension() - && ext.eq_ignore_ascii_case("nix") - { - files.push(path); - } - } - } + let files: Vec = WalkDir::new(".") + .into_iter() + .filter_map(|entry| entry.ok()) + .filter(|entry| { + entry.file_type().is_file() + && entry + .path() + .extension() + .map(|ext| ext.eq_ignore_ascii_case("nix")) + .unwrap_or(false) + }) + .map(|entry| entry.path().to_path_buf()) + .collect(); + if files.is_empty() { Err(EhError::NoNixFilesFound) } else { diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 54657fb..ece4292 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -9,6 +9,6 @@ publish = false [dependencies] -clap.workspace = true +clap.workspace = true clap_complete.workspace = true -eh = { path = "../eh" } +eh = { path = "../eh" }