Add a function to recursively list all entries in a directory with tokio.
This commit is contained in:
parent
1b740b1f2f
commit
0da375c529
@ -1,5 +1,6 @@
|
|||||||
mod render;
|
mod render;
|
||||||
mod runner;
|
mod runner;
|
||||||
mod stylesheet;
|
mod stylesheet;
|
||||||
|
mod walk_fs;
|
||||||
|
|
||||||
pub(crate) use runner::build_site;
|
pub(crate) use runner::build_site;
|
||||||
|
26
src/command/build/walk_fs.rs
Normal file
26
src/command/build/walk_fs.rs
Normal 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)
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
|
#![feature(async_closure)]
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
Loading…
Reference in New Issue
Block a user