1use std::fmt;
28use std::io;
29
30#[derive(Debug)]
32pub enum CacheError {
33 Io(io::Error),
35 InvalidName(String),
37 ConfigParse(String),
39 NotFound(String),
41 PermissionDenied(String),
43 AlreadyExists(String),
45 Expired(String),
47 InvalidConfig(String),
49 Serialization(String),
51 InvalidPath(String),
53 Generic(String),
55}
56
57impl fmt::Display for CacheError {
58 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59 match self {
60 CacheError::Io(err) => write!(f, "I/O error: {}", err),
61 CacheError::InvalidName(msg) => write!(f, "Invalid cache name: {}", msg),
62 CacheError::ConfigParse(msg) => write!(f, "Configuration parse error: {}", msg),
63 CacheError::NotFound(msg) => write!(f, "Cache not found: {}", msg),
64 CacheError::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
65 CacheError::AlreadyExists(msg) => write!(f, "Cache already exists: {}", msg),
66 CacheError::Expired(msg) => write!(f, "Cache expired: {}", msg),
67 CacheError::InvalidConfig(msg) => write!(f, "Invalid configuration: {}", msg),
68 CacheError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
69 CacheError::InvalidPath(msg) => write!(f, "Invalid path: {}", msg),
70 CacheError::Generic(msg) => write!(f, "Error: {}", msg),
71 }
72 }
73}
74
75impl std::error::Error for CacheError {}
76
77impl From<io::Error> for CacheError {
78 fn from(err: io::Error) -> Self {
79 CacheError::Io(err)
80 }
81}
82
83impl From<serde_json::Error> for CacheError {
84 fn from(err: serde_json::Error) -> Self {
85 CacheError::ConfigParse(err.to_string())
86 }
87}
88
89impl CacheError {
90 pub fn kind(&self) -> &'static str {
92 match self {
93 CacheError::Io(_) => "io",
94 CacheError::InvalidName(_) => "invalid_name",
95 CacheError::ConfigParse(_) => "config_parse",
96 CacheError::NotFound(_) => "not_found",
97 CacheError::PermissionDenied(_) => "permission_denied",
98 CacheError::AlreadyExists(_) => "already_exists",
99 CacheError::Expired(_) => "expired",
100 CacheError::InvalidConfig(_) => "invalid_config",
101 CacheError::Serialization(_) => "serialization",
102 CacheError::InvalidPath(_) => "invalid_path",
103 CacheError::Generic(_) => "generic",
104 }
105 }
106
107 pub fn message(&self) -> String {
109 match self {
110 CacheError::Io(err) => err.to_string(),
111 CacheError::InvalidName(msg) => msg.clone(),
112 CacheError::ConfigParse(msg) => msg.clone(),
113 CacheError::NotFound(msg) => msg.clone(),
114 CacheError::PermissionDenied(msg) => msg.clone(),
115 CacheError::AlreadyExists(msg) => msg.clone(),
116 CacheError::Expired(msg) => msg.clone(),
117 CacheError::InvalidConfig(msg) => msg.clone(),
118 CacheError::Serialization(msg) => msg.clone(),
119 CacheError::InvalidPath(msg) => msg.clone(),
120 CacheError::Generic(msg) => msg.clone(),
121 }
122 }
123
124 pub fn new<S: Into<String>>(message: S) -> Self {
126 CacheError::Generic(message.into())
127 }
128
129 pub fn is_io_error(&self) -> bool {
131 matches!(self, CacheError::Io(_))
132 }
133
134 pub fn is_not_found(&self) -> bool {
136 matches!(self, CacheError::NotFound(_))
137 }
138
139 pub fn is_permission_denied(&self) -> bool {
141 matches!(self, CacheError::PermissionDenied(_))
142 }
143}