diff options
author | fxqnlr <[email protected]> | 2023-04-30 20:11:10 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-04-30 20:11:10 +0200 |
commit | e8cac6786bc2e91382316ef1023a494c3e812013 (patch) | |
tree | b8b463cfbbea2290829b1a53d4835ee8fb2742c5 | |
parent | 06f933dac333d58b2cc3ae7ab0c285e5015a0257 (diff) | |
download | modlist-e8cac6786bc2e91382316ef1023a494c3e812013.tar modlist-e8cac6786bc2e91382316ef1023a494c3e812013.tar.gz modlist-e8cac6786bc2e91382316ef1023a494c3e812013.zip |
added modloader default to config
-rw-r--r-- | src/config.rs | 11 | ||||
-rw-r--r-- | src/lib.rs | 41 | ||||
-rw-r--r-- | src/main.rs | 3 |
3 files changed, 52 insertions, 3 deletions
diff --git a/src/config.rs b/src/config.rs index 9064548..cf27257 100644 --- a/src/config.rs +++ b/src/config.rs | |||
@@ -6,12 +6,13 @@ use std::{ | |||
6 | 6 | ||
7 | use serde::{Deserialize, Serialize}; | 7 | use serde::{Deserialize, Serialize}; |
8 | 8 | ||
9 | use crate::{db::db_setup, error::MLE}; | 9 | use crate::{db::db_setup, error::MLE, Modloader}; |
10 | 10 | ||
11 | #[derive(Debug, Clone, Serialize, Deserialize)] | 11 | #[derive(Debug, Clone, Serialize, Deserialize)] |
12 | pub struct Cfg { | 12 | pub struct Cfg { |
13 | pub data: String, | 13 | pub data: String, |
14 | pub cache: String, | 14 | pub cache: String, |
15 | pub defaults: Defaults, | ||
15 | pub apis: Apis, | 16 | pub apis: Apis, |
16 | } | 17 | } |
17 | 18 | ||
@@ -20,6 +21,11 @@ pub struct Apis { | |||
20 | pub modrinth: String, | 21 | pub modrinth: String, |
21 | } | 22 | } |
22 | 23 | ||
24 | #[derive(Debug, Clone, Serialize, Deserialize)] | ||
25 | pub struct Defaults { | ||
26 | pub modloader: Modloader, | ||
27 | } | ||
28 | |||
23 | impl Cfg { | 29 | impl Cfg { |
24 | pub fn init(path: Option<String>) -> MLE<Self> { | 30 | pub fn init(path: Option<String>) -> MLE<Self> { |
25 | let configfile = match path.clone() { | 31 | let configfile = match path.clone() { |
@@ -72,6 +78,9 @@ fn create_config(path: &str) -> MLE<()> { | |||
72 | let default_cfg = Cfg { | 78 | let default_cfg = Cfg { |
73 | data: cache_dir.clone(), | 79 | data: cache_dir.clone(), |
74 | cache: format!("{}/cache", cache_dir), | 80 | cache: format!("{}/cache", cache_dir), |
81 | defaults: Defaults { | ||
82 | modloader: Modloader::Fabric | ||
83 | }, | ||
75 | apis: Apis { | 84 | apis: Apis { |
76 | modrinth: String::from("https://api.modrinth.com/v2/"), | 85 | modrinth: String::from("https://api.modrinth.com/v2/"), |
77 | }, | 86 | }, |
@@ -11,6 +11,7 @@ use std::fmt::Display; | |||
11 | pub use apis::*; | 11 | pub use apis::*; |
12 | pub use commands::*; | 12 | pub use commands::*; |
13 | use error::{ErrorType, MLError, MLE}; | 13 | use error::{ErrorType, MLError, MLE}; |
14 | use serde::{Deserialize, Serialize, de::Visitor}; | ||
14 | 15 | ||
15 | #[derive(Debug, Clone, PartialEq, Eq)] | 16 | #[derive(Debug, Clone, PartialEq, Eq)] |
16 | pub enum Modloader { | 17 | pub enum Modloader { |
@@ -36,3 +37,43 @@ impl Display for Modloader { | |||
36 | } | 37 | } |
37 | } | 38 | } |
38 | } | 39 | } |
40 | |||
41 | impl Serialize for Modloader { | ||
42 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
43 | where | ||
44 | S: serde::Serializer { | ||
45 | match self { | ||
46 | Modloader::Fabric => serializer.serialize_str("fabric"), | ||
47 | Modloader::Forge => serializer.serialize_str("forge"), | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | impl<'de> Deserialize<'de> for Modloader { | ||
53 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
54 | where | ||
55 | D: serde::Deserializer<'de>, | ||
56 | { | ||
57 | struct FieldVisitor; | ||
58 | |||
59 | impl<'de> Visitor<'de> for FieldVisitor { | ||
60 | type Value = Modloader; | ||
61 | |||
62 | fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
63 | formatter.write_str("`fabric`, `forge` or `quilt`") | ||
64 | } | ||
65 | |||
66 | fn visit_str<E>(self, v: &str) -> Result<Modloader, E> | ||
67 | where | ||
68 | E: serde::de::Error, { | ||
69 | match v { | ||
70 | "fabric" => Ok(Modloader::Fabric), | ||
71 | "forge" => Ok(Modloader::Forge), | ||
72 | _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"])) | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | deserializer.deserialize_identifier(FieldVisitor) | ||
78 | } | ||
79 | } | ||
diff --git a/src/main.rs b/src/main.rs index 53cbe71..82c0ade 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -201,8 +201,7 @@ async fn main() { | |||
201 | } => { | 201 | } => { |
202 | let ml = match modloader { | 202 | let ml = match modloader { |
203 | Some(ml) => Modloader::from(&ml).unwrap(), | 203 | Some(ml) => Modloader::from(&ml).unwrap(), |
204 | //TODO add default modloader to config | 204 | None => config.clone().defaults.modloader, |
205 | None => Modloader::Fabric, | ||
206 | }; | 205 | }; |
207 | 206 | ||
208 | let ver = match version { | 207 | let ver = match version { |