//! Error types for cachewrap

use std::fmt;

/// Error type for cache operations
#[derive(Debug)]
pub enum CacheError {
    /// Redis connection error
    ConnectionError(String),

    /// Redis command error
    CommandError(String),

    /// Serialization/deserialization error
    SerializationError(String),

    /// Invalid configuration
    ConfigError(String),

    /// Timeout error
    TimeoutError(String),
}

impl fmt::Display for CacheError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CacheError::ConnectionError(msg) => write!(f, "Connection error: {}", msg),
            CacheError::CommandError(msg) => write!(f, "Command error: {}", msg),
            CacheError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
            CacheError::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
            CacheError::TimeoutError(msg) => write!(f, "Timeout error: {}", msg),
        }
    }
}

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

impl From<redis::RedisError> for CacheError {
    fn from(err: redis::RedisError) -> Self {
        CacheError::CommandError(err.to_string())
    }
}

impl From<serde_json::Error> for CacheError {
    fn from(err: serde_json::Error) -> Self {
        CacheError::SerializationError(err.to_string())
    }
}

/// Result type alias for cache operations
pub type Result<T> = std::result::Result<T, CacheError>;
