mcapi 0.2.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, Java Runtimes, and SLP)
Documentation
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use url::Url;

use crate::error::Result;

/// Forge `promotions_slim.json` payload mapping `"<mc>-recommended"` / `"<mc>-latest"`
/// to Forge build numbers.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ForgePromotions {
    #[serde(default)]
    pub homepage: Option<String>,
    #[serde(default)]
    pub promos: HashMap<String, String>,
}

impl ForgePromotions {
    /// Returns the recommended Forge version for `mc_version`, if present.
    pub fn recommended_for(&self, mc_version: &str) -> Option<&str> {
        self.promos
            .get(&format!("{mc_version}-recommended"))
            .map(String::as_str)
    }

    /// Returns the latest Forge version for `mc_version`, if present.
    pub fn latest_for(&self, mc_version: &str) -> Option<&str> {
        self.promos
            .get(&format!("{mc_version}-latest"))
            .map(String::as_str)
    }

    /// Returns the recommended Forge version for `mc_version`, falling back to the latest
    /// version if no recommended build is listed.
    pub fn recommended_or_latest(&self, mc_version: &str) -> Option<&str> {
        self.recommended_for(mc_version)
            .or_else(|| self.latest_for(mc_version))
    }
}

/// Parsed Forge Maven artifact version (`<mc_version>-<forge_version>[-<branch>]`).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ForgeArtifactVersion {
    pub raw: String,
    pub mc_version: String,
    pub forge_version: String,
    pub branch: Option<String>,
}

impl ForgeArtifactVersion {
    /// Parses a Forge Maven version string such as `"1.20.1-47.2.0"` or `"1.7.10-10.13.4.1614-1.7.10"`.
    pub fn parse(raw: &str) -> Option<Self> {
        let mut parts = raw.splitn(3, '-');
        let mc_version = parts.next()?.to_string();
        let forge_version = parts.next()?.to_string();
        let branch = parts.next().map(str::to_string);
        Some(Self {
            raw: raw.to_string(),
            mc_version,
            forge_version,
            branch,
        })
    }

    /// Builds the canonical Forge installer JAR URL on Forge Maven, preserving any
    /// legacy branch suffix.
    pub fn installer_url(&self) -> Result<Url> {
        crate::loaders::forge_installer_url_with_branch(
            &self.mc_version,
            &self.forge_version,
            self.branch.as_deref(),
        )
    }
}

/// Forge build entry returned by BMCLAPI `/forge/minecraft/{mcversion}`.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BmclapiForgeEntry {
    #[serde(rename = "_id", default)]
    pub id: Option<String>,
    pub build: u32,
    pub mcversion: String,
    pub version: String,
    #[serde(default)]
    pub branch: Option<String>,
    #[serde(default)]
    pub modified: Option<String>,
    #[serde(default)]
    pub files: Vec<BmclapiForgeFile>,
}

/// Individual downloadable artifact entry inside [`BmclapiForgeEntry`].
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct BmclapiForgeFile {
    pub format: String,
    pub category: String,
    #[serde(default)]
    pub hash: Option<String>,
}