//! Repository traversal: finds code files and manifests, groups files by directory. use std::collections::BTreeMap; use std::fs; use std::path::{Path, PathBuf}; const CODE_EXTENSIONS: [&str; 4] = ["rs", "wgsl", "rhai", "sh"]; const COMMENT_CHECKED_EXTENSIONS: [&str; 3] = ["rs", "wgsl", "rhai"]; /// Documentation and local reference clones are not our code, so they are exempt. const SKIPPED_DIRS: [&str; 4] = ["target", "docs", "node_modules", "ref"]; fn has_extension(path: &Path, allowed: &[&str]) -> bool { path.extension() .and_then(|e| e.to_str()) .is_some_and(|e| allowed.contains(&e)) } /// Files whose `//` and `/* */` comments are checked. pub fn is_comment_checked(path: &Path) -> bool { has_extension(path, &COMMENT_CHECKED_EXTENSIONS) } /// A test file: `tests.rs` next to a module, or anything under `tests/` or `benches/`. /// Tests are exempt from the file size limit and from the files-per-directory limit. pub fn is_test_file(path: &Path) -> bool { path.file_name().is_some_and(|name| name == "tests.rs") || path .iter() .any(|component| component == "tests" || component == "benches") } /// Every file under `root` accepted by `keep`, sorted so reports are stable. fn collect(root: &Path, keep: impl Fn(&Path) -> bool) -> Vec { let mut files = Vec::new(); let mut stack = vec![root.to_path_buf()]; while let Some(dir) = stack.pop() { let Ok(entries) = fs::read_dir(&dir) else { continue; }; for entry in entries.flatten() { let path = entry.path(); let name = entry.file_name().to_string_lossy().into_owned(); if path.is_dir() { let skipped = name.starts_with('.') || SKIPPED_DIRS.contains(&name.as_str()); if !skipped { stack.push(path); } } else if keep(&path) { files.push(path); } } } files.sort(); files } /// All code files under `root`. pub fn code_files(root: &Path) -> Vec { collect(root, |path| has_extension(path, &CODE_EXTENSIONS)) } /// All `Cargo.toml` files under `root`. pub fn manifests(root: &Path) -> Vec { collect(root, |path| { path.file_name().is_some_and(|name| name == "Cargo.toml") }) } /// Number of code files directly inside each directory; test files do not count. pub fn files_per_dir(files: &[PathBuf]) -> BTreeMap { let mut counts = BTreeMap::new(); for file in files { if is_test_file(file) { continue; } if let Some(dir) = file.parent() { *counts.entry(dir.to_path_buf()).or_insert(0) += 1; } } counts }