diff --git a/src/command/build/render.rs b/src/command/build/render.rs index ef0a1aa..eee6257 100644 --- a/src/command/build/render.rs +++ b/src/command/build/render.rs @@ -4,8 +4,8 @@ use std::path::PathBuf; use include_dir::include_dir; use include_dir::Dir; +use tokio::fs::DirEntry; use tokio::task::JoinHandle; -use walkdir::WalkDir; use crate::config::Config; use crate::context::RenderBlogPostPage; @@ -22,6 +22,9 @@ use crate::render::DusterRenderer; use crate::render::RendererIntegration; 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"); @@ -235,7 +238,7 @@ impl SiteRenderer { if !static_files_directory.exists() { 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 { let (path, contents) = entry.await??; 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)>; -fn get_all_files>( +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>( root_dir: P, -) -> Result>, walkdir::Error> { - let files = WalkDir::new(root_dir) +) -> Result>, CustomError> { + let files = walk_fs(root_dir.as_ref(), filter_to_files).await?; + let files_and_content = files .into_iter() - .filter(|e| match e { - Ok(dir_entry) => dir_entry.file_type().is_file(), - Err(_) => true, - }) - .collect::, _>>()?; - let org_files = files - .into_iter() - .map(walkdir::DirEntry::into_path) - .map(|path| tokio::spawn(read_file(path))); - Ok(org_files) + .map(|entry| tokio::spawn(read_file(entry.path()))); + Ok(files_and_content) } async fn read_file(path: PathBuf) -> ReadFileResult { diff --git a/src/command/build/runner.rs b/src/command/build/runner.rs index 20b57b5..51e7fec 100644 --- a/src/command/build/runner.rs +++ b/src/command/build/runner.rs @@ -6,6 +6,7 @@ use std::sync::Mutex; use super::stylesheet::Stylesheet; use super::walk_fs::walk_fs; use super::walk_fs::WalkAction; +use super::walk_fs::WalkFsFilterResult; use crate::cli::parameters::BuildArgs; use crate::command::build::render::SiteRenderer; use crate::config::Config; @@ -62,9 +63,7 @@ async fn get_output_directory(config: &Config) -> Result { Ok(output_directory) } -async fn filter_to_highest_folders_containing_org_files( - entry: &DirEntry, -) -> Result { +async fn filter_to_highest_folders_containing_org_files(entry: &DirEntry) -> WalkFsFilterResult { let file_type = entry.file_type().await?; if !file_type.is_dir() { return Ok(WalkAction::Halt); diff --git a/src/command/build/walk_fs.rs b/src/command/build/walk_fs.rs index 287cbf4..6cf9724 100644 --- a/src/command/build/walk_fs.rs +++ b/src/command/build/walk_fs.rs @@ -6,10 +6,9 @@ use tokio::fs::DirEntry; use crate::error::CustomError; -pub(crate) async fn walk_fs< - P: Into, - F: AsyncFn(&DirEntry) -> Result, ->( +pub(crate) type WalkFsFilterResult = Result; + +pub(crate) async fn walk_fs, F: AsyncFn(&DirEntry) -> WalkFsFilterResult>( root: P, filter: F, ) -> Result, CustomError> {