use std::fs::read_dir;
use std::path::Path;
use std::path::PathBuf;
const RUST_EXTENSION: &str = "rs";
pub struct RustFileFinder;
impl RustFileFinder {
pub fn under(path: &Path) -> Result<Vec<PathBuf>, String> {
if !path.is_dir() {
return Ok(if Self::is_rust(path) {
vec![path.to_path_buf()]
} else {
Vec::new()
});
}
let mut found = Vec::new();
for child in Self::children(path)? {
found.extend(Self::under(&child)?);
}
Ok(found)
}
fn children(path: &Path) -> Result<Vec<PathBuf>, String> {
let mut children: Vec<PathBuf> = read_dir(path)
.map_err(|error| format!("{} could not be read: {error}", path.display()))?
.filter_map(Result::ok)
.map(|entry| entry.path())
.collect();
children.sort();
Ok(children)
}
fn is_rust(path: &Path) -> bool {
path.extension().and_then(|extension| extension.to_str()) == Some(RUST_EXTENSION)
}
}