use std::path::Path;
use std::path::PathBuf;
use walkdir::WalkDir;
pub struct SourceWalker;
impl SourceWalker {
pub fn walk(root: &Path) -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = WalkDir::new(root)
.into_iter()
.filter_entry(|entry| !Self::is_skipped(root, entry.path()))
.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_file())
.map(|entry| entry.into_path())
.filter(|path| path.extension().is_some_and(|extension| extension == "rs"))
.collect();
paths.sort();
paths
}
fn is_skipped(root: &Path, path: &Path) -> bool {
if path == root {
return false;
}
matches!(
path.file_name().and_then(|name| name.to_str()),
Some("target") | Some(".git")
) || Self::is_another_package(path)
}
fn is_another_package(path: &Path) -> bool {
path.is_dir() && path.join("Cargo.toml").is_file()
}
}