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 From 245b6b9926033acd750503ab4947c9514c5e1aa6 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 3 May 2023 08:01:51 +0200 Subject: fuck serde, plus maybe added quilt --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/apis/modrinth.rs | 9 ++--- src/lib.rs | 96 ++++++---------------------------------------------- 4 files changed, 15 insertions(+), 94 deletions(-) (limited to 'src/apis') diff --git a/Cargo.lock b/Cargo.lock index 0e93641..7422038 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,7 +798,7 @@ dependencies = [ [[package]] name = "modlist" -version = "0.14.0" +version = "0.14.1" dependencies = [ "chrono", "clap", diff --git a/Cargo.toml b/Cargo.toml index 736831d..bd41811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "modlist" -version = "0.14.0" +version = "0.14.1" edition = "2021" # 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}; use crate::{ error::{ErrorType, MLError, MLE}, - List, Modloader, + List, }; #[derive(Debug, Deserialize, Clone)] @@ -168,14 +168,9 @@ pub async fn projects(api: &str, ids: Vec) -> Vec { ///Get applicable versions from mod_id with list context pub async fn versions(api: &str, id: String, list: List) -> Vec { - let loaderstr = match list.modloader { - Modloader::Forge => String::from("forge"), - Modloader::Fabric => String::from("fabric"), - }; - let url = format!( r#"project/{}/version?loaders=["{}"]&game_versions=["{}"]"#, - id, loaderstr, list.mc_version + id, list.modloader.to_string(), list.mc_version ); 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::*; use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; pub use commands::*; use error::{ErrorType, MLError, MLE}; -use serde::{Deserialize, Serialize, de::Visitor}; +use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub enum Modloader { + #[serde(rename(serialize = "fabric", deserialize = "fabric"))] Fabric, + #[serde(rename(serialize = "forge", deserialize = "forge"))] Forge, + #[serde(rename(serialize = "quilt", deserialize = "quilt"))] + Quilt, } impl Modloader { @@ -35,53 +39,16 @@ impl Display for Modloader { match self { Modloader::Fabric => write!(f, "fabric"), Modloader::Forge => write!(f, "forge"), + Modloader::Quilt => write!(f, "quilt"), } } } -impl Serialize for Modloader { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer { - match self { - Modloader::Fabric => serializer.serialize_str("fabric"), - Modloader::Forge => serializer.serialize_str("forge"), - } - } -} - -impl<'de> Deserialize<'de> for Modloader { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct FieldVisitor; - - impl<'de> Visitor<'de> for FieldVisitor { - type Value = Modloader; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("`fabric`, `forge` or `quilt`") - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, { - match v { - "fabric" => Ok(Modloader::Fabric), - "forge" => Ok(Modloader::Forge), - _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"])) - } - } - } - - deserializer.deserialize_identifier(FieldVisitor) - } -} - -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub enum VersionLevel { + #[serde(rename(serialize = "release", deserialize = "release"))] Release, + #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))] Snapshot, Version(String) } @@ -101,7 +68,7 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { Ok(()) } -/// Loads game versions +/// Loads game versions from file pub fn load_game_versions(path: &str) -> MLE> { let mut file = File::open(path)?; let mut data = String::new(); @@ -145,44 +112,3 @@ impl VersionLevel { } } } - -impl Serialize for VersionLevel { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer { - match self { - VersionLevel::Release => serializer.serialize_str("release"), - VersionLevel::Snapshot => serializer.serialize_str("snapshot"), - VersionLevel::Version(v) => serializer.serialize_str(v), - } - } -} - -impl<'de> Deserialize<'de> for VersionLevel { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct FieldVisitor; - - impl<'de> Visitor<'de> for FieldVisitor { - type Value = VersionLevel; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("`fabric`, `forge` or `quilt`") - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, { - match v { - "release" => Ok(VersionLevel::Release), - "snapshot" => Ok(VersionLevel::Snapshot), - _ => Ok(VersionLevel::Version(String::from(v))) - } - } - } - - deserializer.deserialize_identifier(FieldVisitor) - } -} -- cgit v1.2.3