use std::collections::HashMap; use reqwest::Client; use serde::Deserialize; #[derive(Deserialize)] struct ForgeVersionResponse { promos: HashMap, } /// Queries the newest version of forge for a given minecraft version from the /// forge api pub async fn newest_forge_version( http: &Client, mcversion: &str, ) -> anyhow::Result> { let resp = http .get("https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json") .send() .await? .bytes() .await?; let mut resp = json5::from_str::(std::str::from_utf8(&resp)?)?; Ok(resp.promos.remove(&format!("{}-latest", mcversion))) } /// Parses the strange forge version format found in modpack.json files to the /// actual forge version number which is used in other places such as the twitch /// manifest. #[inline] pub fn parse_version(version: &str) -> Option<&str> { version.split('-').nth(1).map(|s| s.trim_end_matches(']')) } #[cfg(test)] mod tests { use super::*; #[test] fn parse_version_valid() { assert_eq!(parse_version("[1.16.5-420.69-1.16.5]"), Some("420.69")); assert_eq!(parse_version("[1.16.5-420.69]"), Some("420.69")); } }