diff --git a/Cargo.lock b/Cargo.lock index 34c66d7..c1bd77d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,7 +446,6 @@ dependencies = [ "tokio", "toml", "url", - "walkdir", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 9010954..47d1263 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/command/build/mod.rs b/src/command/build/mod.rs index 6b8fd66..b1b161e 100644 --- a/src/command/build/mod.rs +++ b/src/command/build/mod.rs @@ -1,6 +1,5 @@ mod render; mod runner; mod stylesheet; -mod walk_fs; pub(crate) use runner::build_site; diff --git a/src/command/build/render.rs b/src/command/build/render.rs index eee6257..6ffb7d8 100644 --- a/src/command/build/render.rs +++ b/src/command/build/render.rs @@ -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"); diff --git a/src/command/build/runner.rs b/src/command/build/runner.rs index 51e7fec..a4aa01b 100644 --- a/src/command/build/runner.rs +++ b/src/command/build/runner.rs @@ -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, 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 { diff --git a/src/error/error.rs b/src/error/error.rs index 73d08ef..a11e69f 100644 --- a/src/error/error.rs +++ b/src/error/error.rs @@ -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 for CustomError { } } -impl From for CustomError { - fn from(value: walkdir::Error) -> Self { - CustomError::WalkDir(value) - } -} - impl From for CustomError { fn from(value: tokio::task::JoinError) -> Self { CustomError::Tokio(value) diff --git a/src/intermediate/blog_post.rs b/src/intermediate/blog_post.rs index 01f8d08..6f48864 100644 --- a/src/intermediate/blog_post.rs +++ b/src/intermediate/blog_post.rs @@ -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>( +pub(crate) async fn get_org_files>( root_dir: P, -) -> Result>>, 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>>, 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."); +} diff --git a/src/main.rs b/src/main.rs index 09fb643..bbe13c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ mod context; mod error; mod intermediate; mod render; +mod walk_fs; fn main() -> Result { let rt = tokio::runtime::Runtime::new()?; diff --git a/src/command/build/walk_fs.rs b/src/walk_fs.rs similarity index 100% rename from src/command/build/walk_fs.rs rename to src/walk_fs.rs