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::util::{deserialize_optional_url, serialize_option_json};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use url::Url;

use super::common::{MINECRAFT_GAME_ID, SortOrder, define_u8_enum};
use super::files::{File, FileReleaseType};

/// A CurseForge mod/project returned by `/v1/mods/{modId}` and search endpoints.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Mod {
    pub id: u32,
    pub game_id: u32,
    pub name: String,
    pub slug: String,
    pub links: ModLinks,
    pub summary: String,
    pub status: ModStatus,
    pub download_count: u64,
    pub is_featured: bool,
    pub primary_category_id: u32,
    #[serde(default)]
    pub categories: Vec<super::Category>,
    #[serde(default)]
    pub class_id: Option<u32>,
    #[serde(default)]
    pub authors: Vec<ModAuthor>,
    #[serde(default)]
    pub logo: Option<ModAsset>,
    #[serde(default)]
    pub screenshots: Vec<ModAsset>,
    pub main_file_id: u32,
    #[serde(default)]
    pub latest_files: Vec<File>,
    #[serde(default)]
    pub latest_files_indexes: Vec<FileIndex>,
    #[serde(default)]
    pub latest_early_access_files_indexes: Vec<FileIndex>,
    pub date_created: DateTime<Utc>,
    pub date_modified: DateTime<Utc>,
    pub date_released: DateTime<Utc>,
    #[serde(default)]
    pub allow_mod_distribution: Option<bool>,
    #[serde(default)]
    pub game_popularity_rank: Option<u32>,
    #[serde(default)]
    pub is_available: Option<bool>,
    #[serde(default)]
    pub thumbs_up_count: Option<u64>,
    #[serde(default)]
    pub rating: Option<f64>,
    #[serde(default)]
    pub featured_project_tag: Option<i32>,
    #[serde(default)]
    pub server_affiliation: Option<serde_json::Value>,
    #[serde(default)]
    pub social_links: Vec<serde_json::Value>,
}

/// Compact file index attached to [`Mod::latest_files_indexes`].
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FileIndex {
    pub game_version: String,
    pub file_id: u32,
    pub filename: String,
    pub release_type: FileReleaseType,
    #[serde(default)]
    pub game_version_type_id: Option<u32>,
    #[serde(default)]
    pub mod_loader: Option<ModLoaderType>,
}

/// External project links on a [`Mod`].
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModLinks {
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub website_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub wiki_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub issues_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub source_url: Option<Url>,
}

define_u8_enum! {
    /// Moderation/publication status of a [`Mod`].
    pub enum ModStatus {
        New = 1,
        ChangesRequired = 2,
        UnderSoftReview = 3,
        Approved = 4,
        Rejected = 5,
        ChangesMade = 6,
        Inactive = 7,
        Abandoned = 8,
        Deleted = 9,
        UnderReview = 10,
    }
}

/// Author information on a [`Mod`].
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModAuthor {
    pub id: u32,
    pub name: String,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub avatar_url: Option<Url>,
}

/// Logo or screenshot asset on a [`Mod`].
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModAsset {
    pub id: u32,
    pub mod_id: u32,
    pub title: String,
    pub description: String,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub thumbnail_url: Option<Url>,
    #[serde(default, deserialize_with = "deserialize_optional_url")]
    pub url: Option<Url>,
}

define_u8_enum! {
    /// Sort fields supported by `GET /v1/mods/search`.
    pub enum ModsSearchSortField {
        Featured = 1,
        Popularity = 2,
        LastUpdated = 3,
        Name = 4,
        Author = 5,
        TotalDownloads = 6,
        Category = 7,
        GameVersion = 8,
        EarlyAccess = 9,
        FeaturedReleased = 10,
        ReleasedDate = 11,
        Rating = 12,
    }
}

define_u8_enum! {
    /// Mod loader filter enum defined by CurseForge.
    pub enum ModLoaderType {
        Any = 0,
        Forge = 1,
        Cauldron = 2,
        LiteLoader = 3,
        Fabric = 4,
        Quilt = 5,
        NeoForge = 6,
    }
}

/// Fluent query builder for `GET /v1/mods/search`.
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ModSearchQuery {
    pub game_id: u32,
    pub class_id: Option<u32>,
    pub category_id: Option<u32>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_option_json"
    )]
    pub category_ids: Option<Vec<u32>>,
    pub game_version: Option<String>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_option_json"
    )]
    pub game_versions: Option<Vec<String>>,
    pub search_filter: Option<String>,
    pub sort_field: Option<ModsSearchSortField>,
    pub sort_order: Option<SortOrder>,
    pub mod_loader_type: Option<ModLoaderType>,
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_option_json"
    )]
    pub mod_loader_types: Option<Vec<ModLoaderType>>,
    pub game_version_type_id: Option<u32>,
    pub author_id: Option<u32>,
    pub primary_author_id: Option<u32>,
    pub slug: Option<String>,
    pub index: Option<u32>,
    pub page_size: Option<u32>,
}

impl ModSearchQuery {
    /// Creates a new search query scoped to `game_id`.
    pub fn new(game_id: u32) -> Self {
        Self {
            game_id,
            ..Default::default()
        }
    }

    /// Creates a new search query scoped to Minecraft (`432`).
    pub fn minecraft() -> Self {
        Self::new(MINECRAFT_GAME_ID)
    }

    /// Filters by class ID (e.g., `6` for Mods, `4471` for Modpacks).
    pub fn class_id(mut self, class_id: u32) -> Self {
        self.class_id = Some(class_id);
        self
    }

    /// Filters by a single category ID.
    pub fn category_id(mut self, category_id: u32) -> Self {
        self.category_id = Some(category_id);
        self
    }

    /// Filters by a game version string (e.g., `"1.20.1"`).
    pub fn game_version(mut self, game_version: impl Into<String>) -> Self {
        self.game_version = Some(game_version.into());
        self
    }

    /// Sets the free-text search query.
    pub fn search_filter(mut self, filter: impl Into<String>) -> Self {
        self.search_filter = Some(filter.into());
        self
    }

    /// Sets the sort field.
    pub fn sort_field(mut self, sort_field: ModsSearchSortField) -> Self {
        self.sort_field = Some(sort_field);
        self
    }

    /// Sets the sort direction.
    pub fn sort_order(mut self, sort_order: SortOrder) -> Self {
        self.sort_order = Some(sort_order);
        self
    }

    /// Filters by mod loader type.
    pub fn mod_loader_type(mut self, loader: ModLoaderType) -> Self {
        self.mod_loader_type = Some(loader);
        self
    }

    /// Filters by exact project slug.
    pub fn slug(mut self, slug: impl Into<String>) -> Self {
        self.slug = Some(slug.into());
        self
    }

    /// Sets the zero-based pagination offset.
    pub fn index(mut self, index: u32) -> Self {
        self.index = Some(index);
        self
    }

    /// Sets the page size (up to 50 per CurseForge API rules).
    pub fn page_size(mut self, page_size: u32) -> Self {
        self.page_size = Some(page_size);
        self
    }
}

/// Request body for `POST /v1/mods/featured`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FeaturedModsRequest {
    pub game_id: u32,
    #[serde(default)]
    pub excluded_mod_ids: Vec<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub game_version_type_id: Option<u32>,
}

/// Response payload inside `POST /v1/mods/featured`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FeaturedModsResponse {
    #[serde(default)]
    pub featured: Vec<Mod>,
    #[serde(default)]
    pub popular: Vec<Mod>,
    #[serde(default)]
    pub recently_updated: Vec<Mod>,
}