summaryrefslogtreecommitdiff
path: root/src/apis/modrinth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/apis/modrinth.rs')
-rw-r--r--src/apis/modrinth.rs75
1 files changed, 28 insertions, 47 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs
index 9afe7f3..525cc0d 100644
--- a/src/apis/modrinth.rs
+++ b/src/apis/modrinth.rs
@@ -1,10 +1,10 @@
1use chrono::{DateTime, FixedOffset}; 1use chrono::{DateTime, FixedOffset};
2use reqwest::Client; 2use reqwest::Client;
3use serde::Deserialize; 3use 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)]
@@ -113,7 +113,24 @@ pub struct Hash {
113 pub sha1: String, 113 pub sha1: String,
114} 114}
115 115
116async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> { 116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct GameVersion {
118 pub version: String,
119 pub version_type: GameVersionType,
120 pub date: String,
121 pub major: bool,
122}
123
124#[allow(non_camel_case_types)]
125#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
126pub enum GameVersionType {
127 release,
128 snapshot,
129 alpha,
130 beta
131}
132
133async fn get(api: &str, path: &str) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error>> {
117 let url = format!(r#"{}{}"#, api, path); 134 let url = format!(r#"{}{}"#, api, path);
118 135
119 let client = Client::builder() 136 let client = Client::builder()
@@ -135,7 +152,7 @@ async fn get(api: &str, path: String) -> Result<Option<Vec<u8>>, Box<dyn std::er
135 152
136pub async fn project(api: &str, name: &str) -> Project { 153pub async fn project(api: &str, name: &str) -> Project {
137 let url = format!("project/{}", name); 154 let url = format!("project/{}", name);
138 let data = get(api, url).await.unwrap().unwrap(); 155 let data = get(api, &url).await.unwrap().unwrap();
139 156
140 serde_json::from_slice(&data).unwrap() 157 serde_json::from_slice(&data).unwrap()
141} 158}
@@ -144,24 +161,19 @@ pub async fn projects(api: &str, ids: Vec<String>) -> Vec<Project> {
144 let all = ids.join(r#"",""#); 161 let all = ids.join(r#"",""#);
145 let url = format!(r#"projects?ids=["{}"]"#, all); 162 let url = format!(r#"projects?ids=["{}"]"#, all);
146 163
147 let data = get(api, url).await.unwrap().unwrap(); 164 let data = get(api, &url).await.unwrap().unwrap();
148 165
149 serde_json::from_slice(&data).unwrap() 166 serde_json::from_slice(&data).unwrap()
150} 167}
151 168
152///Get applicable versions from mod_id with list context 169///Get applicable versions from mod_id with list context
153pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> { 170pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> {
154 let loaderstr = match list.modloader {
155 Modloader::Forge => String::from("forge"),
156 Modloader::Fabric => String::from("fabric"),
157 };
158
159 let url = format!( 171 let url = format!(
160 r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, 172 r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#,
161 id, loaderstr, list.mc_version 173 id, list.modloader.to_string(), list.mc_version
162 ); 174 );
163 175
164 let data = get(api, url).await.unwrap(); 176 let data = get(api, &url).await.unwrap();
165 177
166 match data { 178 match data {
167 Some(data) => serde_json::from_slice(&data).unwrap(), 179 Some(data) => serde_json::from_slice(&data).unwrap(),
@@ -173,7 +185,7 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> {
173pub async fn get_raw_versions(api: &str, versions: Vec<String>) -> Vec<Version> { 185pub async fn get_raw_versions(api: &str, versions: Vec<String>) -> Vec<Version> {
174 let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); 186 let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#));
175 187
176 let data = get(api, url).await.unwrap().unwrap(); 188 let data = get(api, &url).await.unwrap().unwrap();
177 189
178 serde_json::from_slice(&data).unwrap() 190 serde_json::from_slice(&data).unwrap()
179} 191}
@@ -195,39 +207,8 @@ pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> {
195 } 207 }
196} 208}
197 209
198pub enum MCVersionType { 210pub async fn get_game_versions() -> Vec<GameVersion> {
199 Release, 211 let data = get("https://api.modrinth.com/v2/", "tag/game_version").await.unwrap().unwrap();
200 Latest,
201 Specific,
202}
203
204#[derive(Debug, Deserialize)]
205pub struct MCVersion {
206 pub version: String,
207 pub version_type: String,
208 pub date: String,
209 pub major: bool,
210}
211 212
212pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String { 213 serde_json::from_slice(&data).unwrap()
213 let data = get(api, String::from("tag/game_version"))
214 .await
215 .unwrap()
216 .unwrap();
217 let mc_versions: Vec<MCVersion> = serde_json::from_slice(&data).unwrap();
218 let ver = match version {
219 MCVersionType::Release => {
220 let mut i = 0;
221 while !mc_versions[i].major {
222 i += 1;
223 }
224 &mc_versions[i]
225 }
226 MCVersionType::Latest => &mc_versions[0],
227 MCVersionType::Specific => {
228 println!("Not inplemented");
229 &mc_versions[0]
230 }
231 };
232 String::from(&ver.version)
233} 214}