mcapi 0.3.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
//! Shared Minecraft profile property and decoded `textures` payload models
//! used by both Mojang session servers and Yggdrasil / authlib-injector servers.

use std::collections::HashMap;

use base64::{Engine as _, engine::general_purpose::STANDARD};
use serde::{Deserialize, Serialize};
use url::Url;

use crate::{Error, McUuid, Result};

/// Key-value property attached to a Minecraft profile, optionally RSA-signed.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ProfileProperty {
    pub name: String,
    pub value: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
}

/// Decoded contents of a Base64 `textures` [`ProfileProperty`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TexturesPayload {
    pub timestamp: u64,
    pub profile_id: McUuid,
    pub profile_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signature_required: Option<bool>,
    #[serde(default)]
    pub textures: HashMap<TextureType, TextureEntry>,
}

/// Texture slot kind inside [`TexturesPayload`].
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum TextureType {
    Skin,
    Cape,
}

impl TextureType {
    /// Returns the lowercase URL path segment (`"skin"` or `"cape"`).
    #[must_use]
    pub const fn as_path_segment(self) -> &'static str {
        match self {
            TextureType::Skin => "skin",
            TextureType::Cape => "cape",
        }
    }
}

/// Single texture entry inside [`TexturesPayload`].
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextureEntry {
    pub url: Url,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<TextureMetadata>,
}

/// Optional metadata attached to a [`TextureEntry`] (such as `"slim"` skin model).
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextureMetadata {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
}

/// Decodes a Base64-encoded `textures` [`ProfileProperty`] value into [`TexturesPayload`].
pub fn decode_textures_value(value: &str) -> Result<TexturesPayload> {
    let decoded = STANDARD.decode(value.as_bytes()).map_err(Error::base64)?;
    Ok(serde_json::from_slice(&decoded)?)
}

/// Finds and decodes the `"textures"` entry in `properties`, returning `Ok(None)` if absent.
pub fn decode_textures_property(properties: &[ProfileProperty]) -> Result<Option<TexturesPayload>> {
    let Some(prop) = properties.iter().find(|p| p.name == "textures") else {
        return Ok(None);
    };
    decode_textures_value(&prop.value).map(Some)
}

#[cfg(any(feature = "mojang", feature = "yggdrasil"))]
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct JoinServerBody<'a> {
    pub access_token: &'a str,
    pub selected_profile: McUuid,
    pub server_id: &'a str,
}