diff --git a/src/basic_types.rs b/src/basic_types.rs index 7ca4f30..0d1f309 100644 --- a/src/basic_types.rs +++ b/src/basic_types.rs @@ -3,9 +3,10 @@ use serde::{Deserialize, Serialize}; use std::{ error::Error, fs::{self, read_to_string}, - io::Write, + io::{self, Write}, path::{Path, PathBuf}, }; +use thiserror::Error; use crate::{ conflict_resolver::ConflictSolver, @@ -30,7 +31,7 @@ pub struct RootConfig { } impl RootConfig { - pub fn load_from_file(path: impl AsRef) -> Result> { + pub fn load_from_file(path: impl AsRef) -> Result { trace!( "Loading RootConfig from file: {}", path.as_ref().to_string_lossy() @@ -51,10 +52,10 @@ impl RootConfig { self.mods.iter().find(|e| e.id == id).cloned() } - pub fn load_instance_by_id(&self, id: &str) -> Result> { + pub fn load_instance_by_id(&self, id: &str) -> Result { let conf = self .get_instance_config(id) - .ok_or("ID not found in root config")?; + .ok_or(ConfigReadWriteError::IDNotFound)?; ModdedInstance::load_from_file(&conf.path) } @@ -116,7 +117,7 @@ impl ModdedInstance { } } - pub fn load_from_file(path: impl AsRef) -> Result> { + pub fn load_from_file(path: impl AsRef) -> Result { trace!( "Loading ModdedInstance from file: {}", path.as_ref().to_string_lossy() @@ -128,7 +129,7 @@ impl ModdedInstance { Ok(config) } - pub fn save_to_file(&self, path: impl AsRef) -> Result<(), Box> { + pub fn save_to_file(&self, path: impl AsRef) -> Result<(), ConfigReadWriteError> { trace!( "Saving ModdedInstance to: {}", path.as_ref().to_string_lossy() @@ -290,3 +291,18 @@ impl ModFile { self.internal_priority } } + +#[derive(Error, Debug)] +pub enum ConfigReadWriteError { + #[error("IO failure")] + Io(#[from] io::Error), + + #[error("Failed to deserialize toml")] + Deserialize(#[from] toml::de::Error), + + #[error("Failed to serialize to toml")] + Serialize(#[from] toml::ser::Error), + + #[error("The provided ID could not be found")] + IDNotFound, +}