use std::fmt;
use std::io;
#[derive(Debug)]
pub enum CacheError {
Io(io::Error),
InvalidName(String),
ConfigParse(String),
NotFound(String),
PermissionDenied(String),
AlreadyExists(String),
Expired(String),
InvalidConfig(String),
Serialization(String),
InvalidPath(String),
SymlinkDetected(String),
SizeLimitExceeded(String),
FileCountLimitExceeded(String),
Corrupted(String),
Generic(String),
}
impl fmt::Display for CacheError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CacheError::Io(err) => write!(f, "I/O error: {}", err),
CacheError::InvalidName(msg) => write!(f, "Invalid cache name: {}", msg),
CacheError::ConfigParse(msg) => write!(f, "Configuration parse error: {}", msg),
CacheError::NotFound(msg) => write!(f, "Cache not found: {}", msg),
CacheError::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
CacheError::AlreadyExists(msg) => write!(f, "Cache already exists: {}", msg),
CacheError::Expired(msg) => write!(f, "Cache expired: {}", msg),
CacheError::InvalidConfig(msg) => write!(f, "Invalid configuration: {}", msg),
CacheError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
CacheError::InvalidPath(msg) => write!(f, "Invalid path: {}", msg),
CacheError::SymlinkDetected(msg) => write!(f, "Symbolic link detected: {}", msg),
CacheError::SizeLimitExceeded(msg) => write!(f, "Cache size limit exceeded: {}", msg),
CacheError::FileCountLimitExceeded(msg) => write!(f, "Cache file count limit exceeded: {}", msg),
CacheError::Corrupted(msg) => write!(f, "Cache corrupted: {}", msg),
CacheError::Generic(msg) => write!(f, "Error: {}", msg),
}
}
}
impl std::error::Error for CacheError {}
impl From<io::Error> for CacheError {
fn from(err: io::Error) -> Self {
CacheError::Io(err)
}
}
impl From<serde_json::Error> for CacheError {
fn from(err: serde_json::Error) -> Self {
CacheError::ConfigParse(err.to_string())
}
}
impl CacheError {
pub fn kind(&self) -> &'static str {
match self {
CacheError::Io(_) => "io",
CacheError::InvalidName(_) => "invalid_name",
CacheError::ConfigParse(_) => "config_parse",
CacheError::NotFound(_) => "not_found",
CacheError::PermissionDenied(_) => "permission_denied",
CacheError::AlreadyExists(_) => "already_exists",
CacheError::Expired(_) => "expired",
CacheError::InvalidConfig(_) => "invalid_config",
CacheError::Serialization(_) => "serialization",
CacheError::InvalidPath(_) => "invalid_path",
CacheError::SymlinkDetected(_) => "symlink_detected",
CacheError::SizeLimitExceeded(_) => "size_limit_exceeded",
CacheError::FileCountLimitExceeded(_) => "file_count_limit_exceeded",
CacheError::Corrupted(_) => "corrupted",
CacheError::Generic(_) => "generic",
}
}
pub fn message(&self) -> String {
match self {
CacheError::Io(err) => err.to_string(),
CacheError::InvalidName(msg) => msg.clone(),
CacheError::ConfigParse(msg) => msg.clone(),
CacheError::NotFound(msg) => msg.clone(),
CacheError::PermissionDenied(msg) => msg.clone(),
CacheError::AlreadyExists(msg) => msg.clone(),
CacheError::Expired(msg) => msg.clone(),
CacheError::InvalidConfig(msg) => msg.clone(),
CacheError::Serialization(msg) => msg.clone(),
CacheError::InvalidPath(msg) => msg.clone(),
CacheError::SymlinkDetected(msg) => msg.clone(),
CacheError::SizeLimitExceeded(msg) => msg.clone(),
CacheError::FileCountLimitExceeded(msg) => msg.clone(),
CacheError::Corrupted(msg) => msg.clone(),
CacheError::Generic(msg) => msg.clone(),
}
}
pub fn new<S: Into<String>>(message: S) -> Self {
CacheError::Generic(message.into())
}
pub fn is_io_error(&self) -> bool {
matches!(self, CacheError::Io(_))
}
pub fn is_not_found(&self) -> bool {
matches!(self, CacheError::NotFound(_))
}
pub fn is_permission_denied(&self) -> bool {
matches!(self, CacheError::PermissionDenied(_))
}
}