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::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 {
|
||||||
|
@ -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);
|
||||||
|
@ -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> {
|
||||||
|
Loading…
Reference in New Issue
Block a user