2024-10-19 16:15:23 -04:00
|
|
|
use std::collections::VecDeque;
|
2024-10-18 21:05:29 -04:00
|
|
|
use std::ops::AsyncFn;
|
2024-10-19 16:15:23 -04:00
|
|
|
use std::path::PathBuf;
|
2024-10-18 21:05:29 -04:00
|
|
|
|
|
|
|
use tokio::fs::DirEntry;
|
|
|
|
|
|
|
|
use crate::error::CustomError;
|
|
|
|
|
2024-10-19 16:15:23 -04:00
|
|
|
pub(crate) async fn walk_fs<
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
F: AsyncFn(&DirEntry) -> Result<WalkAction, CustomError>,
|
|
|
|
>(
|
2024-10-18 21:05:29 -04:00
|
|
|
root: P,
|
2024-10-19 16:15:23 -04:00
|
|
|
filter: F,
|
|
|
|
) -> Result<Vec<DirEntry>, CustomError> {
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
let mut backlog = VecDeque::new();
|
|
|
|
backlog.push_back(root.into());
|
|
|
|
while let Some(p) = backlog.pop_front() {
|
|
|
|
let mut entries = tokio::fs::read_dir(p).await?;
|
2024-10-18 21:22:39 -04:00
|
|
|
while let Some(entry) = entries.next_entry().await? {
|
2024-10-19 16:15:23 -04:00
|
|
|
let action = filter(&entry).await?;
|
|
|
|
match action {
|
|
|
|
WalkAction::HaltAndCapture => {
|
|
|
|
ret.push(entry);
|
|
|
|
}
|
|
|
|
WalkAction::Halt => {}
|
|
|
|
WalkAction::RecurseAndCapture => {
|
|
|
|
backlog.push_back(entry.path());
|
|
|
|
ret.push(entry);
|
|
|
|
}
|
|
|
|
WalkAction::Recurse => {
|
|
|
|
backlog.push_back(entry.path());
|
|
|
|
}
|
|
|
|
};
|
2024-10-18 21:05:29 -04:00
|
|
|
}
|
2024-10-18 21:22:39 -04:00
|
|
|
}
|
2024-10-19 16:15:23 -04:00
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) enum WalkAction {
|
|
|
|
/// Do not walk down this path but add it to the return list.
|
|
|
|
HaltAndCapture,
|
|
|
|
|
|
|
|
/// Do not walk down this path and do not add it to the return list.
|
|
|
|
Halt,
|
|
|
|
|
|
|
|
/// Walk down this path and add it to the return list.
|
|
|
|
RecurseAndCapture,
|
|
|
|
|
|
|
|
/// Walk down this path but do not add it to the return list.
|
|
|
|
Recurse,
|
2024-10-18 21:05:29 -04:00
|
|
|
}
|