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::data::brawlers::BrawlerId;
use crate::errors::BrawlError;
use crate::models::common::{BrawlerGear, Item};
use serde::Deserialize;

/// Collection of all brawlers available in Brawl Stars.
///
/// Contains a list of all playable brawlers with their abilities,
/// power-ups, and cosmetics information.
///
/// # 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 brawlers = Brawlers::get(&client).await?;
///     println!("Total brawlers: {}", brawlers.items.len());
///     for brawler in &brawlers.items {
///         println!("- {}: {} star powers", brawler.name, brawler.star_powers.len());
///     }
///     Ok(())
/// }
/// ```
#[derive(Deserialize)]
pub struct Brawlers {
    pub items: Vec<BrawlerInfo>,
}

/// Detailed information about a single brawler.
///
/// Contains comprehensive data about a brawler including its unique abilities
/// (star powers), power-ups (hyper charges), gears, and gadgets.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BrawlerInfo {
    pub id: u64,
    pub name: String,
    pub star_powers: Vec<Item>,
    pub hyper_charges: Vec<Item>,
    pub gears: Vec<BrawlerGear>,
    pub gadgets: Vec<Item>,
}

impl Brawlers {
    /// Fetches all available brawlers in the game.
    ///
    /// Retrieves comprehensive data about every brawler including their
    /// star powers, gadgets, gears, and hyper charges.
    ///
    /// # Arguments
    ///
    /// * `client` - The [`BrawlClient`] to use for the request
    ///
    /// # Errors
    ///
    /// Returns [`BrawlError`] if:
    /// - Network communication fails
    /// - The API response cannot be parsed
    ///
    /// # 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 brawlers = Brawlers::get(&client).await?;
    ///     for brawler in &brawlers.items {
    ///         println!("{}: {}", brawler.name, brawler.id);
    ///     }
    ///     Ok(())
    /// }
    /// ```
    pub async fn get(client: &BrawlClient) -> Result<Self, BrawlError> {
        let endpoint = Endpoint::Brawlers;

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

impl BrawlerInfo {
    /// Fetches detailed information about a specific brawler by its ID.
    ///
    /// Retrieves comprehensive data about the brawler including its star powers,
    /// gadgets, gears, and hyper charges.
    ///
    /// # Arguments
    /// * `client` - The [`BrawlClient`] to use for the request
    /// * `id` - The unique identifier of the brawler to fetch
    ///
    /// # Errors
    /// Returns [`BrawlError`] if:
    /// - Network communication fails
    /// - The API response cannot be parsed
    /// - The specified brawler ID does not exist
    ///
    /// # Examples
    /// ```no_run
    /// use brawl_rs::prelude::*;
    /// use brawl_rs::data::brawlers::BrawlerId;
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let client = BrawlClient::new(std::env::var("BRAWL_TOKEN")?);
    ///     let brawler = BrawlerInfo::get(&client, BrawlerId::Shelly).await?;
    ///     println!("{} has {} star powers", brawler.name, brawler.star_powers.len());
    ///     Ok(())
    /// }
    /// ```

    pub async fn get(client: &BrawlClient, id: BrawlerId) -> Result<Self, BrawlError> {
        let endpoint = Endpoint::Brawler(id);

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