From 92bda65a9983e60036b3d49333e9bfe9bcd0543f Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 11 Jan 2023 17:21:08 +0100 Subject: begin of mc_version api impl --- data.db | 0 src/apis/modrinth.rs | 29 +++++++++++++++++++++++++++++ src/commands/list.rs | 8 ++++---- 3 files changed, 33 insertions(+), 4 deletions(-) delete mode 100644 data.db diff --git a/data.db b/data.db deleted file mode 100644 index e69de29..0000000 diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 3880fa0..78073e6 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -194,3 +194,32 @@ pub fn extract_current_version(versions: Vec) -> Result panic!("available_versions should never be negative"), } } + +pub enum MCVersionType { + Release, + Latest, +} + +#[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: String, 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], + }; + String::from(&ver.version) +} diff --git a/src/commands/list.rs b/src/commands/list.rs index 526b434..a02f8b1 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -1,6 +1,6 @@ use std::io::{Error, ErrorKind}; -use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}}; +use crate::{db::{lists_insert, lists_remove, config_change_current_list, lists_get_all_ids, config_get_current_list, lists_get, lists_version}, Modloader, config::Cfg, input::{Input, Subcmd}, cmd_update, error::{MLE, ErrorType, MLError}, modrinth::MCVersionType}; #[derive(Debug, Clone, PartialEq, Eq)] pub struct List { @@ -26,7 +26,7 @@ pub async fn list(config: Cfg, input: Input) -> Result<(), Box { - match version(config, input.args.ok_or("NO_VERSION")?).await { + match version(config, Some(input.args.ok_or("NO_VERSION")?), Some(MCVersionType::Release)).await { Ok(..) => Ok(()), Err(e) => Err(Box::new(e)) } @@ -85,10 +85,10 @@ fn remove(config: Cfg, args: Vec) -> Result<(), Box) -> MLE<()> { +async fn version(config: Cfg, args: Option>, version_type: Option) -> MLE<()> { let current_list = lists_get(config.clone(), config_get_current_list(config.clone())?)?; - lists_version(config.clone(), String::from(¤t_list.id), String::from(&args[0]))?; + lists_version(config.clone(), String::from(¤t_list.id), String::from(&args.unwrap()[0]))?; //update the list & with -- args cmd_update(config, vec![current_list], true, true, false).await } -- cgit v1.2.3