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

@@ -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")
}
}