summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.rs11
-rw-r--r--src/lib.rs41
-rw-r--r--src/main.rs3
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
7use serde::{Deserialize, Serialize}; 7use serde::{Deserialize, Serialize};
8 8
9use crate::{db::db_setup, error::MLE}; 9use crate::{db::db_setup, error::MLE, Modloader};
10 10
11#[derive(Debug, Clone, Serialize, Deserialize)] 11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Cfg { 12pub 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)]
25pub struct Defaults {
26 pub modloader: Modloader,
27}
28
23impl Cfg { 29impl 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 },
diff --git a/src/lib.rs b/src/lib.rs
index 185edd7..05d74db 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,6 +11,7 @@ use std::fmt::Display;
11pub use apis::*; 11pub use apis::*;
12pub use commands::*; 12pub use commands::*;
13use error::{ErrorType, MLError, MLE}; 13use error::{ErrorType, MLError, MLE};
14use serde::{Deserialize, Serialize, de::Visitor};
14 15
15#[derive(Debug, Clone, PartialEq, Eq)] 16#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum Modloader { 17pub enum Modloader {
@@ -36,3 +37,43 @@ impl Display for Modloader {
36 } 37 }
37 } 38 }
38} 39}
40
41impl 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
52impl<'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 {