mcapi 0.3.1

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
use serde::{Deserialize, Serialize};

/// Skin or cape model kind in Blessing Skin.
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TextureModel {
    #[default]
    Steve,
    Alex,
    Cape,
}

impl TextureModel {
    /// Returns the wire value (`"steve"`, `"alex"`, or `"cape"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            TextureModel::Steve => "steve",
            TextureModel::Alex => "alex",
            TextureModel::Cape => "cape",
        }
    }
}

/// Filter kind for `GET /skinlib/list`.
#[derive(Clone, Copy, Debug, Default, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SkinlibFilter {
    #[default]
    Skin,
    Steve,
    Alex,
    Cape,
}

/// Sort mode for `GET /skinlib/list`.
#[derive(Clone, Copy, Debug, Default, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SkinlibSort {
    #[default]
    Time,
    Likes,
}

/// Fluent query builder for `GET /skinlib/list`.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
pub struct SkinlibQuery {
    pub filter: Option<SkinlibFilter>,
    pub sort: Option<SkinlibSort>,
    pub keyword: Option<String>,
    pub uploader: Option<u64>,
    pub page: Option<u32>,
}

impl SkinlibQuery {
    /// Creates an empty [`SkinlibQuery`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters by texture type (`skin`, `steve`, `alex`, or `cape`).
    #[must_use]
    pub fn filter(mut self, filter: SkinlibFilter) -> Self {
        self.filter = Some(filter);
        self
    }

    /// Sets the sort field (`time` or `likes`).
    #[must_use]
    pub fn sort(mut self, sort: SkinlibSort) -> Self {
        self.sort = Some(sort);
        self
    }

    /// Filters textures by search keyword.
    #[must_use]
    pub fn keyword(mut self, keyword: impl Into<String>) -> Self {
        self.keyword = Some(keyword.into());
        self
    }

    /// Filters textures uploaded by a specific user ID.
    #[must_use]
    pub fn uploader(mut self, uid: u64) -> Self {
        self.uploader = Some(uid);
        self
    }

    /// Sets the 1-based page number.
    #[must_use]
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }
}

/// Summary texture entry in `GET /skinlib/list`.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct SkinlibItem {
    pub tid: u64,
    pub name: String,
    #[serde(rename = "type")]
    pub texture_type: TextureModel,
    pub uploader: u64,
    pub public: bool,
    #[serde(default)]
    pub likes: u64,
    #[serde(default)]
    pub hash: Option<String>,
}

/// Detailed texture entry returned by `GET /skinlib/info/{tid}`.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct TextureDetail {
    pub tid: u64,
    pub name: String,
    #[serde(rename = "type")]
    pub texture_type: TextureModel,
    pub hash: String,
    pub size: u64,
    pub uploader: u64,
    pub public: bool,
    #[serde(default)]
    pub likes: u64,
    #[serde(default)]
    pub upload_at: Option<String>,
}

/// Multipart upload request for `POST /api/skinlib`.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UploadTextureRequest {
    pub name: String,
    pub texture_type: TextureModel,
    pub public: bool,
    pub file_name: String,
    pub png_bytes: Vec<u8>,
}

impl UploadTextureRequest {
    /// Creates an upload request (defaulting `public` visibility to `true`).
    #[must_use]
    pub fn new(
        name: impl Into<String>,
        texture_type: TextureModel,
        file_name: impl Into<String>,
        png_bytes: Vec<u8>,
    ) -> Self {
        Self {
            name: name.into(),
            texture_type,
            public: true,
            file_name: file_name.into(),
            png_bytes,
        }
    }

    /// Sets whether the uploaded texture is publicly listed in the skin library.
    #[must_use]
    pub fn public(mut self, public: bool) -> Self {
        self.public = public;
        self
    }
}

/// Uploaded texture summary returned inside `POST /api/skinlib`.
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct UploadedTexture {
    pub tid: u64,
    pub name: String,
    #[serde(rename = "type")]
    pub texture_type: TextureModel,
    pub hash: String,
    pub public: bool,
}

/// Texture slot on a player character (`"skin"` or `"cape"`).
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TextureSlot {
    /// Player skin slot (`"skin"`).
    Skin,
    /// Player cape slot (`"cape"`).
    Cape,
}

/// Fluent query builder for `GET /api/closet`.
#[non_exhaustive]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
pub struct ClosetQuery {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<ClosetCategory>,
    #[serde(rename = "q", skip_serializing_if = "Option::is_none")]
    pub keyword: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<u32>,
}

impl ClosetQuery {
    /// Creates an empty [`ClosetQuery`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Filters closet items by category (`skin` or `cape`).
    #[must_use]
    pub fn category(mut self, category: ClosetCategory) -> Self {
        self.category = Some(category);
        self
    }

    /// Filters closet items by search keyword (`q`).
    #[must_use]
    pub fn keyword(mut self, keyword: impl Into<String>) -> Self {
        self.keyword = Some(keyword.into());
        self
    }

    /// Sets the 1-based page number.
    #[must_use]
    pub fn page(mut self, page: u32) -> Self {
        self.page = Some(page);
        self
    }
}

/// Closet category filter (`"skin"` or `"cape"`).
#[derive(Clone, Copy, Debug, Default, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ClosetCategory {
    #[default]
    Skin,
    Cape,
}

/// Texture entry stored in a user's closet (`GET /api/closet`).
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct ClosetItem {
    pub tid: u64,
    pub name: String,
    #[serde(rename = "type")]
    pub texture_type: TextureModel,
    pub hash: String,
    #[serde(default)]
    pub pivot: Option<serde_json::Value>,
}