diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -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 | } | ||