1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
#[derive(Debug)]
pub enum KVSError {
GeneralKVSError,
KeyNotFoundError,
IOError,
SerdeJsonError,
FromUtf8Error,
SledError,
}
impl Display for KVSError {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
KVSError::KeyNotFoundError => write!(f, "Key not found"),
KVSError::IOError => write!(f, "Imput output error"),
KVSError::SerdeJsonError => write!(f, "Json serialization error"),
KVSError::GeneralKVSError => write!(f, "Unknown error"),
KVSError::FromUtf8Error => write!(f, "Cant converct to string"),
KVSError::SledError => write!(f, "Sled engine error"),
}
}
}
impl Error for KVSError {
fn description(&self) -> &str {
"KVS error .. please get some help"
}
}
impl From<std::io::Error> for KVSError {
fn from(_err: std::io::Error) -> KVSError {
KVSError::IOError
}
}
impl From<serde_json::Error> for KVSError {
fn from(_err: serde_json::Error) -> KVSError {
KVSError::SerdeJsonError
}
}
impl From<std::string::FromUtf8Error> for KVSError {
fn from(_err: std::string::FromUtf8Error) -> KVSError {
KVSError::FromUtf8Error
}
}
impl From<sled::Error> for KVSError {
fn from(_err: sled::Error) -> KVSError {
KVSError::SledError
}
}
impl From<String> for KVSError {
fn from(_err: String) -> KVSError {
KVSError::GeneralKVSError
}
}
pub type Result<T> = std::result::Result<T, KVSError>;