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/config.rs | |
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/config.rs')
-rw-r--r-- | src/config.rs | 40 |
1 files changed, 26 insertions, 14 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 | } |