even more improved errors

This commit is contained in:
2026-03-02 23:23:18 +01:00
parent 2c9206007f
commit 8347fe4ea9

View File

@@ -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<Path>) -> Result<Self, Box<dyn Error>> {
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, ConfigReadWriteError> {
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<ModdedInstance, Box<dyn Error>> {
pub fn load_instance_by_id(&self, id: &str) -> Result<ModdedInstance, ConfigReadWriteError> {
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<Path>) -> Result<Self, Box<dyn Error>> {
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, ConfigReadWriteError> {
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<Path>) -> Result<(), Box<dyn Error>> {
pub fn save_to_file(&self, path: impl AsRef<Path>) -> 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,
}