From 7a85cf311c85ab45c75098dae58b5ebf5fef60bc Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 13:48:32 +0200 Subject: add config crate, slight cleanup --- src/config.rs | 103 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 49 insertions(+), 54 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 6280932..8ecdc69 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,14 +1,16 @@ use std::{ fs::{create_dir_all, File}, - io::{Read, Write}, + io::Write, path::Path, }; -use indicatif::{ProgressBar, ProgressStyle}; use serde::{Deserialize, Serialize}; use crate::{ - check_game_versions, db::setup, error::{EType, MLErr, MLE}, Modloader, VersionLevel, + check_game_versions, + db::setup, + error::{EType, MLErr, MLE}, + Modloader, VersionLevel, }; #[derive(Debug, Clone, Serialize, Deserialize)] @@ -20,6 +22,28 @@ pub struct Cfg { pub apis: Apis, } +impl Default for Cfg { + fn default() -> Self { + let cache_dir = dirs::cache_dir() + .unwrap() + .join("modlist") + .to_string_lossy() + .to_string(); + Self { + data: cache_dir.clone(), + cache: format!("{cache_dir}/cache"), + versions: cache_dir, + defaults: Defaults { + modloader: Modloader::Fabric, + version: VersionLevel::Release, + }, + apis: Apis { + modrinth: String::from("https://api.modrinth.com/v2/"), + }, + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Apis { pub modrinth: String, @@ -44,21 +68,24 @@ impl Cfg { .to_string(), }; - let mut file = match File::open(&configfile) { - Ok(file) => file, - Err(err) => { - if err.kind() == std::io::ErrorKind::NotFound && path.is_none() - { - create_config(&configfile)?; - File::open(&configfile)? - } else { - return Err(err.into()); - } - } + if let Some(err) = File::open(&configfile).err() { + if err.kind() == std::io::ErrorKind::NotFound && path.is_none() { + create_config(&configfile)?; + } else { + return Err(err.into()); + }; }; - let mut content = String::new(); - file.read_to_string(&mut content)?; - let config = toml::from_str::(&content)?; + + let config: Cfg = config::Config::builder() + .add_source(config::File::with_name(&configfile).required(false)) + .add_source( + config::Environment::with_prefix("MODLIST").separator("_"), + ) + .build() + .unwrap() + .try_deserialize() + .unwrap(); + //Check cache if !Path::new(&config.cache).exists() { create_cache(&config.cache)?; @@ -81,60 +108,28 @@ impl Cfg { } fn create_config(path: &str) -> MLE<()> { - let p = ProgressBar::new(1); - p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap()); - p.set_message("Create default config"); - - let cache_dir = dirs::cache_dir() - .unwrap() - .join("modlist") - .to_string_lossy() - .to_string(); - let default_cfg = Cfg { - data: cache_dir.clone(), - cache: format!("{cache_dir}/cache"), - versions: cache_dir, - defaults: Defaults { - modloader: Modloader::Fabric, - version: VersionLevel::Release, - }, - apis: Apis { - modrinth: String::from("https://api.modrinth.com/v2/"), - }, - }; create_dir_all(path.split("config.toml").collect::>()[0])?; let mut file = File::create(path)?; - file.write_all(toml::to_string(&default_cfg)?.as_bytes())?; - p.finish_with_message(format!("Created default config ({path})")); + file.write_all(toml::to_string(&Cfg::default())?.as_bytes())?; + println!("Created default config ({path})"); Ok(()) } fn create_database(path: &str) -> MLE<()> { - let p = ProgressBar::new(1); - p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap()); - p.set_message("Create database"); - File::create(path)?; setup(path)?; - p.finish_with_message(format!("Created database ({path})")); + println!("Created database ({path})"); Ok(()) } fn create_cache(path: &str) -> MLE<()> { - let p = ProgressBar::new(1); - p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap()); - p.set_message("Create cache"); - create_dir_all(path)?; - p.finish_with_message(format!("Created cache ({path})")); + println!("Created cache ({path})"); Ok(()) } fn create_versions_dummy(path: &str) -> MLE<()> { - let p = ProgressBar::new(1); - p.set_style(ProgressStyle::with_template("{wide_msg}").unwrap()); - p.set_message("Create version file"); File::create(path)?; - p.finish_with_message(format!("Created version file ({path})")); + println!("Created version file ({path})"); Ok(()) } -- cgit v1.2.3