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::parse_date;
use chrono::{DateTime, Utc};
use serde::Deserialize;

/// Collection of all available game modes in Brawl Stars.
///
/// Game modes are different types of gameplay experiences available in Brawl Stars,
/// each with unique mechanics and rewards.
///
/// # 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 modes = GameModes::get(&client).await?;
///     for mode in &modes.items {
///         println!("Mode: {}", mode.name.as_deref().unwrap_or("Unknown"));
///     }
///     Ok(())
/// }
/// ```
#[derive(Deserialize)]
pub struct GameModes {
    pub items: Vec<GameModeInfo>,
}

/// Information about a single game mode.
///
/// Represents a game mode available in Brawl Stars with its unique identifier
/// and display name.
#[derive(Deserialize)]
pub struct GameModeInfo {
    pub id: u8,
    pub name: Option<String>,
}

/// Current rotation of events (currently active game modes with maps).
///
/// Contains information about which events are currently active in the game,
/// including the game mode, map, and time window.
///
/// # 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 binding = Rotation::get(&client).await?;
///     let rotation = binding.first().expect("No active events found");
///
///     println!("Event: {} on map {}", rotation.event.mode, rotation.event.map);
///     println!("Active from {} to {}", rotation.start_time, rotation.end_time);
///     Ok(())
/// }
/// ```
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Rotation {
    #[serde(deserialize_with = "parse_date")]
    pub start_time: DateTime<Utc>,
    #[serde(deserialize_with = "parse_date")]
    pub end_time: DateTime<Utc>,
    pub slot_id: u8,
    pub event: EventInfo,
}

/// Details about a specific event/match type.
///
/// Contains information about the game mode and map for an event.
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventInfo {
    pub id: u64,
    pub mode: String,
    pub mode_id: u8,
    pub map: String,
    pub modifiers: Option<Vec<String>>,
}

impl GameModes {
    /// Fetches all available game modes.
    ///
    /// Retrieves a list of all game modes that are available or have been available
    /// in Brawl Stars.
    ///
    /// # 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 modes = GameModes::get(&client).await?;
    ///     println!("Available modes: {}", modes.items.len());
    ///     Ok(())
    /// }
    /// ```
    pub async fn get(client: &BrawlClient) -> Result<Self, BrawlError> {
        let endpoint = Endpoint::GameModes;

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

impl Rotation {
    /// Fetches the current event rotation.
    ///
    /// Retrieves information about which events are currently active or will soon be active.
    /// This can return multiple events in a rotation.
    ///
    /// # 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 rotations = Rotation::get(&client).await?;
    ///
    ///     if let Some(rotation) = rotations.first() {
    ///         println!("Current event: {}", rotation.event.mode);
    ///     }
    ///     Ok(())
    /// }
    /// ```
    pub async fn get(client: &BrawlClient) -> Result<Vec<Self>, BrawlError> {
        let endpoint = Endpoint::Rotation;

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