1use crate::RedisError;
2use std::error;
3use std::fmt;
4use std::fmt::Display;
5
6#[derive(Debug)]
7pub enum Error {
8 Generic(GenericError),
9 FromUtf8(std::string::FromUtf8Error),
10 ParseInt(std::num::ParseIntError),
11}
12
13impl Error {
14 #[must_use]
15 pub fn generic(message: &str) -> Self {
16 Self::Generic(GenericError::new(message))
17 }
18}
19
20impl From<RedisError> for Error {
21 fn from(err: RedisError) -> Self {
22 Self::generic(err.to_string().as_str())
23 }
24}
25
26impl From<std::string::FromUtf8Error> for Error {
27 fn from(err: std::string::FromUtf8Error) -> Self {
28 Self::FromUtf8(err)
29 }
30}
31
32impl From<std::num::ParseIntError> for Error {
33 fn from(err: std::num::ParseIntError) -> Self {
34 Self::ParseInt(err)
35 }
36}
37
38impl Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 match *self {
41 Self::Generic(ref err) => write!(f, "{err}"),
44 Self::FromUtf8(ref err) => write!(f, "{err}"),
45 Self::ParseInt(ref err) => write!(f, "{err}"),
46 }
47 }
48}
49
50impl error::Error for Error {
51 fn cause(&self) -> Option<&dyn error::Error> {
52 match *self {
53 Self::Generic(ref err) => Some(err),
58 Self::FromUtf8(ref err) => Some(err),
59 Self::ParseInt(ref err) => Some(err),
60 }
61 }
62}
63
64#[derive(Debug)]
65pub struct GenericError {
66 message: String,
67}
68
69impl GenericError {
70 #[must_use]
71 pub fn new(message: &str) -> Self {
72 Self {
73 message: String::from(message),
74 }
75 }
76}
77
78impl Display for GenericError {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 write!(f, "Store error: {}", self.message)
81 }
82}
83
84impl error::Error for GenericError {
85 fn description(&self) -> &str {
86 self.message.as_str()
87 }
88
89 fn cause(&self) -> Option<&dyn error::Error> {
90 None
91 }
92}