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.rs49
1 files changed, 25 insertions, 24 deletions
diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs
index 75e65e6..7cdc719 100644
--- a/src/apis/modrinth.rs
+++ b/src/apis/modrinth.rs
@@ -131,7 +131,6 @@ pub enum GameVersionType {
131} 131}
132 132
133/// # Errors 133/// # Errors
134/// # Panics
135async fn get( 134async fn get(
136 api: &str, 135 api: &str,
137 path: &str, 136 path: &str,
@@ -156,54 +155,57 @@ async fn get(
156} 155}
157 156
158/// # Errors 157/// # Errors
159/// # Panics 158pub async fn project(api: &str, name: &str) -> MLE<Project> {
160pub async fn project(api: &str, name: &str) -> Project {
161 let url = format!("project/{name}"); 159 let url = format!("project/{name}");
162 let data = get(api, &url).await.unwrap().unwrap(); 160 let data = get(api, &url).await
161 .map_err(|_| MLErr::new(EType::Other, "geterr"))?
162 .ok_or(MLErr::new(EType::Other, "geterr2"))?;
163 163
164 serde_json::from_slice(&data).unwrap() 164 serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from project"))
165} 165}
166 166
167/// # Errors 167/// # Errors
168/// # Panics 168pub async fn projects(api: &str, ids: Vec<String>) -> MLE<Vec<Project>> {
169pub async fn projects(api: &str, ids: Vec<String>) -> Vec<Project> {
170 let all = ids.join(r#"",""#); 169 let all = ids.join(r#"",""#);
171 let url = format!(r#"projects?ids=["{all}"]"#); 170 let url = format!(r#"projects?ids=["{all}"]"#);
172 171
173 let data = get(api, &url).await.unwrap().unwrap(); 172 let data = get(api, &url).await
173 .map_err(|_| MLErr::new(EType::Other, "geterr"))?
174 .ok_or(MLErr::new(EType::Other, "geterr2"))?;
174 175
175 serde_json::from_slice(&data).unwrap() 176 serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from projects"))
176} 177}
177 178
178///Get applicable versions from `mod_id` with list context 179///Get applicable versions from `mod_id` with list context
179/// # Errors 180/// # Errors
180/// # Panics 181pub async fn versions(api: &str, id: String, list: List) -> MLE<Vec<Version>> {
181pub async fn versions(api: &str, id: String, list: List) -> Vec<Version> {
182 let url = format!( 182 let url = format!(
183 r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, 183 r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#,
184 id, list.modloader, list.mc_version 184 id, list.modloader, list.mc_version
185 ); 185 );
186 186
187 let data = get(api, &url).await.unwrap(); 187 let data = get(api, &url).await
188 .map_err(|_| MLErr::new(EType::Other, "geterr"))?;
188 189
189 match data { 190 Ok(match data {
190 Some(data) => serde_json::from_slice(&data).unwrap(), 191 Some(data) => serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from version"))?,
191 None => Vec::new(), 192 None => Vec::new(),
192 } 193 })
193} 194}
194 195
195///Get version with the version ids 196///Get version with the version ids
196/// # Errors 197/// # Errors
197/// # Panics
198pub async fn get_raw_versions( 198pub async fn get_raw_versions(
199 api: &str, 199 api: &str,
200 versions: Vec<String>, 200 versions: Vec<String>,
201) -> Vec<Version> { 201) -> MLE<Vec<Version>> {
202 let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); 202 let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#));
203 203
204 let data = get(api, &url).await.unwrap().unwrap(); 204 let data = get(api, &url).await
205 .map_err(|_| MLErr::new(EType::Other, "geterr"))?
206 .ok_or(MLErr::new(EType::Other, "geterr2"))?;
205 207
206 serde_json::from_slice(&data).unwrap() 208 serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from raw version"))
207} 209}
208 210
209/// # Errors 211/// # Errors
@@ -224,12 +226,11 @@ pub fn extract_current_version(versions: Vec<Version>) -> MLE<String> {
224} 226}
225 227
226/// # Errors 228/// # Errors
227/// # Panics 229pub async fn get_game_versions() -> MLE<Vec<GameVersion>> {
228pub async fn get_game_versions() -> Vec<GameVersion> {
229 let data = get("https://api.modrinth.com/v2/", "tag/game_version") 230 let data = get("https://api.modrinth.com/v2/", "tag/game_version")
230 .await 231 .await
231 .unwrap() 232 .map_err(|_| MLErr::new(EType::Other, "geterr"))?
232 .unwrap(); 233 .ok_or(MLErr::new(EType::Other, "geterr2"))?;
233 234
234 serde_json::from_slice(&data).unwrap() 235 serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from game version"))
235} 236}