summaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
committerfxqnlr <[email protected]>2024-09-04 17:32:19 +0200
commitecc4743fdec43eb578e9c35bb008c68909f1517e (patch)
tree73916114bc2bff8c72f759f5aae11a95d4dede22 /src/config.rs
parent11e64fc7560de3cd0def718edf68c31e3dc8be72 (diff)
downloadmodlist-refactor.tar
modlist-refactor.tar.gz
modlist-refactor.zip
better error handlingrefactor
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs26
1 files changed, 16 insertions, 10 deletions
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::{
7use serde::{Deserialize, Serialize}; 7use serde::{Deserialize, Serialize};
8 8
9use crate::{ 9use crate::{
10 data::{gameversion::{check_game_versions, VersionLevel}, modloader::Modloader}, db::setup, error::{EType, MLErr, MLE} 10 data::{
11 gameversion::{check_game_versions, VersionLevel},
12 modloader::Modloader,
13 },
14 db::setup,
15 errors::{Error, MLE},
11}; 16};
12 17
13#[derive(Debug, Clone, Serialize, Deserialize)] 18#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -55,14 +60,17 @@ pub struct Defaults {
55impl Cfg { 60impl Cfg {
56 /// # Errors 61 /// # Errors
57 pub async fn init(path: Option<String>) -> MLE<Self> { 62 pub async fn init(path: Option<String>) -> MLE<Self> {
58 let configfile = match path.clone() { 63 let configfile = if let Some(cf) = path.clone() {
59 Some(p) => p, 64 cf
60 None => dirs::config_dir() 65 } else {
61 .ok_or(MLErr::new(EType::Other, "config_dir"))? 66 let Some(config_dir) = dirs::config_dir() else {
67 return Err(Error::SysDirNotFound("config".to_string()));
68 };
69 config_dir
62 .join("modlist") 70 .join("modlist")
63 .join("config.toml") 71 .join("config.toml")
64 .to_string_lossy() 72 .to_string_lossy()
65 .to_string(), 73 .to_string()
66 }; 74 };
67 75
68 if let Some(err) = File::open(&configfile).err() { 76 if let Some(err) = File::open(&configfile).err() {
@@ -78,10 +86,8 @@ impl Cfg {
78 .add_source( 86 .add_source(
79 config::Environment::with_prefix("MODLIST").separator("_"), 87 config::Environment::with_prefix("MODLIST").separator("_"),
80 ) 88 )
81 .build() 89 .build()?
82 .unwrap() 90 .try_deserialize()?;
83 .try_deserialize()
84 .unwrap();
85 91
86 //Check cache 92 //Check cache
87 if !Path::new(&config.cache).exists() { 93 if !Path::new(&config.cache).exists() {