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 crate::uuid::McUuid;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use url::Url;

/// Active state of a skin or cape item in Minecraft Services.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum SkinState {
    Active,
    Inactive,
}

/// Skin arm geometry model expected by Minecraft Services.
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SkinVariant {
    #[default]
    Classic,
    Slim,
}

impl SkinVariant {
    /// Returns the wire value (`"classic"` or `"slim"`).
    pub const fn as_str(self) -> &'static str {
        match self {
            SkinVariant::Classic => "classic",
            SkinVariant::Slim => "slim",
        }
    }
}

/// Skin entry attached to [`MinecraftProfile`].
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct SkinInfo {
    pub id: String,
    pub state: SkinState,
    pub url: Url,
    pub variant: SkinVariant,
    #[serde(default)]
    pub texture_key: Option<String>,
}

/// Cape entry attached to [`MinecraftProfile`].
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CapeInfo {
    pub id: String,
    pub state: SkinState,
    pub url: Url,
    #[serde(default)]
    pub alias: Option<String>,
}

/// Authenticated player's Minecraft Services profile returned by `GET /minecraft/profile`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MinecraftProfile {
    pub id: McUuid,
    pub name: String,
    #[serde(default)]
    pub skins: Vec<SkinInfo>,
    #[serde(default)]
    pub capes: Vec<CapeInfo>,
    #[serde(default)]
    pub profile_actions: serde_json::Value,
}

/// Entitlements response returned by `GET /entitlements/mcstore`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EntitlementsResponse {
    #[serde(default)]
    pub items: Vec<EntitlementItem>,
    #[serde(default)]
    pub signature: Option<String>,
    #[serde(default)]
    pub key_id: Option<String>,
}

impl EntitlementsResponse {
    /// Returns `true` if the entitlements contain Minecraft: Java Edition (`product_minecraft` or `game_minecraft`).
    pub fn owns_minecraft_java(&self) -> bool {
        self.items
            .iter()
            .any(|item| matches!(item.name.as_str(), "product_minecraft" | "game_minecraft"))
    }
}

/// Single item in [`EntitlementsResponse`].
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EntitlementItem {
    pub name: String,
    #[serde(default)]
    pub signature: Option<String>,
}

/// Name change eligibility metadata returned by `GET /minecraft/profile/namechange`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NameChangeInfo {
    pub changed_at: Option<DateTime<Utc>>,
    pub created_at: Option<DateTime<Utc>>,
    pub name_change_allowed: bool,
}

/// Availability status for a candidate Minecraft username.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum NameAvailabilityStatus {
    Available,
    Duplicate,
    NotAllowed,
    #[serde(other)]
    Unknown,
}

/// Response returned by `GET /minecraft/profile/name/{name}/available`.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct NameAvailability {
    pub status: NameAvailabilityStatus,
}

/// Player privilege toggles returned by `GET /player/attributes`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerAttributes {
    #[serde(default)]
    pub privileges: serde_json::Value,
    #[serde(default)]
    pub profanity_filter_preferences: serde_json::Value,
    #[serde(default)]
    pub ban_status: serde_json::Value,
}

/// Player chat signing certificate pair returned by `POST /player/certificates`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PlayerCertificates {
    pub key_pair: KeyPairPem,
    pub public_key_signature: String,
    #[serde(default)]
    pub public_key_signature_v2: Option<String>,
    pub expires_at: DateTime<Utc>,
    pub refreshed_after: DateTime<Utc>,
}

/// RSA key pair returned inside [`PlayerCertificates`].
#[derive(Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct KeyPairPem {
    pub private_key: String,
    pub public_key: String,
}

impl std::fmt::Debug for KeyPairPem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("KeyPairPem")
            .field("private_key", &"[REDACTED]")
            .field("public_key", &self.public_key)
            .finish()
    }
}