natter/src/command/build/walk_fs.rs

27 lines
720 B
Rust

use std::ops::AsyncFn;
use std::path::Path;
use tokio::fs::DirEntry;
use crate::error::CustomError;
pub(crate) async fn walk_fs<P: AsRef<Path>, F: AsyncFn(&DirEntry) -> Result<bool, CustomError>>(
root: P,
predicate: F,
) -> Result<Vec<DirEntry>, CustomError> {
let mut ret = Vec::new();
let mut entries = tokio::fs::read_dir(root).await?;
while let Some(entry) = entries.next_entry().await? {
let file_type = entry.file_type().await?;
if file_type.is_dir() {
let child_entries = walk_fs(entry.path(), &predicate).await?;
ret.extend(child_entries);
}
if predicate(&entry).await? {
ret.push(entry);
}
}
Ok(ret)
}