Switch to using CustomError because a boxed StdError is not Send.

This commit is contained in:
Tom Alexander
2023-10-22 13:44:03 -04:00
parent d8fc49797e
commit 2f0f3ab346
8 changed files with 95 additions and 61 deletions

View File

@@ -1,57 +1,23 @@
use std::path::Path;
use std::path::PathBuf;
use crate::blog_post::BlogPost;
use crate::cli::parameters::BuildArgs;
use crate::config::Config;
use tokio::task::JoinHandle;
use crate::error::CustomError;
use walkdir::WalkDir;
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> {
let config = Config::load_from_file(args.config).await?;
let org_files = {
let mut ret = Vec::new();
let org_files_iter = get_org_files(config.get_root_directory())?;
for entry in org_files_iter {
ret.push(entry.await??);
}
ret
};
let parsed_org_files = {
let mut ret = Vec::new();
for (path, contents) in org_files.iter() {
let parsed = organic::parser::parse_file(contents.as_str(), Some(path))?;
ret.push((path, contents, parsed));
}
ret
};
Ok(())
}
async fn read_file(path: PathBuf) -> std::io::Result<(PathBuf, String)> {
let contents = tokio::fs::read_to_string(&path).await?;
Ok((path, contents))
}
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)
let root_directory = config.get_root_directory().to_owned();
let post_directories = WalkDir::new(config.get_posts_directory())
.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)
}
Ok(entry) if entry.depth() == 1 && entry.file_type().is_dir() => true,
Ok(_) => false,
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()?;
let org_files = org_files
let load_jobs = post_directories
.into_iter()
.map(walkdir::DirEntry::into_path)
.map(|path| tokio::spawn(read_file(path)));
Ok(org_files)
.map(|path| tokio::spawn(BlogPost::load_blog_post(root_directory.clone(), path)));
Ok(())
}

View File

@@ -1,7 +1,8 @@
use crate::cli::parameters::InitArgs;
use crate::config::Config;
use crate::error::CustomError;
pub(crate) async fn init_writer_folder(args: InitArgs) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn init_writer_folder(args: InitArgs) -> Result<(), CustomError> {
if args.path.exists() && !args.path.is_dir() {
return Err("The supplied path exists but is not a directory. Aborting.".into());
}