redis_client/
results.rs

1use std::collections::HashMap;
2use std::fmt;
3use std::str;
4
5#[derive(Debug, Clone)]
6pub enum RedisResult {
7    Array(Vec<RedisResult>),
8    Bytes(Vec<u8>),
9    String(String),
10    Int(i64),
11    Nil,
12}
13
14impl RedisResult {
15    /// Method to convert a RedisResult into another type
16    pub fn convert<T: From<RedisResult>>(self) -> T {
17        self.into()
18    }
19}
20
21impl fmt::Display for RedisResult {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        match *self {
24            RedisResult::Array(ref value) => write!(f, "{:?}", value),
25            RedisResult::Bytes(ref value) => write!(f, "{:?}", value),
26            RedisResult::String(ref value) => write!(f, "{:?}", value),
27            RedisResult::Int(ref value) => write!(f, "{:?}", value),
28            RedisResult::Nil => write!(f, "null"),
29        }
30    }
31}
32
33impl From<RedisResult> for String {
34    fn from(result: RedisResult) -> String {
35        match result {
36            RedisResult::Array(value) => {
37                let mut retval: String = "[".to_string();
38                for res in value {
39                    retval = retval + &res.convert::<String>() + &",".to_string();
40                }
41                retval = retval + &"]".to_string();
42                retval
43            },
44            RedisResult::Bytes(value) => {
45                let result = str::from_utf8(&value);
46                match result {
47                    Ok(str_value) => str_value.to_string(),
48                    Err(err) => err.to_string(),
49                }
50            },
51            RedisResult::String(value) => value,
52            RedisResult::Int(value) => value.to_string(),
53            RedisResult::Nil => "null".to_string(),
54        }
55    }
56}
57
58
59impl From<RedisResult> for Vec<u8> {
60    fn from(result: RedisResult) -> Vec<u8> {
61        match result {
62            RedisResult::Array(value) => RedisResult::Array(value).convert::<String>().into_bytes(),
63            RedisResult::Bytes(value) => value,
64            RedisResult::String(value) => value.into_bytes(),
65            RedisResult::Int(value) => value.to_string().into_bytes(),
66            RedisResult::Nil => vec![],
67        }
68    }
69}
70
71impl From<RedisResult> for Vec<String> {
72    fn from(result: RedisResult) -> Vec<String> {
73        match result {
74            RedisResult::Array(value) => {
75                let mut retval = Vec::new();
76                for res in value {
77                    retval.push(res.convert::<String>());
78                }
79                retval
80            },
81            RedisResult::Bytes(value) => {
82                let result = str::from_utf8(&value);
83                match result {
84                    Ok(str_value) => vec![str_value.to_string()],
85                    Err(err) => vec![err.to_string()],
86                }
87            },
88            RedisResult::String(value) => vec![value],
89            RedisResult::Int(value) => vec![value.to_string()],
90            RedisResult::Nil => vec![],
91        }
92    }
93}
94
95impl From<RedisResult> for HashMap<String, String> {
96    fn from(result: RedisResult) -> HashMap<String, String> {
97        match result {
98            RedisResult::Array(value) => {
99                let mut retval = HashMap::new();
100                let mut key = String::new();
101                for (index, res) in value.into_iter().enumerate() {
102                    if index % 2 == 0 {
103                        key = res.convert::<String>();
104                    } else {
105                        retval.insert(key.to_string(), res.convert::<String>());
106                    }
107                }
108                retval
109            },
110            RedisResult::Bytes(value) => {
111                let str_value = RedisResult::Bytes(value).convert::<String>();
112                let mut retval = HashMap::new();
113                retval.insert("Stringified value".to_string(), str_value);
114                retval
115            },
116            RedisResult::String(value) => {
117                let mut retval = HashMap::new();
118                retval.insert("Stringified value".to_string(), value);
119                retval
120            },
121            RedisResult::Int(value) => {
122                let str_value = RedisResult::Int(value).convert::<String>();
123                let mut retval = HashMap::new();
124                retval.insert("Stringified value".to_string(), str_value);
125                retval
126            },
127            RedisResult::Nil => {
128                let str_value = RedisResult::Nil.convert::<String>();
129                let mut retval = HashMap::new();
130                retval.insert("Stringified value".to_string(), str_value);
131                retval
132            },
133        }
134    }
135}
136
137impl From<RedisResult> for i64 {
138    fn from(result: RedisResult) -> i64 {
139        match result {
140            RedisResult::Array(_value) => 0,
141            RedisResult::Bytes(_value) => 0,
142            RedisResult::String(_value) => 0,
143            RedisResult::Int(value) => value,
144            RedisResult::Nil => 0,
145        }
146    }
147}
148