Fixed recursion using a BoxFuture.

This commit is contained in:
Tom Alexander 2024-10-18 21:22:39 -04:00
parent 98fa43575d
commit ae3add9c81
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -1,26 +1,31 @@
use std::ops::AsyncFn; use std::ops::AsyncFn;
use std::path::Path; use std::path::Path;
use futures::future::BoxFuture;
use futures::FutureExt;
use tokio::fs::DirEntry; use tokio::fs::DirEntry;
use crate::error::CustomError; use crate::error::CustomError;
pub(crate) async fn walk_fs<P: AsRef<Path>>( pub(crate) fn walk_fs<P: AsRef<Path> + std::marker::Send + 'static>(
root: P, root: P,
predicate: fn(&DirEntry) -> Result<bool, CustomError>, predicate: fn(&DirEntry) -> Result<bool, CustomError>,
) -> Result<Vec<DirEntry>, CustomError> { ) -> BoxFuture<'static, Result<Vec<DirEntry>, CustomError>> {
let mut ret = Vec::new(); async move {
let mut entries = tokio::fs::read_dir(root).await?; let mut ret = Vec::new();
while let Some(entry) = entries.next_entry().await? { let mut entries = tokio::fs::read_dir(root).await?;
let file_type = entry.file_type().await?; while let Some(entry) = entries.next_entry().await? {
if file_type.is_dir() { let file_type = entry.file_type().await?;
let child_entries = walk_fs(entry.path(), predicate).await?; if file_type.is_dir() {
ret.extend(child_entries); let child_entries = walk_fs(entry.path(), predicate).await?;
ret.extend(child_entries);
}
if predicate(&entry)? {
ret.push(entry);
}
} }
if predicate(&entry)? {
ret.push(entry);
}
}
Ok(ret) Ok(ret)
}
.boxed()
} }