brawl-rs 0.1.0

A Rust wrapper for the Brawl Stars API
Documentation
//! Error types for the Brawl API client.
//!
//! This module provides error handling types that can occur when interacting
//! with the Brawl Stars API.

use serde::Deserialize;
use std::fmt::Formatter;

/// An error returned by the Brawl Stars API.
///
/// This struct contains error information returned by the API servers,
/// including a reason code and a descriptive message.
#[derive(Debug, Deserialize)]
pub struct ApiError {
    pub reason: Option<String>,
    pub message: Option<String>,
    #[serde(rename = "type")]
    pub kind: Option<String>,
}

/// Error type for all operations in the Brawl API client.
///
/// This enum represents all possible error conditions that can occur
/// when using this library:
///
/// - **Api** - Error returned by the Brawl Stars API (e.g., invalid player tag)
/// - **Network** - Network communication error (e.g., connection timeout)
/// - **Serialization** - JSON serialization/deserialization error
///
#[derive(Debug)]
pub enum BrawlError {
    /// An error response from the Brawl Stars API
    Api(ApiError),
    /// A network/HTTP communication error
    Network(reqwest::Error),
    /// A JSON serialization or deserialization error
    Serialization(reqwest::Error),
}

impl std::fmt::Display for BrawlError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            BrawlError::Api(e) => {
                let reason = e.reason.as_deref().unwrap_or("unknown reason");
                let message = e.message.as_deref().unwrap_or("no message provided");
                let type_ = e.kind.as_deref().unwrap_or("unknown type");

                write!(f, "API Error: {} ({}) [{}]", reason, message, type_)
            }
            BrawlError::Network(e) => write!(f, "Network Error: {e}"),
            BrawlError::Serialization(e) => write!(f, "Serialization Error: {}", e),
        }
    }
}

impl std::error::Error for BrawlError {}