summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2022-12-26 21:57:03 +0100
committerFxQnLr <[email protected]>2022-12-26 21:57:03 +0100
commit0515548682a95db643a008146105d8ecdb446e58 (patch)
tree6622c5e6b0f97fe83aa559bda128d13947fe92b5 /src
parent43206da6f40027715582599baa0ad1c91477539e (diff)
downloadmodlist-0515548682a95db643a008146105d8ecdb446e58.tar
modlist-0515548682a95db643a008146105d8ecdb446e58.tar.gz
modlist-0515548682a95db643a008146105d8ecdb446e58.zip
changed config to toml, autocreate config;
better error handling
Diffstat (limited to 'src')
-rw-r--r--src/config.rs40
-rw-r--r--src/error.rs33
-rw-r--r--src/main.rs2
3 files changed, 56 insertions, 19 deletions
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 @@
1use config::{Config, File, FileFormat}; 1use std::{fs::File, io::{Read, Write}};
2use serde::Deserialize;
3 2
4#[derive(Debug, Clone, Deserialize)] 3use serde::{Serialize, Deserialize};
4
5use crate::error::MLE;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct Cfg { 8pub struct Cfg {
6 pub data: String, 9 pub data: String,
7 pub downloads: String,
8 pub clean_remove: bool,
9 pub apis: Apis, 10 pub apis: Apis,
10} 11}
11 12
12#[derive(Debug, Clone, Deserialize)] 13#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct Apis { 14pub struct Apis {
14 pub modrinth: String, 15 pub modrinth: String,
15} 16}
16 17
17impl Cfg { 18impl Cfg {
18 pub fn init(path: &str) -> Self { 19 pub fn init(path: &str) -> MLE<Self> {
19 //TODO Error Handling 20 let mut file = match File::open(path) {
20 Config::builder() 21 Ok(file) => file,
21 .add_source(File::new(path, FileFormat::Ini)) 22 Err(err) => {
22 .build() 23 if err.kind() == std::io::ErrorKind::NotFound {
23 .unwrap() 24 println!("No config file found, creating one");
24 .try_deserialize() 25 let default_cfg = Cfg { data: String::from("./"), apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/") } };
25 .unwrap() 26 let mut file = File::create(path)?;
27 file.write_all(&toml::to_string(&default_cfg)?.as_bytes())?;
28 File::open(path)?
29 } else {
30 return Err(err.into());
31 }
32 }
33 };
34 let mut content = String::new();
35 file.read_to_string(&mut content)?;
36 let config = toml::from_str::<Cfg>(&content)?;
37 Ok(config)
26 } 38 }
27} 39}
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 @@
1use core::fmt; 1use core::fmt;
2use serde::Deserialize;
2 3
3pub type MLE<T> = Result<T, MLError>; 4pub type MLE<T> = Result<T, MLError>;
4 5
5#[derive(Debug)] 6#[derive(Debug, Deserialize)]
6pub struct MLError { 7pub struct MLError {
7 etype: ErrorType, 8 etype: ErrorType,
8 message: String, 9 message: String,
9} 10}
10 11
11#[derive(Debug)] 12#[derive(Debug, Deserialize)]
12pub enum ErrorType { 13pub enum ErrorType {
13 ArgumentError 14 ArgumentError,
15 ConfigError,
16 LibToml,
17 IoError,
14} 18}
15 19
16impl std::error::Error for MLError { 20impl std::error::Error for MLError {
@@ -22,11 +26,32 @@ impl std::error::Error for MLError {
22impl fmt::Display for MLError { 26impl fmt::Display for MLError {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 27 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 match self.etype { 28 match self.etype {
25 ErrorType::ArgumentError => write!(f, "ARGS") 29 ErrorType::ArgumentError => write!(f, "ARGS"),
30 ErrorType::ConfigError => write!(f, "CONFIG"),
31 ErrorType::LibToml => write!(f, "TOML"),
32 ErrorType::IoError => write!(f, "IO")
26 } 33 }
27 } 34 }
28} 35}
29 36
37impl From<toml::de::Error> for MLError {
38 fn from(error: toml::de::Error) -> Self {
39 Self { etype: ErrorType::LibToml, message: error.to_string() }
40 }
41}
42
43impl From<toml::ser::Error> for MLError {
44 fn from(error: toml::ser::Error) -> Self {
45 Self { etype: ErrorType::LibToml, message: error.to_string() }
46 }
47}
48
49impl From<std::io::Error> for MLError {
50 fn from(error: std::io::Error) -> Self {
51 Self { etype: ErrorType::IoError, message: error.to_string() }
52 }
53}
54
30impl MLError { 55impl MLError {
31 pub fn new(etype: ErrorType, message: &str) -> Self { 56 pub fn new(etype: ErrorType, message: &str) -> Self {
32 Self { etype, message: String::from(message) } 57 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};
2 2
3#[tokio::main] 3#[tokio::main]
4async fn main() { 4async fn main() {
5 let config = Cfg::init("config.ini"); 5 let config = Cfg::init("config.toml").unwrap();
6 //TODO Error Handling 6 //TODO Error Handling
7 get_input(config).await.unwrap(); 7 get_input(config).await.unwrap();
8} 8}