// SPDX-FileCopyrightText: 2026 Constantin Bauer // SPDX-License-Identifier: GPL-3.0-only use std::collections::HashSet; use std::io; use clap::ArgMatches; use crate::cli::instance::parse_loader; use crate::cli::output::print_table; use crate::instance::ModLoader; use crate::net::HttpClient; use crate::net::mojang::{VersionEntry, VersionManifest}; type CliResult = Result<(), Box>; pub async fn handle_version(matches: &ArgMatches) -> CliResult { match matches.subcommand() { Some(("list", sub_matches)) => list_versions(sub_matches).await, _ => Ok(()), } } async fn list_versions(matches: &ArgMatches) -> CliResult { let client = HttpClient::new(); let manifest = crate::net::mojang::fetch_version_manifest(&client).await?; let snapshots = matches.get_flag("snapshots"); let loader = matches .get_one::("loader") .map(|value| parse_loader(value).map_err(io::Error::other)) .transpose()?; // vanilla supports everything by definition, so only fetch loader-specific // version lists when the user actually asked for a modded loader let supported = match loader { Some(ModLoader::Vanilla) | None => None, Some(loader) => Some(fetch_supported_versions(&client, loader).await?), }; let rows = filter_manifest_versions(&manifest, supported.as_ref(), snapshots) .into_iter() .map(|entry| vec![entry.id, entry.version_type]) .collect::>(); print_table(&["Version", "Type"], &rows); Ok(()) } async fn fetch_supported_versions( client: &HttpClient, loader: ModLoader, ) -> Result, Box> { let versions = crate::instance::loader::get_installer(loader) .get_game_versions(client) .await? .into_iter() .map(|version| version.id) .collect(); Ok(versions) } fn filter_manifest_versions( manifest: &VersionManifest, supported: Option<&HashSet>, snapshots: bool, ) -> Vec { manifest .versions .iter() .filter(|entry| snapshots || entry.version_type == "release") .filter(|entry| { supported .map(|versions| versions.contains(&entry.id)) .unwrap_or(true) }) .cloned() .collect() } #[cfg(test)] #[path = "tests/version.rs"] mod tests;