2024-10-18 21:05:29 -04:00
|
|
|
use std::ops::AsyncFn;
|
|
|
|
use std::path::Path;
|
|
|
|
|
2024-10-18 21:22:39 -04:00
|
|
|
use futures::future::BoxFuture;
|
|
|
|
use futures::FutureExt;
|
2024-10-18 21:05:29 -04:00
|
|
|
use tokio::fs::DirEntry;
|
|
|
|
|
|
|
|
use crate::error::CustomError;
|
|
|
|
|
2024-10-18 21:23:22 -04:00
|
|
|
pub(crate) fn walk_fs<'p, P: AsRef<Path> + std::marker::Send + 'p>(
|
2024-10-18 21:05:29 -04:00
|
|
|
root: P,
|
2024-10-18 21:19:40 -04:00
|
|
|
predicate: fn(&DirEntry) -> Result<bool, CustomError>,
|
2024-10-18 21:23:22 -04:00
|
|
|
) -> BoxFuture<'p, Result<Vec<DirEntry>, CustomError>> {
|
2024-10-18 21:22:39 -04:00
|
|
|
async move {
|
|
|
|
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)? {
|
|
|
|
ret.push(entry);
|
|
|
|
}
|
2024-10-18 21:05:29 -04:00
|
|
|
}
|
|
|
|
|
2024-10-18 21:22:39 -04:00
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
.boxed()
|
2024-10-18 21:05:29 -04:00
|
|
|
}
|