use thiserror::Error;
#[cfg(feature = "serde")]
use serde_json;
pub type Result<T> = std::result::Result<T, CachifiedError>;
#[derive(Error, Debug)]
pub enum CachifiedError {
#[error("Failed to get fresh value: {0}")]
FreshValueError(String),
#[error("Cache validation failed: {0}")]
ValidationError(String),
#[error("Cache operation failed: {0}")]
CacheError(String),
#[error("Cachified error: {0}")]
Other(String),
}
impl CachifiedError {
pub fn fresh_value<S: Into<String>>(msg: S) -> Self {
CachifiedError::FreshValueError(msg.into())
}
pub fn validation<S: Into<String>>(msg: S) -> Self {
CachifiedError::ValidationError(msg.into())
}
pub fn cache<S: Into<String>>(msg: S) -> Self {
CachifiedError::CacheError(msg.into())
}
pub fn other<S: Into<String>>(msg: S) -> Self {
CachifiedError::Other(msg.into())
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for CachifiedError {
fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
CachifiedError::Other(err.to_string())
}
}
impl From<String> for CachifiedError {
fn from(err: String) -> Self {
CachifiedError::Other(err)
}
}
impl From<&str> for CachifiedError {
fn from(err: &str) -> Self {
CachifiedError::Other(err.to_string())
}
}
#[cfg(feature = "redis")]
impl From<redis::RedisError> for CachifiedError {
fn from(err: redis::RedisError) -> Self {
CachifiedError::CacheError(format!("Redis error: {}", err))
}
}
#[cfg(feature = "serde")]
impl From<serde_json::Error> for CachifiedError {
fn from(err: serde_json::Error) -> Self {
CachifiedError::Other(format!("Serialization error: {}", err))
}
}