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,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;