diff options
author | FxQnLr <[email protected]> | 2022-12-26 21:57:03 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2022-12-26 21:57:03 +0100 |
commit | 0515548682a95db643a008146105d8ecdb446e58 (patch) | |
tree | 6622c5e6b0f97fe83aa559bda128d13947fe92b5 /src | |
parent | 43206da6f40027715582599baa0ad1c91477539e (diff) | |
download | modlist-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.rs | 40 | ||||
-rw-r--r-- | src/error.rs | 33 | ||||
-rw-r--r-- | src/main.rs | 2 |
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 @@ | |||
1 | use config::{Config, File, FileFormat}; | 1 | use std::{fs::File, io::{Read, Write}}; |
2 | use serde::Deserialize; | ||
3 | 2 | ||
4 | #[derive(Debug, Clone, Deserialize)] | 3 | use serde::{Serialize, Deserialize}; |
4 | |||
5 | use crate::error::MLE; | ||
6 | |||
7 | #[derive(Debug, Clone, Serialize, Deserialize)] | ||
5 | pub struct Cfg { | 8 | pub 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)] |
13 | pub struct Apis { | 14 | pub struct Apis { |
14 | pub modrinth: String, | 15 | pub modrinth: String, |
15 | } | 16 | } |
16 | 17 | ||
17 | impl Cfg { | 18 | impl 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 @@ | |||
1 | use core::fmt; | 1 | use core::fmt; |
2 | use serde::Deserialize; | ||
2 | 3 | ||
3 | pub type MLE<T> = Result<T, MLError>; | 4 | pub type MLE<T> = Result<T, MLError>; |
4 | 5 | ||
5 | #[derive(Debug)] | 6 | #[derive(Debug, Deserialize)] |
6 | pub struct MLError { | 7 | pub 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)] |
12 | pub enum ErrorType { | 13 | pub enum ErrorType { |
13 | ArgumentError | 14 | ArgumentError, |
15 | ConfigError, | ||
16 | LibToml, | ||
17 | IoError, | ||
14 | } | 18 | } |
15 | 19 | ||
16 | impl std::error::Error for MLError { | 20 | impl std::error::Error for MLError { |
@@ -22,11 +26,32 @@ impl std::error::Error for MLError { | |||
22 | impl fmt::Display for MLError { | 26 | impl 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 | ||
37 | impl 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 | |||
43 | impl 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 | |||
49 | impl 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 | |||
30 | impl MLError { | 55 | impl 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] |
4 | async fn main() { | 4 | async 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 | } |