Add a function to recursively list all entries in a directory with tokio.

This commit is contained in:
Tom Alexander 2024-10-18 21:05:29 -04:00
parent 1b740b1f2f
commit 0da375c529
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 28 additions and 0 deletions

View File

@ -1,5 +1,6 @@
mod render;
mod runner;
mod stylesheet;
mod walk_fs;
pub(crate) use runner::build_site;

View File

@ -0,0 +1,26 @@
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)
}

View File

@ -1,4 +1,5 @@
#![feature(let_chains)]
#![feature(async_closure)]
use std::process::ExitCode;
use clap::Parser;