use crate::args; use std::path::{Path, PathBuf}; pub fn validate_and_prepare_dir_output(cfg: &args::Config) -> Result { if !cfg.output_provided { return Err("when input is a directory, -o/--output must be provided".into()); } let out_root = PathBuf::from(&cfg.output); if out_root.exists() && !out_root.is_dir() { return Err(format!("output must be a directory, got file: {}", out_root.display())); } std::fs::create_dir_all(&out_root).map_err(|e| format!("failed to create output dir {}: {e}", out_root.display()))?; Ok(out_root) } pub fn walk_files_recursive(root: &Path) -> Result, String> { let mut out = Vec::new(); let mut stack = vec![root.to_path_buf()]; while let Some(dir) = stack.pop() { let rd = std::fs::read_dir(&dir).map_err(|e| format!("failed to read dir {}: {e}", dir.display()))?; for entry in rd { let entry = entry.map_err(|e| format!("failed to read dir entry: {e}"))?; let ty = entry.file_type().map_err(|e| format!("failed to stat {}: {e}", entry.path().display()))?; if ty.is_dir() { stack.push(entry.path()); } else if ty.is_file() { out.push(entry.path()); } } } Ok(out) }