redis-protocol-mm 4.2.0

Structs and functions to implement the Redis protocol.
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use alloc::format;
use alloc::string::ToString;
use alloc::vec::Vec;
use core::hash::{Hash, Hasher};
use crate::decode_mut::utils::hash_tuple;
use crate::resp3::types::{Auth, FrameKind, RespVersion, VerbatimStringFormat, NULL};
use crate::types::{RedisParseError, RedisProtocolError, RedisProtocolErrorKind};
use nom::IResult;

#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};

#[cfg(feature = "hashbrown")]
use hashbrown::{HashMap, HashSet};

pub type IndexFrameMap = HashMap<Resp3IndexFrame, Resp3IndexFrame>;
pub type IndexAttributes = Option<IndexFrameMap>;
pub type DResult<'a, T> = IResult<(&'a [u8], usize), T, RedisParseError<&'a [u8]>>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Resp2IndexFrame {
  SimpleString { start: usize, end: usize },
  Error { start: usize, end: usize },
  Integer(i64),
  BulkString { start: usize, end: usize },
  Array(Vec<Resp2IndexFrame>),
  Null,
}

#[derive(Clone, Debug)]
pub enum Resp3IndexFrame {
  BlobString {
    data: (usize, usize),
    attributes: IndexAttributes,
  },
  BlobError {
    data: (usize, usize),
    attributes: IndexAttributes,
  },
  SimpleString {
    data: (usize, usize),
    attributes: IndexAttributes,
  },
  SimpleError {
    data: (usize, usize),
    attributes: IndexAttributes,
  },
  Boolean {
    data: bool,
    attributes: IndexAttributes,
  },
  Null,
  Number {
    data: i64,
    attributes: IndexAttributes,
  },
  Double {
    data: f64,
    attributes: IndexAttributes,
  },
  BigNumber {
    data: (usize, usize),
    attributes: IndexAttributes,
  },
  VerbatimString {
    data: (usize, usize),
    attributes: IndexAttributes,
    format: VerbatimStringFormat,
  },
  Array {
    data: Vec<Resp3IndexFrame>,
    attributes: IndexAttributes,
  },
  Map {
    data: IndexFrameMap,
    attributes: IndexAttributes,
  },
  Set {
    data: HashSet<Resp3IndexFrame>,
    attributes: IndexAttributes,
  },
  Push {
    data: Vec<Resp3IndexFrame>,
    attributes: IndexAttributes,
  },
  Hello {
    version: RespVersion,
    auth: Option<Auth>,
  },
  ChunkedString((usize, usize)),
}

impl PartialEq for Resp3IndexFrame {
  fn eq(&self, other: &Self) -> bool {
    use self::Resp3IndexFrame::*;

    match *self {
      ChunkedString(ref b) => match *other {
        ChunkedString(ref _b) => b == _b,
        _ => false,
      },
      Array {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Array {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      BlobString {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          BlobString {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      SimpleString {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          SimpleString {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      SimpleError {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          SimpleError {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      Number {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Number {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      Null => match *other {
        Null => true,
        _ => false,
      },
      Boolean {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Boolean {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      Double {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Double {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      BlobError {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          BlobError {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      VerbatimString {
        ref data,
        ref format,
        ref attributes,
      } => {
        let (_data, _format, _attributes) = (data, format, attributes);
        match *other {
          VerbatimString {
            ref data,
            ref format,
            ref attributes,
          } => _data == data && _format == format && attributes == _attributes,
          _ => false,
        }
      }
      Map {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Map {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      Set {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Set {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      Push {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          Push {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
      Hello { ref version, ref auth } => {
        let (_version, _auth) = (version, auth);
        match *other {
          Hello { ref version, ref auth } => _version == version && _auth == auth,
          _ => false,
        }
      }
      BigNumber {
        ref data,
        ref attributes,
      } => {
        let (_data, _attributes) = (data, attributes);
        match *other {
          BigNumber {
            ref data,
            ref attributes,
          } => data == _data && attributes == _attributes,
          _ => false,
        }
      }
    }
  }
}

impl Eq for Resp3IndexFrame {}

impl Hash for Resp3IndexFrame {
  fn hash<H: Hasher>(&self, state: &mut H) {
    use self::Resp3IndexFrame::*;
    self.kind().hash_prefix().hash(state);

    match *self {
      BlobString { ref data, .. } => hash_tuple(state, data),
      SimpleString { ref data, .. } => hash_tuple(state, data),
      SimpleError { ref data, .. } => hash_tuple(state, data),
      Number { ref data, .. } => data.hash(state),
      Null => NULL.hash(state),
      Double { ref data, .. } => data.to_string().hash(state),
      Boolean { ref data, .. } => data.hash(state),
      BlobError { ref data, .. } => hash_tuple(state, data),
      VerbatimString {
        ref data, ref format, ..
      } => {
        format.hash(state);
        hash_tuple(state, data);
      }
      ChunkedString(ref data) => hash_tuple(state, data),
      BigNumber { ref data, .. } => hash_tuple(state, data),
      _ => panic!("Invalid RESP3 data type to use as hash key."),
    };
  }
}

impl Resp3IndexFrame {
  pub fn new_end_stream() -> Self {
    Resp3IndexFrame::ChunkedString((0, 0))
  }

  pub fn add_attributes(&mut self, attributes: IndexFrameMap) -> Result<(), RedisProtocolError> {
    use self::Resp3IndexFrame::*;

    let _attributes = match *self {
      Array { ref mut attributes, .. } => attributes,
      BlobString { ref mut attributes, .. } => attributes,
      SimpleString { ref mut attributes, .. } => attributes,
      SimpleError { ref mut attributes, .. } => attributes,
      Number { ref mut attributes, .. } => attributes,
      Double { ref mut attributes, .. } => attributes,
      BlobError { ref mut attributes, .. } => attributes,
      VerbatimString { ref mut attributes, .. } => attributes,
      Boolean { ref mut attributes, .. } => attributes,
      Map { ref mut attributes, .. } => attributes,
      Set { ref mut attributes, .. } => attributes,
      Push { ref mut attributes, .. } => attributes,
      BigNumber { ref mut attributes, .. } => attributes,
      _ => {
        return Err(RedisProtocolError::new(
          RedisProtocolErrorKind::DecodeError,
          format!("{:?} cannot have attributes.", self.kind()),
        ))
      }
    };

    if let Some(_attributes) = _attributes.as_mut() {
      _attributes.extend(attributes.into_iter());
    } else {
      *_attributes = Some(attributes);
    }

    Ok(())
  }

  /// Read the associated `FrameKind`.
  pub fn kind(&self) -> FrameKind {
    use self::Resp3IndexFrame::*;

    match *self {
      Array { .. } => FrameKind::Array,
      BlobString { .. } => FrameKind::BlobString,
      SimpleString { .. } => FrameKind::SimpleString,
      SimpleError { .. } => FrameKind::SimpleError,
      Number { .. } => FrameKind::Number,
      Null => FrameKind::Null,
      Double { .. } => FrameKind::Double,
      BlobError { .. } => FrameKind::BlobError,
      VerbatimString { .. } => FrameKind::VerbatimString,
      Boolean { .. } => FrameKind::Boolean,
      Map { .. } => FrameKind::Map,
      Set { .. } => FrameKind::Set,
      Push { .. } => FrameKind::Push,
      Hello { .. } => FrameKind::Hello,
      BigNumber { .. } => FrameKind::BigNumber,
      ChunkedString(ref inner) => {
        if inner.1 - inner.0 == 0 {
          FrameKind::EndStream
        } else {
          FrameKind::ChunkedString
        }
      }
    }
  }
}

#[derive(Debug)]
pub struct StreamedIndexFrame {
  pub kind: FrameKind,
  pub attributes: IndexAttributes,
}

impl StreamedIndexFrame {
  pub fn new(kind: FrameKind) -> Self {
    StreamedIndexFrame { kind, attributes: None }
  }
}

#[derive(Debug)]
pub enum DecodedIndexFrame {
  Complete(Resp3IndexFrame),
  Streaming(StreamedIndexFrame),
}

impl DecodedIndexFrame {
  /// Add attributes to the decoded frame, if possible.
  pub fn add_attributes(&mut self, attributes: IndexFrameMap) -> Result<(), RedisProtocolError> {
    let _ = match *self {
      DecodedIndexFrame::Streaming(ref mut inner) => inner.attributes = Some(attributes),
      DecodedIndexFrame::Complete(ref mut inner) => inner.add_attributes(attributes)?,
    };

    Ok(())
  }

  /// Convert the decoded frame to a complete frame, returning an error if a streaming variant is found.
  pub fn into_complete_frame(self) -> Result<Resp3IndexFrame, RedisProtocolError> {
    match self {
      DecodedIndexFrame::Complete(frame) => Ok(frame),
      DecodedIndexFrame::Streaming(_) => Err(RedisProtocolError::new(
        RedisProtocolErrorKind::DecodeError,
        "Expected complete frame.",
      )),
    }
  }
}