Remove a use of WalkDir.
This commit is contained in:
parent
7ddc4011b3
commit
2081d25066
@ -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 {
|
||||
|
@ -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);
|
||||
|
@ -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> {
|
||||
|
Loading…
Reference in New Issue
Block a user