use std::fs::{self, DirEntry};
use std::io;
use std::path::Path;
pub fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> io::Result<()> {
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, cb)?;
} else {
cb(&entry);
}
}
}
Ok(())
}
pub fn get_all_paths(dir: &Path) -> Result<Vec<std::path::PathBuf>, String> {
let mut all_paths: Vec<std::path::PathBuf> = vec![];
let dir_entries = match fs::read_dir(dir) {
Ok(entries) => entries,
Err(err) => return Ok(vec![]),
};
for entry in dir_entries {
let entry_path = entry.unwrap().path();
if entry_path.is_dir() {
let mut dir_paths: Vec<std::path::PathBuf> = get_all_paths(&entry_path)?;
all_paths.append(&mut dir_paths);
} else {
all_paths.push(entry_path);
}
}
Ok(all_paths)
}