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
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
8 changed files with 95 additions and 61 deletions

View File

@ -1,5 +1,13 @@
use std::path::Path;
use std::path::PathBuf;
use tokio::task::JoinHandle;
use walkdir::WalkDir;
use crate::error::CustomError;
#[derive(Debug)]
struct BlogPost {
pub(crate) struct BlogPost {
id: String,
}
@ -7,11 +15,8 @@ impl BlogPost {
pub(crate) async fn load_blog_post<P: AsRef<Path>, R: AsRef<Path>>(
root_dir: R,
post_dir: P,
) -> Result<BlogPost, Box<dyn std::error::Error>> {
async fn inner(
root_dir: &Path,
post_dir: &Path,
) -> Result<BlogPost, Box<dyn std::error::Error>> {
) -> Result<BlogPost, CustomError> {
async fn inner(root_dir: &Path, post_dir: &Path) -> Result<BlogPost, CustomError> {
let org_files = {
let mut ret = Vec::new();
let org_files_iter = get_org_files(post_dir)?;
@ -23,7 +28,8 @@ impl BlogPost {
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))?;
let parsed = organic::parser::parse_file(contents.as_str(), Some(path))
.map_err(|_| CustomError::Static("Failed to parse org-mode document."))?;
ret.push((path, contents, parsed));
}
ret
@ -37,6 +43,11 @@ impl BlogPost {
}
}
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> {

View File

@ -0,0 +1,2 @@
mod definition;
pub(crate) use definition::BlogPost;

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());
}

View File

@ -3,6 +3,8 @@ use std::path::PathBuf;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
use crate::error::CustomError;
use super::raw::RawConfig;
/// This is the config struct used by most of the code, which is an interpreted version of the RawConfig struct which is the raw disk-representation of the config.
@ -12,8 +14,8 @@ pub(crate) struct Config {
}
impl Config {
pub(crate) fn new<P: AsRef<Path>>(root_dir: P) -> Result<Config, Box<dyn std::error::Error>> {
fn inner(root_dir: &Path) -> Result<Config, Box<dyn std::error::Error>> {
pub(crate) fn new<P: AsRef<Path>>(root_dir: P) -> Result<Config, CustomError> {
fn inner(root_dir: &Path) -> Result<Config, CustomError> {
let file_path = root_dir.join("writer.toml");
Ok(Config {
raw: RawConfig::default(),
@ -23,10 +25,8 @@ impl Config {
inner(root_dir.as_ref())
}
pub(crate) async fn load_from_file<P: Into<PathBuf>>(
path: P,
) -> Result<Config, Box<dyn std::error::Error>> {
async fn inner(path: PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
pub(crate) async fn load_from_file<P: Into<PathBuf>>(path: P) -> Result<Config, CustomError> {
async fn inner(path: PathBuf) -> Result<Config, CustomError> {
let contents = tokio::fs::read_to_string(&path).await?;
let parsed_contents: RawConfig = toml::from_str(contents.as_str())?;
Ok(Config {
@ -37,7 +37,7 @@ impl Config {
inner(path.into()).await
}
pub(crate) async fn write_to_disk(&self) -> Result<(), Box<dyn std::error::Error>> {
pub(crate) async fn write_to_disk(&self) -> Result<(), CustomError> {
let mut config_file = File::create(&self.config_path).await?;
config_file
.write_all(toml::to_string(&self.raw)?.as_bytes())
@ -51,4 +51,8 @@ impl Config {
.parent()
.expect("Config file must exist inside a directory.")
}
pub(crate) fn get_posts_directory(&self) -> PathBuf {
self.get_root_directory().join("posts")
}
}

45
src/error/error.rs Normal file
View File

@ -0,0 +1,45 @@
#[derive(Debug)]
pub(crate) enum CustomError {
Static(&'static str),
IO(std::io::Error),
TomlSerialize(toml::ser::Error),
TomlDeserialize(toml::de::Error),
WalkDir(walkdir::Error),
Tokio(tokio::task::JoinError),
}
impl From<std::io::Error> for CustomError {
fn from(value: std::io::Error) -> Self {
CustomError::IO(value)
}
}
impl From<&'static str> for CustomError {
fn from(value: &'static str) -> Self {
CustomError::Static(value)
}
}
impl From<toml::ser::Error> for CustomError {
fn from(value: toml::ser::Error) -> Self {
CustomError::TomlSerialize(value)
}
}
impl From<toml::de::Error> for CustomError {
fn from(value: toml::de::Error) -> Self {
CustomError::TomlDeserialize(value)
}
}
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)
}
}

3
src/error/mod.rs Normal file
View File

@ -0,0 +1,3 @@
#[allow(clippy::module_inception)]
mod error;
pub(crate) use error::CustomError;

View File

@ -6,12 +6,14 @@ use self::cli::parameters::Cli;
use self::cli::parameters::Commands;
use self::command::build::build_site;
use self::command::init::init_writer_folder;
use self::error::CustomError;
mod blog_post;
mod cli;
mod command;
mod config;
mod error;
fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
fn main() -> Result<ExitCode, CustomError> {
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(async {
let main_body_result = main_body().await;
@ -19,7 +21,7 @@ fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
})
}
async fn main_body() -> Result<ExitCode, Box<dyn std::error::Error>> {
async fn main_body() -> Result<ExitCode, CustomError> {
let args = Cli::parse();
match args.command {
Commands::Init(args) => {