brawl-rs 0.1.0

A Rust wrapper for the Brawl Stars API
Documentation
use crate::client::endpoints::Endpoint;
use crate::parse_date;
use crate::{BrawlClient, BrawlError};
use chrono::{DateTime, Utc};
use serde::Deserialize;

/// Collection of battle records for a player.
///
/// Contains a list of recent battles played by a player, including match details,
/// results, and participant information.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleLog {
    pub items: Vec<Battle>,
}

/// A single battle record.
///
/// Represents one completed match with all its details including timing,
/// event information, and battle results.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Battle {
    #[serde(deserialize_with = "parse_date")]
    pub battle_time: DateTime<Utc>,
    pub event: BattleEvent,
    pub battle: BattleInfo,
}

/// Information about the event/map where the battle took place.
///
/// Contains details about the game mode and map configuration for the battle.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleEvent {
    pub id: u64,
    pub mode: Option<String>,
    pub mode_id: u8,
    pub map: Option<String>,
}

/// Detailed information about the battle outcome.
///
/// Includes the game mode, result, ranking, trophy changes, and all participants.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleInfo {
    pub mode: String,
    #[serde(rename = "type")]
    pub kind: String,
    pub result: Option<String>,
    pub duration: Option<u16>,
    pub rank: Option<u8>,
    pub trophy_change: Option<i8>,
    #[serde(default)]
    pub players: Vec<BattlePlayer>,
    pub teams: Option<Vec<Vec<BattlePlayer>>>,
    pub star_player: Option<BattlePlayer>,
}

/// Information about a player who participated in the battle.
///
/// Contains the player's tag, name, and their chosen brawler for the match.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattlePlayer {
    pub tag: String,
    pub name: String,
    pub brawler: BattleBrawler,
}

/// Information about the brawler used in battle.
///
/// Contains the brawler's identity, power level, and trophy count at the time of battle.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BattleBrawler {
    pub id: u64,
    pub name: String,
    pub power: u8,
    pub trophies: u32,
}

impl BattleLog {
    /// Fetches the battle log for a specific player.
    ///
    /// Retrieves the recent battle history for a player by their tag.
    /// The battle log contains detailed information about each match including
    /// results, participants, and game statistics.
    ///
    /// # 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
    /// - JSON deserialization 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 battle_log = BattleLog::get(&client, "20YY0G9L0").await?;
    ///
    ///     println!("Found {} battles", battle_log.items.len());
    ///     for battle in &battle_log.items {
    ///         println!("Battle at: {}", battle.battle_time);
    ///     }
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn get(client: &BrawlClient, tag: &str) -> Result<Self, BrawlError> {
        let endpoint = Endpoint::BattleLog(tag.to_string());

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