From 0515548682a95db643a008146105d8ecdb446e58 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Mon, 26 Dec 2022 21:57:03 +0100 Subject: changed config to toml, autocreate config; better error handling --- src/config.rs | 40 ++++++++++++++++++++++++++-------------- src/error.rs | 33 +++++++++++++++++++++++++++++---- src/main.rs | 2 +- 3 files changed, 56 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index ad59963..99d2ec2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,27 +1,39 @@ -use config::{Config, File, FileFormat}; -use serde::Deserialize; +use std::{fs::File, io::{Read, Write}}; -#[derive(Debug, Clone, Deserialize)] +use serde::{Serialize, Deserialize}; + +use crate::error::MLE; + +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { pub data: String, - pub downloads: String, - pub clean_remove: bool, pub apis: Apis, } -#[derive(Debug, Clone, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Apis { pub modrinth: String, } impl Cfg { - pub fn init(path: &str) -> Self { - //TODO Error Handling - Config::builder() - .add_source(File::new(path, FileFormat::Ini)) - .build() - .unwrap() - .try_deserialize() - .unwrap() + pub fn init(path: &str) -> MLE { + let mut file = match File::open(path) { + Ok(file) => file, + Err(err) => { + if err.kind() == std::io::ErrorKind::NotFound { + println!("No config file found, creating one"); + let default_cfg = Cfg { data: String::from("./"), apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/") } }; + let mut file = File::create(path)?; + file.write_all(&toml::to_string(&default_cfg)?.as_bytes())?; + File::open(path)? + } else { + return Err(err.into()); + } + } + }; + let mut content = String::new(); + file.read_to_string(&mut content)?; + let config = toml::from_str::(&content)?; + Ok(config) } } diff --git a/src/error.rs b/src/error.rs index c82688c..1ac2a48 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,16 +1,20 @@ use core::fmt; +use serde::Deserialize; pub type MLE = Result; -#[derive(Debug)] +#[derive(Debug, Deserialize)] pub struct MLError { etype: ErrorType, message: String, } -#[derive(Debug)] +#[derive(Debug, Deserialize)] pub enum ErrorType { - ArgumentError + ArgumentError, + ConfigError, + LibToml, + IoError, } impl std::error::Error for MLError { @@ -22,11 +26,32 @@ impl std::error::Error for MLError { impl fmt::Display for MLError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.etype { - ErrorType::ArgumentError => write!(f, "ARGS") + ErrorType::ArgumentError => write!(f, "ARGS"), + ErrorType::ConfigError => write!(f, "CONFIG"), + ErrorType::LibToml => write!(f, "TOML"), + ErrorType::IoError => write!(f, "IO") } } } +impl From for MLError { + fn from(error: toml::de::Error) -> Self { + Self { etype: ErrorType::LibToml, message: error.to_string() } + } +} + +impl From for MLError { + fn from(error: toml::ser::Error) -> Self { + Self { etype: ErrorType::LibToml, message: error.to_string() } + } +} + +impl From for MLError { + fn from(error: std::io::Error) -> Self { + Self { etype: ErrorType::IoError, message: error.to_string() } + } +} + impl MLError { pub fn new(etype: ErrorType, message: &str) -> Self { Self { etype, message: String::from(message) } diff --git a/src/main.rs b/src/main.rs index 957e5aa..a093bb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use modlist::{config::Cfg, input::get_input}; #[tokio::main] async fn main() { - let config = Cfg::init("config.ini"); + let config = Cfg::init("config.toml").unwrap(); //TODO Error Handling get_input(config).await.unwrap(); } -- cgit v1.2.3