summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs41
1 files changed, 41 insertions, 0 deletions
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}