1use std::fmt;
2
3#[derive(Debug)]
4pub enum StorageError {
5 Put(String),
6 Get(String),
7 Delete(String),
8 NotFound(String),
9 PermissionDenied(String),
10 Connection(String),
11 InvalidConfig(String),
12}
13
14impl fmt::Display for StorageError {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 match self {
17 StorageError::Put(msg) => write!(f, "Put error: {}", msg),
18 StorageError::Get(msg) => write!(f, "Get error: {}", msg),
19 StorageError::Delete(msg) => write!(f, "Delete error: {}", msg),
20 StorageError::NotFound(key) => write!(f, "Key not found: {}", key),
21 StorageError::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
22 StorageError::Connection(msg) => write!(f, "Connection error: {}", msg),
23 StorageError::InvalidConfig(msg) => write!(f, "Invalid config: {}", msg),
24 }
25 }
26}
27
28impl std::error::Error for StorageError {}
29
30impl From<std::io::Error> for StorageError {
31 fn from(err: std::io::Error) -> Self {
32 if err.kind() == std::io::ErrorKind::NotFound {
33 StorageError::NotFound(err.to_string())
34 } else if err.kind() == std::io::ErrorKind::PermissionDenied {
35 StorageError::PermissionDenied(err.to_string())
36 } else {
37 StorageError::Connection(err.to_string())
38 }
39 }
40}