mcapi 0.3.0

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

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

/// Known platform keys in Mojang's `java-runtime/all.json`.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MojangPlatform {
    Linux,
    LinuxI386,
    MacOs,
    MacOsArm64,
    WindowsX64,
    WindowsX86,
    WindowsArm64,
}

impl MojangPlatform {
    /// Returns the Mojang `all.json` platform key string.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            MojangPlatform::Linux => "linux",
            MojangPlatform::LinuxI386 => "linux-i386",
            MojangPlatform::MacOs => "mac-os",
            MojangPlatform::MacOsArm64 => "mac-os-arm64",
            MojangPlatform::WindowsX64 => "windows-x64",
            MojangPlatform::WindowsX86 => "windows-x86",
            MojangPlatform::WindowsArm64 => "windows-arm64",
        }
    }
}

/// Top-level `all.json` index mapping platform key → component name → runtime entries.
#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangJavaRuntimes {
    #[serde(flatten)]
    pub platforms: HashMap<String, HashMap<String, Vec<MojangRuntimeEntry>>>,
}

impl MojangJavaRuntimes {
    /// Looks up runtime entries for `platform` (e.g., `"windows-x64"`) and `component`
    /// (e.g., `"java-runtime-gamma"`).
    #[must_use]
    pub fn for_platform_and_component(
        &self,
        platform: &str,
        component: &str,
    ) -> Option<&[MojangRuntimeEntry]> {
        self.platforms
            .get(platform)?
            .get(component)
            .map(Vec::as_slice)
    }
}

/// Single Java runtime entry inside [`MojangJavaRuntimes`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangRuntimeEntry {
    pub availability: MojangAvailability,
    pub manifest: MojangManifestRef,
    pub version: MojangRuntimeVersion,
}

/// Availability metadata for a Mojang Java runtime entry.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangAvailability {
    pub group: u32,
    pub progress: u32,
}

/// Manifest reference pointing to the runtime's file list JSON.
pub type MojangManifestRef = MojangRuntimeArtifact;

/// Version label and release timestamp for a Mojang Java runtime entry.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangRuntimeVersion {
    pub name: String,
    pub released: String,
}

/// Complete per-runtime file manifest fetched from [`MojangManifestRef::url`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangRuntimeFileManifest {
    pub files: HashMap<String, MojangRuntimeFile>,
}

/// Individual file, directory, or symlink entry inside [`MojangRuntimeFileManifest`].
#[non_exhaustive]
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum MojangRuntimeFile {
    File {
        downloads: Box<MojangRuntimeDownloads>,
        #[serde(default)]
        executable: bool,
    },
    Directory {},
    Link {
        target: String,
    },
}

/// Download variants (`raw` and optional `lzma`) for a Mojang runtime file.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangRuntimeDownloads {
    pub raw: MojangRuntimeArtifact,
    #[serde(default)]
    pub lzma: Option<MojangRuntimeArtifact>,
}

/// Download URL, SHA-1, and size for a Mojang runtime artifact.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct MojangRuntimeArtifact {
    pub sha1: String,
    pub size: u64,
    pub url: Url,
}