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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::resp2::types::Frame as Resp2Frame;
use crate::resp3::types::Frame as Resp3Frame;
use cookie_factory::GenError;
use nom::error::{ErrorKind, FromExternalError, ParseError};
use nom::{Err as NomError, Needed};
use std::borrow::Borrow;
use std::borrow::Cow;
use std::fmt::{self, Debug};
use std::io::Error as IoError;
use std::str;
pub const CRLF: &'static str = "\r\n";
#[derive(Debug)]
pub enum RedisProtocolErrorKind {
EncodeError,
BufferTooSmall(usize),
DecodeError,
IO(IoError),
Unknown,
}
impl PartialEq for RedisProtocolErrorKind {
fn eq(&self, other: &Self) -> bool {
use self::RedisProtocolErrorKind::*;
match *self {
EncodeError => match *other {
EncodeError => true,
_ => false,
},
DecodeError => match *other {
DecodeError => true,
_ => false,
},
BufferTooSmall(amt) => match *other {
BufferTooSmall(_amt) => amt == amt,
_ => false,
},
IO(_) => match *other {
IO(_) => true,
_ => false,
},
Unknown => match *other {
Unknown => true,
_ => false,
},
}
}
}
impl Eq for RedisProtocolErrorKind {}
impl RedisProtocolErrorKind {
pub fn to_str(&self) -> &'static str {
use self::RedisProtocolErrorKind::*;
match *self {
EncodeError => "Encode Error",
DecodeError => "Decode Error",
Unknown => "Unknown Error",
IO(_) => "IO Error",
BufferTooSmall(_) => "Buffer too small",
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct RedisProtocolError {
desc: Cow<'static, str>,
kind: RedisProtocolErrorKind,
}
impl RedisProtocolError {
pub fn buffer_too_small(amt: usize) -> Self {
RedisProtocolError::new(RedisProtocolErrorKind::BufferTooSmall(amt), "")
}
pub fn new<S: Into<Cow<'static, str>>>(kind: RedisProtocolErrorKind, desc: S) -> Self {
RedisProtocolError {
kind,
desc: desc.into(),
}
}
pub fn description(&self) -> &str {
self.desc.borrow()
}
pub fn new_empty() -> Self {
RedisProtocolError {
kind: RedisProtocolErrorKind::Unknown,
desc: "".into(),
}
}
pub fn to_string(&self) -> String {
format!("{}: {}", self.kind.to_str(), self.desc)
}
pub fn kind(&self) -> &RedisProtocolErrorKind {
&self.kind
}
}
impl fmt::Display for RedisProtocolError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}: {}", self.kind.to_str(), self.desc)
}
}
impl From<GenError> for RedisProtocolError {
fn from(e: GenError) -> Self {
match e {
GenError::CustomError(i) => match i {
1 => RedisProtocolError::new(RedisProtocolErrorKind::EncodeError, "Invalid frame kind."),
2 => RedisProtocolError::new(RedisProtocolErrorKind::EncodeError, "Cannot encode NaN."),
3 => RedisProtocolError::new(RedisProtocolErrorKind::EncodeError, "Cannot stream non aggregate type."),
_ => RedisProtocolError::new_empty(),
},
GenError::InvalidOffset => RedisProtocolError::new(RedisProtocolErrorKind::EncodeError, "Invalid offset."),
GenError::BufferTooSmall(b) => RedisProtocolError::buffer_too_small(b),
_ => RedisProtocolError::new_empty(),
}
}
}
impl From<NomError<nom::error::Error<&[u8]>>> for RedisProtocolError {
fn from(e: NomError<nom::error::Error<&[u8]>>) -> Self {
if let NomError::Incomplete(Needed::Size(ref s)) = e {
RedisProtocolError {
kind: RedisProtocolErrorKind::BufferTooSmall(s.get()),
desc: Cow::Borrowed(""),
}
} else {
let desc = match e {
NomError::Failure(inner) => format!("Failure: {:?}", inner.code),
NomError::Error(inner) => format!("Error: {:?}", inner.code),
_ => format!("{:?}", e),
};
RedisProtocolError {
kind: RedisProtocolErrorKind::DecodeError,
desc: Cow::Owned(desc),
}
}
}
}
impl From<NomError<&[u8]>> for RedisProtocolError {
fn from(e: NomError<&[u8]>) -> Self {
if let NomError::Incomplete(Needed::Size(ref s)) = e {
RedisProtocolError {
kind: RedisProtocolErrorKind::BufferTooSmall(s.get()),
desc: Cow::Borrowed(""),
}
} else {
RedisProtocolError {
kind: RedisProtocolErrorKind::DecodeError,
desc: Cow::Owned(format!("{:?}", e)),
}
}
}
}
impl From<IoError> for RedisProtocolError {
fn from(e: IoError) -> Self {
RedisProtocolError::new(RedisProtocolErrorKind::IO(e), "IO Error")
}
}
impl<I> From<RedisParseError<I>> for RedisProtocolError
where
I: Debug,
{
fn from(e: RedisParseError<I>) -> Self {
RedisProtocolError::new(RedisProtocolErrorKind::DecodeError, format!("{:?}", e))
}
}
pub enum RedisParseError<I> {
Custom {
context: &'static str,
message: Cow<'static, str>,
},
Incomplete(Needed),
Nom(I, ErrorKind),
}
impl<I> fmt::Debug for RedisParseError<I>
where
I: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RedisParseError::Custom {
ref context,
ref message,
} => write!(f, "{}: {}", context, message),
RedisParseError::Nom(input, kind) => write!(f, "{:?} at {:?}", kind, input),
RedisParseError::Incomplete(ref needed) => write!(f, "Incomplete({:?})", needed),
}
}
}
impl<I> RedisParseError<I> {
pub fn new_custom<S: Into<Cow<'static, str>>>(ctx: &'static str, message: S) -> Self {
RedisParseError::Custom {
context: ctx,
message: message.into(),
}
}
pub fn into_nom_error(self) -> nom::Err<RedisParseError<I>> {
match self {
RedisParseError::Incomplete(n) => nom::Err::Incomplete(n),
_ => nom::Err::Failure(self),
}
}
}
impl<I> ParseError<I> for RedisParseError<I> {
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
RedisParseError::Nom(input, kind)
}
fn append(_: I, _: ErrorKind, other: Self) -> Self {
other
}
}
impl<I, E> FromExternalError<I, E> for RedisParseError<I> {
fn from_external_error(input: I, kind: ErrorKind, _e: E) -> Self {
RedisParseError::Nom(input, kind)
}
}
impl<I> From<nom::Err<RedisParseError<I>>> for RedisParseError<I> {
fn from(e: NomError<RedisParseError<I>>) -> Self {
match e {
NomError::Incomplete(n) => RedisParseError::Incomplete(n),
NomError::Failure(e) | NomError::Error(e) => e,
}
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Redirection {
Moved { slot: u16, server: String },
Ask { slot: u16, server: String },
}
impl Redirection {
pub fn to_resp2_frame(&self) -> Resp2Frame {
let inner = match *self {
Redirection::Moved { ref slot, ref server } => format!("MOVED {} {}", slot, server),
Redirection::Ask { ref slot, ref server } => format!("ASK {} {}", slot, server),
};
Resp2Frame::Error(inner)
}
pub fn to_resp3_frame(&self) -> Resp3Frame {
let inner = match *self {
Redirection::Moved { ref slot, ref server } => format!("MOVED {} {}", slot, server),
Redirection::Ask { ref slot, ref server } => format!("ASK {} {}", slot, server),
};
Resp3Frame::SimpleError {
data: inner,
attributes: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::num::NonZeroUsize;
#[test]
fn should_create_empty_error() {
let e = RedisProtocolError::new_empty();
assert_eq!(e.description(), "");
assert_eq!(e.kind(), &RedisProtocolErrorKind::Unknown);
}
#[test]
fn should_create_encode_error() {
let e = RedisProtocolError::new(RedisProtocolErrorKind::EncodeError, "foo");
assert_eq!(e.description(), "foo");
assert_eq!(e.kind(), &RedisProtocolErrorKind::EncodeError);
}
#[test]
fn should_create_decode_error() {
let e = RedisProtocolError::new(RedisProtocolErrorKind::DecodeError, "foo");
assert_eq!(e.description(), "foo");
assert_eq!(e.kind(), &RedisProtocolErrorKind::DecodeError);
}
#[test]
fn should_create_buf_too_small_error() {
let e = RedisProtocolError::new(RedisProtocolErrorKind::BufferTooSmall(10), "foo");
assert_eq!(e.description(), "foo");
assert_eq!(e.kind(), &RedisProtocolErrorKind::BufferTooSmall(10));
}
#[test]
fn should_cast_from_nom_incomplete() {
let n: NomError<&[u8]> = NomError::Incomplete(Needed::Size(NonZeroUsize::new(10).unwrap()));
let e = RedisProtocolError::from(n);
assert_eq!(e.kind(), &RedisProtocolErrorKind::BufferTooSmall(10));
}
#[test]
fn should_print_error_kinds() {
assert_eq!(RedisProtocolErrorKind::EncodeError.to_str(), "Encode Error");
assert_eq!(RedisProtocolErrorKind::DecodeError.to_str(), "Decode Error");
assert_eq!(RedisProtocolErrorKind::Unknown.to_str(), "Unknown Error");
assert_eq!(RedisProtocolErrorKind::BufferTooSmall(10).to_str(), "Buffer too small");
}
}