mudra-cli 0.1.0

A robust, high-performance currency converter with caching and CLI interface
Documentation
//! Error types for the currency converter application

use thiserror::Error;

/// Custom error type for currency converter operations
#[derive(Error, Debug)]
pub enum CurrencyError {
    /// Network-related errors (timeouts, connection failures)
    #[error("Network error: {0}")]
    Network(#[from] reqwest::Error),

    /// JSON parsing errors
    #[error("JSON parsing error: {0}")]
    Json(#[from] serde_json::Error),

    /// API-specific errors (invalid currency codes, rate limits)
    #[error("API error: {message}")]
    Api { message: String },

    /// Invalid currency code
    #[error("Invalid currency code: {code}")]
    InvalidCurrency { code: String },

    /// Invalid amount (negative, NaN, etc.)
    #[error("Invalid amount: {amount}")]
    InvalidAmount { amount: f64 },

    /// Configuration errors (missing API key, invalid settings)
    #[error("Configuration error: {message}")]
    Configuration { message: String },

    /// Generic conversion errors
    #[error("Conversion error: {message}")]
    Conversion { message: String },
}

impl CurrencyError {
    /// Create a new API error
    pub fn api<S: Into<String>>(message: S) -> Self {
        CurrencyError::Api {
            message: message.into(),
        }
    }

    /// Create a new configuration error
    pub fn configuration<S: Into<String>>(message: S) -> Self {
        CurrencyError::Configuration {
            message: message.into(),
        }
    }

    /// Create a new conversion error
    pub fn conversion<S: Into<String>>(message: S) -> Self {
        CurrencyError::Conversion {
            message: message.into(),
        }
    }

    /// Create an invalid currency error
    pub fn invalid_currency<S: Into<String>>(code: S) -> Self {
        CurrencyError::InvalidCurrency { code: code.into() }
    }

    /// Create an invalid amount error
    pub fn invalid_amount(amount: f64) -> Self {
        CurrencyError::InvalidAmount { amount }
    }
}

/// Type alias for Results using our custom error type
/// This is a common Rust pattern - makes code cleaner
pub type Result<T> = std::result::Result<T, CurrencyError>;