From ecc4743fdec43eb578e9c35bb008c68909f1517e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 17:32:19 +0200 Subject: better error handling --- src/config.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 8312d15..f361deb 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,12 @@ use std::{ use serde::{Deserialize, Serialize}; use crate::{ - data::{gameversion::{check_game_versions, VersionLevel}, modloader::Modloader}, db::setup, error::{EType, MLErr, MLE} + data::{ + gameversion::{check_game_versions, VersionLevel}, + modloader::Modloader, + }, + db::setup, + errors::{Error, MLE}, }; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -55,14 +60,17 @@ pub struct Defaults { impl Cfg { /// # Errors pub async fn init(path: Option) -> MLE { - let configfile = match path.clone() { - Some(p) => p, - None => dirs::config_dir() - .ok_or(MLErr::new(EType::Other, "config_dir"))? + let configfile = if let Some(cf) = path.clone() { + cf + } else { + let Some(config_dir) = dirs::config_dir() else { + return Err(Error::SysDirNotFound("config".to_string())); + }; + config_dir .join("modlist") .join("config.toml") .to_string_lossy() - .to_string(), + .to_string() }; if let Some(err) = File::open(&configfile).err() { @@ -78,10 +86,8 @@ impl Cfg { .add_source( config::Environment::with_prefix("MODLIST").separator("_"), ) - .build() - .unwrap() - .try_deserialize() - .unwrap(); + .build()? + .try_deserialize()?; //Check cache if !Path::new(&config.cache).exists() { -- cgit v1.2.3