Remove a use of WalkDir.

This commit is contained in:
Tom Alexander 2024-10-19 17:14:05 -04:00
parent 7ddc4011b3
commit 2081d25066
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 28 additions and 22 deletions

View File

@ -4,8 +4,8 @@ use std::path::PathBuf;
use include_dir::include_dir; use include_dir::include_dir;
use include_dir::Dir; use include_dir::Dir;
use tokio::fs::DirEntry;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use walkdir::WalkDir;
use crate::config::Config; use crate::config::Config;
use crate::context::RenderBlogPostPage; use crate::context::RenderBlogPostPage;
@ -22,6 +22,9 @@ use crate::render::DusterRenderer;
use crate::render::RendererIntegration; use crate::render::RendererIntegration;
use super::stylesheet::Stylesheet; use super::stylesheet::Stylesheet;
use super::walk_fs::walk_fs;
use super::walk_fs::WalkAction;
use super::walk_fs::WalkFsFilterResult;
static MAIN_TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/default_environment/templates/html"); static MAIN_TEMPLATES: Dir = include_dir!("$CARGO_MANIFEST_DIR/default_environment/templates/html");
@ -235,7 +238,7 @@ impl SiteRenderer {
if !static_files_directory.exists() { if !static_files_directory.exists() {
return Ok(()); return Ok(());
} }
let static_files = get_all_files(&static_files_directory)?; let static_files = get_all_files(&static_files_directory).await?;
for entry in static_files { for entry in static_files {
let (path, contents) = entry.await??; let (path, contents) = entry.await??;
let relative_path = path.strip_prefix(&static_files_directory)?; let relative_path = path.strip_prefix(&static_files_directory)?;
@ -265,21 +268,26 @@ fn build_name_contents_pairs<'a>(
type ReadFileResult = std::io::Result<(PathBuf, Vec<u8>)>; type ReadFileResult = std::io::Result<(PathBuf, Vec<u8>)>;
fn get_all_files<P: AsRef<Path>>( async fn filter_to_files(entry: &DirEntry) -> WalkFsFilterResult {
let file_type = entry.file_type().await?;
if file_type.is_dir() {
return Ok(WalkAction::Recurse);
}
if file_type.is_file() {
return Ok(WalkAction::HaltAndCapture);
}
unreachable!("Unhandled file type.");
}
async fn get_all_files<P: AsRef<Path>>(
root_dir: P, root_dir: P,
) -> Result<impl Iterator<Item = JoinHandle<ReadFileResult>>, walkdir::Error> { ) -> Result<impl Iterator<Item = JoinHandle<ReadFileResult>>, CustomError> {
let files = WalkDir::new(root_dir) let files = walk_fs(root_dir.as_ref(), filter_to_files).await?;
let files_and_content = files
.into_iter() .into_iter()
.filter(|e| match e { .map(|entry| tokio::spawn(read_file(entry.path())));
Ok(dir_entry) => dir_entry.file_type().is_file(), Ok(files_and_content)
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()?;
let org_files = files
.into_iter()
.map(walkdir::DirEntry::into_path)
.map(|path| tokio::spawn(read_file(path)));
Ok(org_files)
} }
async fn read_file(path: PathBuf) -> ReadFileResult { async fn read_file(path: PathBuf) -> ReadFileResult {

View File

@ -6,6 +6,7 @@ use std::sync::Mutex;
use super::stylesheet::Stylesheet; use super::stylesheet::Stylesheet;
use super::walk_fs::walk_fs; use super::walk_fs::walk_fs;
use super::walk_fs::WalkAction; use super::walk_fs::WalkAction;
use super::walk_fs::WalkFsFilterResult;
use crate::cli::parameters::BuildArgs; use crate::cli::parameters::BuildArgs;
use crate::command::build::render::SiteRenderer; use crate::command::build::render::SiteRenderer;
use crate::config::Config; use crate::config::Config;
@ -62,9 +63,7 @@ async fn get_output_directory(config: &Config) -> Result<PathBuf, CustomError> {
Ok(output_directory) Ok(output_directory)
} }
async fn filter_to_highest_folders_containing_org_files( async fn filter_to_highest_folders_containing_org_files(entry: &DirEntry) -> WalkFsFilterResult {
entry: &DirEntry,
) -> Result<WalkAction, CustomError> {
let file_type = entry.file_type().await?; let file_type = entry.file_type().await?;
if !file_type.is_dir() { if !file_type.is_dir() {
return Ok(WalkAction::Halt); return Ok(WalkAction::Halt);

View File

@ -6,10 +6,9 @@ use tokio::fs::DirEntry;
use crate::error::CustomError; use crate::error::CustomError;
pub(crate) async fn walk_fs< pub(crate) type WalkFsFilterResult = Result<WalkAction, CustomError>;
P: Into<PathBuf>,
F: AsyncFn(&DirEntry) -> Result<WalkAction, CustomError>, pub(crate) async fn walk_fs<P: Into<PathBuf>, F: AsyncFn(&DirEntry) -> WalkFsFilterResult>(
>(
root: P, root: P,
filter: F, filter: F,
) -> Result<Vec<DirEntry>, CustomError> { ) -> Result<Vec<DirEntry>, CustomError> {