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 jiff::Timestamp;
use serde::{Deserialize, Serialize};
use url::Url;

/// Role of a Modrinth user.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum UserRole {
    Admin,
    Moderator,
    Pyro,
    Developer,
    #[serde(other)]
    Unknown,
}

/// Payout summary attached to an authenticated [`User`].
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct PayoutData {
    #[serde(default)]
    pub balance: Option<f64>,
    #[serde(default)]
    pub payout_wallet: Option<String>,
    #[serde(default)]
    pub payout_wallet_type: Option<String>,
    #[serde(default)]
    pub payout_address: Option<String>,
}

/// Modrinth user account returned by `/v2/user` and `/v2/user/{id|username}`.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct User {
    pub id: String,
    pub username: String,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub email: Option<String>,
    #[serde(default)]
    pub bio: Option<String>,
    #[serde(default)]
    pub payout_data: Option<PayoutData>,
    #[serde(default)]
    pub avatar_url: Option<Url>,
    pub created: Timestamp,
    pub role: UserRole,
    #[serde(default)]
    pub badges: u32,
    #[serde(default)]
    pub auth_providers: Option<Vec<String>>,
    #[serde(default)]
    pub email_verified: Option<bool>,
    #[serde(default)]
    pub has_password: Option<bool>,
    #[serde(default)]
    pub has_totp: Option<bool>,
}

/// Patch payload for `PATCH /v2/user/{id|username}`.
#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
#[non_exhaustive]
#[derive(Clone, Debug, Default, Serialize, Eq, PartialEq)]
pub struct ModifyUserRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bio: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub payout_data: Option<serde_json::Value>,
}

#[cfg(feature = "modrinth-write")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth-write")))]
impl ModifyUserRequest {
    /// Creates an empty [`ModifyUserRequest`].
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Updates the user's handle (`username`).
    #[must_use]
    pub fn username(mut self, username: impl Into<String>) -> Self {
        self.username = Some(username.into());
        self
    }

    /// Updates the user's display name.
    #[must_use]
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Updates the user's email address.
    #[must_use]
    pub fn email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into());
        self
    }

    /// Updates the user's profile biography.
    #[must_use]
    pub fn bio(mut self, bio: impl Into<String>) -> Self {
        self.bio = Some(bio.into());
        self
    }

    /// Updates the user's payout configuration JSON object.
    #[must_use]
    pub fn payout_data(mut self, payout_data: serde_json::Value) -> Self {
        self.payout_data = Some(payout_data);
        self
    }
}

/// Single payout transaction entry inside [`PayoutHistory`].
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct PayoutEntry {
    pub created: Timestamp,
    pub amount: f64,
    pub status: String,
}

/// Payout history returned by `GET /v2/user/{id|username}/payouts`.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
pub struct PayoutHistory {
    pub all_time: String,
    pub last_month: String,
    #[serde(default)]
    pub payouts: Vec<PayoutEntry>,
}