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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::{RedisError, Result};
use serde::de::DeserializeOwned;
use std::{
collections::HashMap,
fmt::{self, Display, Formatter, Write},
hash::{Hash, Hasher},
};
/// Generic Redis Object Model
///
/// This enum is a direct mapping to [`Redis serialization protocol`](https://redis.io/docs/latest/develop/reference/protocol-spec) (RESP)
#[derive(Default)]
pub enum Value {
/// [RESP Simple String](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-strings)
SimpleString(String),
/// [RESP Integer](https://redis.io/docs/latest/develop/reference/protocol-spec/#integers)
Integer(i64),
/// [RESP Double](https://redis.io/docs/latest/develop/reference/protocol-spec/#doubles)
Double(f64),
/// [RESP Bulk String](https://redis.io/docs/latest/develop/reference/protocol-spec/#bulk-strings)
BulkString(Vec<u8>),
/// [RESP Boolean](https://redis.io/docs/latest/develop/reference/protocol-spec/#booleans)
Boolean(bool),
/// [RESP Array](https://redis.io/docs/latest/develop/reference/protocol-spec/#arrays)
Array(Vec<Value>),
/// [RESP Map](https://redis.io/docs/latest/develop/reference/protocol-spec/#maps)
Map(HashMap<Value, Value>),
/// [RESP Set](https://redis.io/docs/latest/develop/reference/protocol-spec/#sets)
Set(Vec<Value>),
/// [RESP Push](https://redis.io/docs/latest/develop/reference/protocol-spec/#pushes)
Push(Vec<Value>),
/// [RESP Error](https://redis.io/docs/latest/develop/reference/protocol-spec/#simple-errors)
Error(RedisError),
/// [RESP Null](https://redis.io/docs/latest/develop/reference/protocol-spec/#nulls)
#[default]
Null,
}
impl Value {
/// A [`Value`](crate::resp::Value) to user type conversion that consumes the input value.
///
/// # Errors
/// Any parsing error ([`Error::Client`](crate::Error::Client)) due to incompatibility between Value variant and taget type
#[inline]
pub fn into<T>(self) -> Result<T>
where
T: DeserializeOwned,
{
T::deserialize(&self)
}
}
impl Hash for Value {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
Value::SimpleString(s) => s.hash(state),
Value::Integer(i) => i.hash(state),
Value::Double(d) => d.to_string().hash(state),
Value::BulkString(bs) => bs.hash(state),
Value::Error(e) => e.hash(state),
Value::Null => "_\r\n".hash(state),
_ => unimplemented!("Hash not implemented for {self}"),
}
}
}
impl PartialEq for Value {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::SimpleString(l0), Self::SimpleString(r0)) => l0 == r0,
(Self::Integer(l0), Self::Integer(r0)) => l0 == r0,
(Self::Double(l0), Self::Double(r0)) => l0 == r0,
(Self::BulkString(l0), Self::BulkString(r0)) => l0 == r0,
(Self::Array(l0), Self::Array(r0)) => l0 == r0,
(Self::Map(l0), Self::Map(r0)) => l0 == r0,
(Self::Set(l0), Self::Set(r0)) => l0 == r0,
(Self::Push(l0), Self::Push(r0)) => l0 == r0,
(Self::Error(l0), Self::Error(r0)) => l0 == r0,
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
impl Eq for Value {}
impl Display for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self {
Value::SimpleString(s) => s.fmt(f),
Value::Integer(i) => i.fmt(f),
Value::Double(d) => d.fmt(f),
Value::BulkString(s) => String::from_utf8_lossy(s).fmt(f),
Value::Boolean(b) => b.fmt(f),
Value::Array(v) => {
f.write_char('[')?;
let mut first = true;
for value in v {
if !first {
f.write_str(", ")?;
}
first = false;
value.fmt(f)?;
}
f.write_char(']')
}
Value::Map(m) => {
f.write_char('{')?;
let mut first = true;
for (key, value) in m {
if !first {
f.write_str(", ")?;
}
first = false;
key.fmt(f)?;
f.write_str(": ")?;
value.fmt(f)?;
}
f.write_char('}')
}
Value::Set(v) => {
f.write_char('[')?;
let mut first = true;
for value in v {
if !first {
f.write_str(", ")?;
}
first = false;
value.fmt(f)?;
}
f.write_char(']')
}
Value::Push(v) => {
f.write_char('[')?;
let mut first = true;
for value in v {
if !first {
f.write_str(", ")?;
}
first = false;
value.fmt(f)?;
}
f.write_char(']')
}
Value::Error(e) => e.fmt(f),
Value::Null => f.write_str("Nil"),
}
}
}
impl fmt::Debug for Value {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::SimpleString(arg0) => f.debug_tuple("SimpleString").field(arg0).finish(),
Self::Integer(arg0) => f.debug_tuple("Integer").field(arg0).finish(),
Self::Double(arg0) => f.debug_tuple("Double").field(arg0).finish(),
Self::BulkString(arg0) => f
.debug_tuple("BulkString")
.field(&String::from_utf8_lossy(arg0).into_owned())
.finish(),
Self::Boolean(arg0) => f.debug_tuple("Boolean").field(arg0).finish(),
Self::Array(arg0) => f.debug_tuple("Array").field(arg0).finish(),
Self::Map(arg0) => f.debug_tuple("Map").field(arg0).finish(),
Self::Set(arg0) => f.debug_tuple("Set").field(arg0).finish(),
Self::Push(arg0) => f.debug_tuple("Push").field(arg0).finish(),
Self::Error(arg0) => f.debug_tuple("Error").field(arg0).finish(),
Self::Null => write!(f, "Nil"),
}
}
}