From 202ed4fcaa69e7de23e83fab0bae4ced6209eee4 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Tue, 2 May 2023 20:37:23 +0200 Subject: added config option for default mc version changed version input system --- src/apis/modrinth.rs | 66 +++++++++++++++++++++------------------------------- 1 file changed, 26 insertions(+), 40 deletions(-) (limited to 'src/apis') diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 9afe7f3..13e7a6d 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, FixedOffset}; use reqwest::Client; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use crate::{ error::{ErrorType, MLError, MLE}, @@ -113,7 +113,24 @@ pub struct Hash { pub sha1: String, } -async fn get(api: &str, path: String) -> Result>, Box> { +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct GameVersion { + pub version: String, + pub version_type: GameVersionType, + pub date: String, + pub major: bool, +} + +#[allow(non_camel_case_types)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum GameVersionType { + release, + snapshot, + alpha, + beta +} + +async fn get(api: &str, path: &str) -> Result>, Box> { let url = format!(r#"{}{}"#, api, path); let client = Client::builder() @@ -135,7 +152,7 @@ async fn get(api: &str, path: String) -> Result>, Box Project { let url = format!("project/{}", name); - let data = get(api, url).await.unwrap().unwrap(); + let data = get(api, &url).await.unwrap().unwrap(); serde_json::from_slice(&data).unwrap() } @@ -144,7 +161,7 @@ pub async fn projects(api: &str, ids: Vec) -> Vec { let all = ids.join(r#"",""#); let url = format!(r#"projects?ids=["{}"]"#, all); - let data = get(api, url).await.unwrap().unwrap(); + let data = get(api, &url).await.unwrap().unwrap(); serde_json::from_slice(&data).unwrap() } @@ -161,7 +178,7 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec { id, loaderstr, list.mc_version ); - let data = get(api, url).await.unwrap(); + let data = get(api, &url).await.unwrap(); match data { Some(data) => serde_json::from_slice(&data).unwrap(), @@ -173,7 +190,7 @@ pub async fn versions(api: &str, id: String, list: List) -> Vec { pub async fn get_raw_versions(api: &str, versions: Vec) -> Vec { let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); - let data = get(api, url).await.unwrap().unwrap(); + let data = get(api, &url).await.unwrap().unwrap(); serde_json::from_slice(&data).unwrap() } @@ -195,39 +212,8 @@ pub fn extract_current_version(versions: Vec) -> MLE { } } -pub enum MCVersionType { - Release, - Latest, - Specific, -} +pub async fn get_game_versions() -> Vec { + let data = get("https://api.modrinth.com/v2/", "tag/game_version").await.unwrap().unwrap(); -#[derive(Debug, Deserialize)] -pub struct MCVersion { - pub version: String, - pub version_type: String, - pub date: String, - pub major: bool, -} - -pub async fn get_minecraft_version(api: &str, version: MCVersionType) -> String { - let data = get(api, String::from("tag/game_version")) - .await - .unwrap() - .unwrap(); - let mc_versions: Vec = serde_json::from_slice(&data).unwrap(); - let ver = match version { - MCVersionType::Release => { - let mut i = 0; - while !mc_versions[i].major { - i += 1; - } - &mc_versions[i] - } - MCVersionType::Latest => &mc_versions[0], - MCVersionType::Specific => { - println!("Not inplemented"); - &mc_versions[0] - } - }; - String::from(&ver.version) + serde_json::from_slice(&data).unwrap() } -- cgit v1.2.3