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::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<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,
) -> Result<impl Iterator<Item = JoinHandle<ReadFileResult>>, walkdir::Error> {
let files = WalkDir::new(root_dir)
) -> Result<impl Iterator<Item = JoinHandle<ReadFileResult>>, 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::<Result<Vec<_>, _>>()?;
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 {

View File

@ -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<PathBuf, CustomError> {
Ok(output_directory)
}
async fn filter_to_highest_folders_containing_org_files(
entry: &DirEntry,
) -> Result<WalkAction, CustomError> {
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);

View File

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