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
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
9 changed files with 37 additions and 35 deletions

1
Cargo.lock generated
View File

@ -446,7 +446,6 @@ dependencies = [
"tokio",
"toml",
"url",
"walkdir",
]
[[package]]

View File

@ -32,7 +32,6 @@ serde_json = "1.0.107"
tokio = { version = "1.30.0", default-features = false, features = ["rt", "rt-multi-thread", "fs", "io-util"] }
toml = "0.8.2"
url = "2.5.0"
walkdir = "2.4.0"
# Optimized build for any sort of release.
[profile.release-lto]

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

@ -8,7 +8,6 @@ pub(crate) enum CustomError {
IO(#[allow(dead_code)] std::io::Error),
TomlSerialize(#[allow(dead_code)] toml::ser::Error),
TomlDeserialize(#[allow(dead_code)] toml::de::Error),
WalkDir(#[allow(dead_code)] walkdir::Error),
Tokio(#[allow(dead_code)] tokio::task::JoinError),
Serde(#[allow(dead_code)] serde_json::Error),
Utf8(#[allow(dead_code)] Utf8Error),
@ -49,12 +48,6 @@ impl From<toml::de::Error> for CustomError {
}
}
impl From<walkdir::Error> for CustomError {
fn from(value: walkdir::Error) -> Self {
CustomError::WalkDir(value)
}
}
impl From<tokio::task::JoinError> for CustomError {
fn from(value: tokio::task::JoinError) -> Self {
CustomError::Tokio(value)

View File

@ -3,13 +3,16 @@ use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use tokio::fs::DirEntry;
use tokio::task::JoinHandle;
use walkdir::WalkDir;
use crate::error::CustomError;
use crate::intermediate::blog_post_page::BlogPostPageInput;
use crate::intermediate::registry::Registry;
use crate::intermediate::IntermediateContext;
use crate::walk_fs::walk_fs;
use crate::walk_fs::WalkAction;
use crate::walk_fs::WalkFsFilterResult;
use super::BlogPostPage;
@ -34,7 +37,7 @@ impl BlogPost {
let org_files = {
let mut ret = Vec::new();
let org_files_iter = get_org_files(post_dir)?;
let org_files_iter = get_org_files(post_dir).await?;
for entry in org_files_iter {
ret.push(entry.await??);
}
@ -122,25 +125,33 @@ async fn read_file(path: PathBuf) -> std::io::Result<(PathBuf, String)> {
Ok((path, contents))
}
pub(crate) fn get_org_files<P: AsRef<Path>>(
pub(crate) async fn get_org_files<P: AsRef<Path>>(
root_dir: P,
) -> Result<impl Iterator<Item = JoinHandle<std::io::Result<(PathBuf, String)>>>, walkdir::Error> {
let org_files = WalkDir::new(root_dir)
.into_iter()
.filter(|e| match e {
Ok(dir_entry) => {
dir_entry.file_type().is_file()
&& Path::new(dir_entry.file_name())
.extension()
.map(|ext| ext.to_ascii_lowercase() == "org")
.unwrap_or(false)
}
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()?;
) -> Result<impl Iterator<Item = JoinHandle<std::io::Result<(PathBuf, String)>>>, CustomError> {
let org_files = walk_fs(root_dir.as_ref(), filter_to_org_files).await?;
let org_files = org_files
.into_iter()
.map(walkdir::DirEntry::into_path)
.map(|entry| entry.path())
.map(|path| tokio::spawn(read_file(path)));
Ok(org_files)
}
async fn filter_to_org_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() {
if entry
.path()
.extension()
.map(|ext| ext.eq_ignore_ascii_case("org"))
.unwrap_or(false)
{
return Ok(WalkAction::HaltAndCapture);
}
return Ok(WalkAction::Halt);
}
unreachable!("Unhandled file type.");
}

View File

@ -16,6 +16,7 @@ mod context;
mod error;
mod intermediate;
mod render;
mod walk_fs;
fn main() -> Result<ExitCode, CustomError> {
let rt = tokio::runtime::Runtime::new()?;