summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2023-05-03 08:01:51 +0200
committerfxqnlr <[email protected]>2023-05-03 08:01:51 +0200
commit245b6b9926033acd750503ab4947c9514c5e1aa6 (patch)
tree23734750cb3c80c69cde2599a2871adc3cd6b659
parent202ed4fcaa69e7de23e83fab0bae4ced6209eee4 (diff)
downloadmodlist-245b6b9926033acd750503ab4947c9514c5e1aa6.tar
modlist-245b6b9926033acd750503ab4947c9514c5e1aa6.tar.gz
modlist-245b6b9926033acd750503ab4947c9514c5e1aa6.zip
fuck serde, plus maybe added quilt
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/apis/modrinth.rs9
-rw-r--r--src/lib.rs96
4 files changed, 15 insertions, 94 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 0e93641..7422038 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -798,7 +798,7 @@ dependencies = [
798 798
799[[package]] 799[[package]]
800name = "modlist" 800name = "modlist"
801version = "0.14.0" 801version = "0.14.1"
802dependencies = [ 802dependencies = [
803 "chrono", 803 "chrono",
804 "clap", 804 "clap",
diff --git a/Cargo.toml b/Cargo.toml
index 736831d..bd41811 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "modlist" 2name = "modlist"
3version = "0.14.0" 3version = "0.14.1"
4edition = "2021" 4edition = "2021"
5 5
6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 6# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
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
5use crate::{ 5use 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
170pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { 170pub 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();
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}