From e8cac6786bc2e91382316ef1023a494c3e812013 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sun, 30 Apr 2023 20:11:10 +0200 Subject: added modloader default to config --- src/config.rs | 11 ++++++++++- src/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 +-- 3 files changed, 52 insertions(+), 3 deletions(-) (limited to 'src') 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::{ use serde::{Deserialize, Serialize}; -use crate::{db::db_setup, error::MLE}; +use crate::{db::db_setup, error::MLE, Modloader}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Cfg { pub data: String, pub cache: String, + pub defaults: Defaults, pub apis: Apis, } @@ -20,6 +21,11 @@ pub struct Apis { pub modrinth: String, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Defaults { + pub modloader: Modloader, +} + impl Cfg { pub fn init(path: Option) -> MLE { let configfile = match path.clone() { @@ -72,6 +78,9 @@ fn create_config(path: &str) -> MLE<()> { let default_cfg = Cfg { data: cache_dir.clone(), cache: format!("{}/cache", cache_dir), + defaults: Defaults { + modloader: Modloader::Fabric + }, apis: Apis { modrinth: String::from("https://api.modrinth.com/v2/"), }, 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; pub use apis::*; pub use commands::*; use error::{ErrorType, MLError, MLE}; +use serde::{Deserialize, Serialize, de::Visitor}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum Modloader { @@ -36,3 +37,43 @@ impl Display for Modloader { } } } + +impl Serialize for Modloader { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer { + match self { + Modloader::Fabric => serializer.serialize_str("fabric"), + Modloader::Forge => serializer.serialize_str("forge"), + } + } +} + +impl<'de> Deserialize<'de> for Modloader { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + struct FieldVisitor; + + impl<'de> Visitor<'de> for FieldVisitor { + type Value = Modloader; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("`fabric`, `forge` or `quilt`") + } + + fn visit_str(self, v: &str) -> Result + where + E: serde::de::Error, { + match v { + "fabric" => Ok(Modloader::Fabric), + "forge" => Ok(Modloader::Forge), + _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"])) + } + } + } + + deserializer.deserialize_identifier(FieldVisitor) + } +} 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() { } => { let ml = match modloader { Some(ml) => Modloader::from(&ml).unwrap(), - //TODO add default modloader to config - None => Modloader::Fabric, + None => config.clone().defaults.modloader, }; let ver = match version { -- cgit v1.2.3