feat: fetch game versions from loader APIs

This commit is contained in:
objz committed 2025-08-30 15:08:22 +02:00
1 parent 173250c53f
commit 5177f078a9
7 files changed
+320 -13

No files matched your search

+54
View File
@@ -2,6 +2,7 @@ use std::path::Path;
use serde::Deserialize;
use crate::instance::loader::GameVersion;
use crate::net::{download_file, HttpClient, NetError};
use crate::tui::progress::set_sub_action;
@@ -20,6 +21,12 @@ pub struct FabricVersion {
pub stable: bool,
}
#[derive(Debug, Clone, Deserialize)]
pub struct FabricGameVersion {
pub version: String,
pub stable: bool,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FabricProfile {
@@ -36,6 +43,41 @@ pub struct FabricLibrary {
pub async fn fetch_fabric_game_versions(
client: &HttpClient,
) -> Result<Vec<GameVersion>, NetError> {
let url = format!("{}/versions/game", FABRIC_META_BASE);
let response = match client.inner().get(&url).send().await {
Ok(r) => r,
Err(e) => {
tracing::error!("Fabric game versions GET {} failed: {}", url, e);
return Err(NetError::Http(e));
}
};
if !response.status().is_success() {
let status = response.status().as_u16();
tracing::error!("HTTP {} for {}", status, url);
return Err(NetError::StatusError { status, url });
}
let versions: Vec<FabricGameVersion> = match response.json().await {
Ok(v) => v,
Err(e) => {
tracing::error!("Failed to parse Fabric game versions: {}", e);
return Err(NetError::Http(e));
}
};
Ok(versions
.into_iter()
.map(|version| GameVersion {
id: version.version,
stable: version.stable,
})
.collect())
}
pub async fn fetch_fabric_versions(
client: &HttpClient,
game_version: &str,
@@ -181,6 +223,18 @@ mod tests {
}
}
#[tokio::test]
async fn test_fetch_game_versions() {
let client = HttpClient::new();
match fetch_fabric_game_versions(&client).await {
Ok(versions) => {
assert!(!versions.is_empty(), "Should have Fabric game versions");
assert!(versions.iter().any(|version| version.id == "1.20.1"));
}
Err(e) => assert!(false, "fetch_fabric_game_versions failed: {}", e),
}
}
#[test]
fn test_maven_coord_to_path() {
assert_eq!(