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 core::num::{ParseFloatError, ParseIntError};
use std::fmt;
use std::str::Utf8Error;
use std::string::FromUtf8Error;

#[derive(Debug)]
pub enum RedisError {
    WrongArity,
    Str(&'static str),
    String(String),
}

impl RedisError {
    pub fn nonexistent_key() -> Self {
        RedisError::Str("ERR could not perform this operation on a key that doesn't exist")
    }
}

impl From<&'static str> for RedisError {
    fn from(s: &'static str) -> Self {
        RedisError::Str(s)
    }
}

impl From<String> for RedisError {
    fn from(s: String) -> Self {
        RedisError::String(s)
    }
}

impl From<ParseFloatError> for RedisError {
    fn from(e: ParseFloatError) -> Self {
        RedisError::String(e.to_string())
    }
}

impl From<ParseIntError> for RedisError {
    fn from(e: ParseIntError) -> Self {
        RedisError::String(e.to_string())
    }
}

impl From<FromUtf8Error> for RedisError {
    fn from(e: FromUtf8Error) -> Self {
        RedisError::String(e.to_string())
    }
}

impl From<Utf8Error> for RedisError {
    fn from(e: Utf8Error) -> Self {
        RedisError::String(e.to_string())
    }
}

impl fmt::Display for RedisError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let d = match self {
            RedisError::WrongArity => "Wrong Arity",
            RedisError::Str(s) => s,
            RedisError::String(s) => s.as_str(),
        };

        write!(f, "{}", d)
    }
}