diff options
author | fxqnlr <[email protected]> | 2023-05-03 08:01:51 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2023-05-03 08:01:51 +0200 |
commit | 245b6b9926033acd750503ab4947c9514c5e1aa6 (patch) | |
tree | 23734750cb3c80c69cde2599a2871adc3cd6b659 /src | |
parent | 202ed4fcaa69e7de23e83fab0bae4ced6209eee4 (diff) | |
download | modlist-245b6b9926033acd750503ab4947c9514c5e1aa6.tar modlist-245b6b9926033acd750503ab4947c9514c5e1aa6.tar.gz modlist-245b6b9926033acd750503ab4947c9514c5e1aa6.zip |
fuck serde, plus maybe added quilt
Diffstat (limited to 'src')
-rw-r--r-- | src/apis/modrinth.rs | 9 | ||||
-rw-r--r-- | src/lib.rs | 96 |
2 files changed, 13 insertions, 92 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 13e7a6d..525cc0d 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs | |||
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; | |||
4 | 4 | ||
5 | use crate::{ | 5 | use crate::{ |
6 | error::{ErrorType, MLError, MLE}, | 6 | error::{ErrorType, MLError, MLE}, |
7 | List, Modloader, | 7 | List, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | #[derive(Debug, Deserialize, Clone)] | 10 | #[derive(Debug, Deserialize, Clone)] |
@@ -168,14 +168,9 @@ pub async fn projects(api: &str, ids: Vec<String>) -> Vec<Project> { | |||
168 | 168 | ||
169 | ///Get applicable versions from mod_id with list context | 169 | ///Get applicable versions from mod_id with list context |
170 | pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { | 170 | pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { |
171 | let loaderstr = match list.modloader { | ||
172 | Modloader::Forge => String::from("forge"), | ||
173 | Modloader::Fabric => String::from("fabric"), | ||
174 | }; | ||
175 | |||
176 | let url = format!( | 171 | let url = format!( |
177 | r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, | 172 | r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, |
178 | id, loaderstr, list.mc_version | 173 | id, list.modloader.to_string(), list.mc_version |
179 | ); | 174 | ); |
180 | 175 | ||
181 | let data = get(api, &url).await.unwrap(); | 176 | let data = get(api, &url).await.unwrap(); |
@@ -12,12 +12,16 @@ pub use apis::*; | |||
12 | use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; | 12 | use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; |
13 | pub use commands::*; | 13 | pub use commands::*; |
14 | use error::{ErrorType, MLError, MLE}; | 14 | use error::{ErrorType, MLError, MLE}; |
15 | use serde::{Deserialize, Serialize, de::Visitor}; | 15 | use serde::{Deserialize, Serialize}; |
16 | 16 | ||
17 | #[derive(Debug, Clone, PartialEq, Eq)] | 17 | #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] |
18 | pub enum Modloader { | 18 | pub 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 | ||
23 | impl Modloader { | 27 | impl 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 | ||
42 | impl 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 | |||
53 | impl<'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)] | ||
83 | pub enum VersionLevel { | 48 | pub 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 |
105 | pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { | 72 | pub 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 | |||
149 | impl 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 | |||
161 | impl<'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 | } | ||