Remove the last use of walkdir.

This commit is contained in:
Tom Alexander
2024-10-19 17:26:37 -04:00
parent 2081d25066
commit 1c3e2ca4d9
9 changed files with 37 additions and 35 deletions

View File

@@ -1,6 +1,5 @@
mod render;
mod runner;
mod stylesheet;
mod walk_fs;
pub(crate) use runner::build_site;

View File

@@ -20,11 +20,11 @@ use crate::intermediate::BlogPost;
use crate::intermediate::IPage;
use crate::render::DusterRenderer;
use crate::render::RendererIntegration;
use crate::walk_fs::walk_fs;
use crate::walk_fs::WalkAction;
use crate::walk_fs::WalkFsFilterResult;
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");

View File

@@ -4,9 +4,6 @@ use std::sync::Arc;
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;
@@ -17,6 +14,9 @@ use crate::intermediate::IPage;
use crate::intermediate::IntermediateContext;
use crate::intermediate::PageInput;
use crate::intermediate::Registry;
use crate::walk_fs::walk_fs;
use crate::walk_fs::WalkAction;
use crate::walk_fs::WalkFsFilterResult;
use include_dir::include_dir;
use include_dir::Dir;
use tokio::fs::DirEntry;
@@ -125,7 +125,7 @@ async fn load_pages(config: &Config) -> Result<Vec<IPage>, CustomError> {
if !pages_source.exists() {
return Ok(Vec::new());
}
let page_files = get_org_files(&pages_source)?;
let page_files = get_org_files(&pages_source).await?;
let org_files = {
let mut ret = Vec::new();
for page in page_files {

View File

@@ -1,53 +0,0 @@
use std::collections::VecDeque;
use std::ops::AsyncFn;
use std::path::PathBuf;
use tokio::fs::DirEntry;
use crate::error::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> {
let mut ret = Vec::new();
let mut backlog = VecDeque::new();
backlog.push_back(root.into());
while let Some(p) = backlog.pop_front() {
let mut entries = tokio::fs::read_dir(p).await?;
while let Some(entry) = entries.next_entry().await? {
let action = filter(&entry).await?;
match action {
WalkAction::HaltAndCapture => {
ret.push(entry);
}
WalkAction::Halt => {}
WalkAction::RecurseAndCapture => {
backlog.push_back(entry.path());
ret.push(entry);
}
WalkAction::Recurse => {
backlog.push_back(entry.path());
}
};
}
}
Ok(ret)
}
pub(crate) enum WalkAction {
/// Do not walk down this path but add it to the return list.
HaltAndCapture,
/// Do not walk down this path and do not add it to the return list.
Halt,
/// Walk down this path and add it to the return list.
#[allow(dead_code)]
RecurseAndCapture,
/// Walk down this path but do not add it to the return list.
Recurse,
}