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/lib.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/lib.rs') 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) + } +} -- cgit v1.2.3