summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs96
1 files changed, 11 insertions, 85 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 73b5697..b48bf61 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,12 +12,16 @@ pub use apis::*;
12use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; 12use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
13pub use commands::*; 13pub use commands::*;
14use error::{ErrorType, MLError, MLE}; 14use error::{ErrorType, MLError, MLE};
15use serde::{Deserialize, Serialize, de::Visitor}; 15use serde::{Deserialize, Serialize};
16 16
17#[derive(Debug, Clone, PartialEq, Eq)] 17#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
18pub enum Modloader { 18pub enum Modloader {
19 #[serde(rename(serialize = "fabric", deserialize = "fabric"))]
19 Fabric, 20 Fabric,
21 #[serde(rename(serialize = "forge", deserialize = "forge"))]
20 Forge, 22 Forge,
23 #[serde(rename(serialize = "quilt", deserialize = "quilt"))]
24 Quilt,
21} 25}
22 26
23impl Modloader { 27impl Modloader {
@@ -35,53 +39,16 @@ impl Display for Modloader {
35 match self { 39 match self {
36 Modloader::Fabric => write!(f, "fabric"), 40 Modloader::Fabric => write!(f, "fabric"),
37 Modloader::Forge => write!(f, "forge"), 41 Modloader::Forge => write!(f, "forge"),
42 Modloader::Quilt => write!(f, "quilt"),
38 } 43 }
39 } 44 }
40} 45}
41 46
42impl Serialize for Modloader { 47#[derive(Debug, Clone, Deserialize, Serialize)]
43 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
44 where
45 S: serde::Serializer {
46 match self {
47 Modloader::Fabric => serializer.serialize_str("fabric"),
48 Modloader::Forge => serializer.serialize_str("forge"),
49 }
50 }
51}
52
53impl<'de> Deserialize<'de> for Modloader {
54 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
55 where
56 D: serde::Deserializer<'de>,
57 {
58 struct FieldVisitor;
59
60 impl<'de> Visitor<'de> for FieldVisitor {
61 type Value = Modloader;
62
63 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
64 formatter.write_str("`fabric`, `forge` or `quilt`")
65 }
66
67 fn visit_str<E>(self, v: &str) -> Result<Modloader, E>
68 where
69 E: serde::de::Error, {
70 match v {
71 "fabric" => Ok(Modloader::Fabric),
72 "forge" => Ok(Modloader::Forge),
73 _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"]))
74 }
75 }
76 }
77
78 deserializer.deserialize_identifier(FieldVisitor)
79 }
80}
81
82#[derive(Debug, Clone)]
83pub enum VersionLevel { 48pub enum VersionLevel {
49 #[serde(rename(serialize = "release", deserialize = "release"))]
84 Release, 50 Release,
51 #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))]
85 Snapshot, 52 Snapshot,
86 Version(String) 53 Version(String)
87} 54}
@@ -101,7 +68,7 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
101 Ok(()) 68 Ok(())
102} 69}
103 70
104/// Loads game versions 71/// Loads game versions from file
105pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { 72pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> {
106 let mut file = File::open(path)?; 73 let mut file = File::open(path)?;
107 let mut data = String::new(); 74 let mut data = String::new();
@@ -145,44 +112,3 @@ impl VersionLevel {
145 } 112 }
146 } 113 }
147} 114}
148
149impl Serialize for VersionLevel {
150 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
151 where
152 S: serde::Serializer {
153 match self {
154 VersionLevel::Release => serializer.serialize_str("release"),
155 VersionLevel::Snapshot => serializer.serialize_str("snapshot"),
156 VersionLevel::Version(v) => serializer.serialize_str(v),
157 }
158 }
159}
160
161impl<'de> Deserialize<'de> for VersionLevel {
162 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
163 where
164 D: serde::Deserializer<'de>,
165 {
166 struct FieldVisitor;
167
168 impl<'de> Visitor<'de> for FieldVisitor {
169 type Value = VersionLevel;
170
171 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
172 formatter.write_str("`fabric`, `forge` or `quilt`")
173 }
174
175 fn visit_str<E>(self, v: &str) -> Result<VersionLevel, E>
176 where
177 E: serde::de::Error, {
178 match v {
179 "release" => Ok(VersionLevel::Release),
180 "snapshot" => Ok(VersionLevel::Snapshot),
181 _ => Ok(VersionLevel::Version(String::from(v)))
182 }
183 }
184 }
185
186 deserializer.deserialize_identifier(FieldVisitor)
187 }
188}