brawl-rs 0.1.0

A Rust wrapper for the Brawl Stars API
Documentation
use crate::client::endpoints::Endpoint;
use crate::client::BrawlClient;
use crate::errors::BrawlError;
use crate::models::common::{BrawlerGear, Item};
use serde::Deserialize;

/// Represents a Brawl Stars player's complete profile.
///
/// This struct contains comprehensive information about a player including their
/// stats, trophies, level, and all their brawlers.
///
/// # Examples
///
/// ```no_run
/// use brawl_rs::prelude::*;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let client = BrawlClient::new(std::env::var("BRAWL_TOKEN")?);
///     let player = Player::get(&client, "20YY0G9L0").await?;
///     println!("Name: {}", player.name);
///     println!("Trophies: {}", player.trophies);
///     println!("Level: {}", player.exp_level);
///     Ok(())
/// }
/// ```
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Player {
    pub tag: String,
    pub name: String,
    pub name_color: String,
    pub trophies: u64,
    pub highest_trophies: u64,
    pub total_prestige_level: u16,
    pub exp_level: u16,
    pub exp_points: u64,
    #[serde(rename = "3vs3Victories")]
    pub victories_3v3: u64,
    pub solo_victories: u64,
    pub duo_victories: u64,
    pub club: PlayerClub,
    pub brawlers: Vec<Brawler>,
}

/// Information about a player's club membership.
///
/// Contains the club tag and name if the player is currently in a club,
/// or `None` values if they are not in a club.
#[derive(Deserialize)]
pub struct PlayerClub {
    pub tag: Option<String>,
    pub name: Option<String>,
}

/// Information about a player's individual brawler.
///
/// Represents a single brawler that a player owns, including its power level,
/// trophies, rank, and cosmetics.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Brawler {
    pub id: u64,
    pub name: String,
    pub power: u8,
    pub rank: u32,
    pub trophies: u32,
    pub highest_trophies: u64,
    pub prestige_level: u16,
    pub current_win_streak: u32,
    pub max_win_streak: u32,
    pub skin: BrawlerSkin,
    #[serde(default)]
    pub gadgets: Vec<Item>,
    #[serde(default)]
    pub gears: Vec<BrawlerGear>,
    #[serde(default)]
    pub star_powers: Vec<Item>,
    #[serde(default)]
    pub hyper_charges: Vec<Item>,
    pub buffies: BrawlerBuffie,
}

/// Information about a brawler's buffies.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BrawlerBuffie {
    pub gadget: bool,
    pub star_power: bool,
    pub hyper_charge: bool,
}

/// Information about a brawler's cosmetic skin.
#[derive(Deserialize)]
pub struct BrawlerSkin {
    pub id: u64,
    pub name: String,
}

impl Player {
    /// Fetches player information by their tag.
    ///
    /// Retrieves complete profile data for a player including all their brawlers,
    /// stats, and club information.
    ///
    /// # Arguments
    ///
    /// * `client` - The [`BrawlClient`] to use for the request
    /// * `tag` - The player's tag (format: XXXX...)
    ///
    /// # Errors
    ///
    /// Returns [`BrawlError`] if:
    /// - The player tag is invalid
    /// - The player does not exist
    /// - Network communication fails
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use brawl_rs::prelude::*;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = BrawlClient::new(std::env::var("BRAWL_TOKEN")?);
    ///     let player = Player::get(&client, "20YY0G9L0").await?;
    ///     println!("Found player: {}", player.name);
    ///     Ok(())
    /// }
    /// ```
    pub async fn get(client: &BrawlClient, tag: &str) -> Result<Self, BrawlError> {
        let endpoint = Endpoint::Player(tag.to_string());

        client.fetch::<Self>(endpoint).await
    }
}