use std::fs;
use std::io;
use std::path::{Path, PathBuf};
pub fn walk(dir: impl AsRef<Path>) -> io::Result<Vec<PathBuf>> {
walk_dir(dir.as_ref())
}
fn walk_dir(dir: &Path) -> io::Result<Vec<PathBuf>> {
let mut files = Vec::new();
let mut pending = vec![dir.to_path_buf()];
while let Some(current) = pending.pop() {
let entries: io::Result<Vec<_>> = fs::read_dir(¤t).and_then(|read| {
read.map(|entry| entry.and_then(|e| e.file_type().map(|kind| (e.path(), kind))))
.collect()
});
for (path, kind) in entries? {
if kind.is_dir() {
pending.push(path);
} else {
files.push(path);
}
}
}
files.sort();
Ok(files)
}
#[cfg(test)]
#[path = "walk.test.rs"]
mod tests;