1use crate::{
2 error::*,
3 resp3::{types::*, utils as resp3_utils},
4 types::{DResult, CRLF},
5 utils,
6};
7use alloc::{format, vec::Vec};
8use core::{fmt::Debug, str, str::FromStr};
9use nom::{
10 bytes::streaming::{take as nom_take, take_until as nom_take_until},
11 combinator::{map as nom_map, map_res as nom_map_res},
12 multi::count as nom_count,
13 number::streaming::be_u8 as nom_be_u8,
14 sequence::terminated as nom_terminated,
15 Err as NomErr,
16};
17
18#[cfg(feature = "bytes")]
19use bytes::{Bytes, BytesMut};
20
21fn map_complete_frame(frame: RangeFrame) -> DecodedRangeFrame {
22 DecodedRangeFrame::Complete(frame)
23}
24
25fn expect_complete_index_frame<T>(frame: DecodedRangeFrame) -> Result<RangeFrame, RedisParseError<T>> {
26 frame
27 .into_complete_frame()
28 .map_err(|e| RedisParseError::new_custom("expect_complete_frame", format!("{:?}", e)))
29}
30
31fn parse_as<V, T>(s: &[u8]) -> Result<V, RedisParseError<T>>
32where
33 V: FromStr,
34 V::Err: Debug,
35{
36 str::from_utf8(s)?
37 .parse::<V>()
38 .map_err(|e| RedisParseError::new_custom("parse_as", format!("{:?}", e)))
39}
40
41fn to_isize<T>(s: &[u8]) -> Result<isize, RedisParseError<T>> {
42 let s = str::from_utf8(s)?;
43
44 if s == "?" {
45 Ok(-1)
46 } else {
47 s.parse::<isize>()
48 .map_err(|e| RedisParseError::new_custom("to_isize", format!("{:?}", e)))
49 }
50}
51fn to_bool<T>(s: &[u8]) -> Result<bool, RedisParseError<T>> {
52 match str::from_utf8(s)? {
53 "t" => Ok(true),
54 "f" => Ok(false),
55 _ => Err(RedisParseError::new_custom("to_bool", "Invalid boolean value.")),
56 }
57}
58
59fn to_verbatimstring_format<T>(s: &[u8]) -> Result<VerbatimStringFormat, RedisParseError<T>> {
60 match str::from_utf8(s)? {
61 "txt" => Ok(VerbatimStringFormat::Text),
62 "mkd" => Ok(VerbatimStringFormat::Markdown),
63 _ => Err(RedisParseError::new_custom(
64 "to_verbatimstring_format",
65 "Invalid format.",
66 )),
67 }
68}
69
70fn to_map<T>(mut data: Vec<RangeFrame>) -> Result<FrameMap<RangeFrame, RangeFrame>, RedisParseError<T>> {
71 if data.len() % 2 != 0 {
72 return Err(RedisParseError::new_custom("to_map", "Invalid hashmap frame length."));
73 }
74
75 let mut out = resp3_utils::new_map(data.len() / 2);
76 while data.len() >= 2 {
77 let value = data.pop().unwrap();
78 let key = data.pop().unwrap();
79
80 if key.kind().can_hash() {
81 out.insert(key, value);
82 } else {
83 return Err(RedisParseError::new_custom("to_map", "Invalid map key."));
84 }
85 }
86
87 Ok(out)
88}
89
90fn to_set<T>(data: Vec<RangeFrame>) -> Result<FrameSet<RangeFrame>, RedisParseError<T>> {
91 data
92 .into_iter()
93 .map(|value| {
94 if value.kind().can_hash() {
95 Ok(value)
96 } else {
97 Err(RedisParseError::new_custom("to_set", "Invalid hash key."))
98 }
99 })
100 .collect()
101}
102
103fn attach_attributes<T>(
104 attributes: RangeAttributes,
105 mut frame: DecodedRangeFrame,
106) -> Result<DecodedRangeFrame, RedisParseError<T>> {
107 if let Err(e) = frame.add_attributes(attributes) {
108 Err(RedisParseError::new_custom("attach_attributes", format!("{:?}", e)))
109 } else {
110 Ok(frame)
111 }
112}
113
114fn d_read_to_crlf(input: (&[u8], usize)) -> DResult<usize> {
115 decode_log_str!(input.0, _input, "Parsing to CRLF. Remaining: {:?}", input);
116 let (input_bytes, data) = nom_terminated(nom_take_until(CRLF.as_bytes()), nom_take(2_usize))(input.0)?;
117 Ok(((input_bytes, input.1 + data.len() + 2), data.len()))
118}
119
120fn d_read_to_crlf_take(input: (&[u8], usize)) -> DResult<&[u8]> {
121 decode_log_str!(input.0, _input, "Parsing to CRLF. Remaining: {:?}", _input);
122 let (input_bytes, data) = nom_terminated(nom_take_until(CRLF.as_bytes()), nom_take(2_usize))(input.0)?;
123 Ok(((input_bytes, input.1 + data.len() + 2), data))
124}
125
126fn d_read_prefix_len(input: (&[u8], usize)) -> DResult<usize> {
127 let ((input, offset), data) = d_read_to_crlf_take(input)?;
128 decode_log!("Reading prefix len. Data: {:?}", str::from_utf8(data));
129 Ok(((input, offset), etry!(parse_as::<usize, _>(data))))
130}
131
132fn d_read_prefix_len_signed(input: (&[u8], usize)) -> DResult<isize> {
133 let ((input, offset), data) = d_read_to_crlf_take(input)?;
134 decode_log!("Reading prefix len. Data: {:?}", str::from_utf8(data));
135 Ok(((input, offset), etry!(to_isize(data))))
136}
137
138fn d_frame_type(input: (&[u8], usize)) -> DResult<FrameKind> {
139 let (input_bytes, byte) = nom_be_u8(input.0)?;
140
141 match FrameKind::from_byte(byte) {
142 Some(kind) => {
143 decode_log_str!(
144 input_bytes,
145 _input,
146 "Parsed frame type {:?}, remaining: {:?}",
147 kind,
148 _input
149 );
150
151 Ok(((input_bytes, input.1 + 1), kind))
152 },
153 None => {
154 if byte == b'H' {
155 Ok((input, FrameKind::Hello))
157 } else {
158 e!(RedisParseError::new_custom("frame_type", "Invalid frame type prefix."))
159 }
160 },
161 }
162}
163
164fn d_parse_simplestring(input: (&[u8], usize)) -> DResult<RangeFrame> {
165 let offset = input.1;
166 let ((input, next_offset), len) = d_read_to_crlf(input)?;
167 Ok(((input, next_offset), RangeFrame::SimpleString {
168 data: (offset, offset + len),
169 attributes: None,
170 }))
171}
172
173fn d_parse_simpleerror(input: (&[u8], usize)) -> DResult<RangeFrame> {
174 let offset = input.1;
175 let ((input, next_offset), len) = d_read_to_crlf(input)?;
176 Ok(((input, next_offset), RangeFrame::SimpleError {
177 data: (offset, offset + len),
178 attributes: None,
179 }))
180}
181
182fn d_parse_number(input: (&[u8], usize)) -> DResult<RangeFrame> {
183 let ((input, next_offset), data) = d_read_to_crlf_take(input)?;
184 let parsed = etry!(parse_as::<i64, _>(data));
185 Ok(((input, next_offset), RangeFrame::Number {
186 data: parsed,
187 attributes: None,
188 }))
189}
190
191fn d_parse_double(input: (&[u8], usize)) -> DResult<RangeFrame> {
192 let ((input, next_offset), data) = d_read_to_crlf_take(input)?;
193 let parsed = etry!(parse_as::<f64, _>(data));
194 Ok(((input, next_offset), RangeFrame::Double {
195 data: parsed,
196 attributes: None,
197 }))
198}
199
200fn d_parse_boolean(input: (&[u8], usize)) -> DResult<RangeFrame> {
201 let ((input, next_offset), data) = d_read_to_crlf_take(input)?;
202 let parsed = etry!(to_bool(data));
203 Ok(((input, next_offset), RangeFrame::Boolean {
204 data: parsed,
205 attributes: None,
206 }))
207}
208
209fn d_parse_null(input: (&[u8], usize)) -> DResult<RangeFrame> {
210 let ((input, next_offset), _) = d_read_to_crlf(input)?;
211 Ok(((input, next_offset), RangeFrame::Null))
212}
213
214fn d_parse_blobstring(input: (&[u8], usize), len: usize) -> DResult<RangeFrame> {
215 let offset = input.1;
216 let (input, data) = nom_terminated(nom_take(len), nom_take(2_usize))(input.0)?;
217
218 Ok(((input, offset + len + 2), RangeFrame::BlobString {
219 data: (offset, offset + data.len()),
220 attributes: None,
221 }))
222}
223
224fn d_parse_bloberror(input: (&[u8], usize)) -> DResult<RangeFrame> {
225 let ((input, offset), len) = d_read_prefix_len(input)?;
226 let (input, data) = nom_terminated(nom_take(len), nom_take(2_usize))(input)?;
227
228 Ok(((input, offset + len + 2), RangeFrame::BlobError {
229 data: (offset, offset + data.len()),
230 attributes: None,
231 }))
232}
233
234fn d_parse_verbatimstring(input: (&[u8], usize)) -> DResult<RangeFrame> {
235 let ((input, prefix_offset), len) = d_read_prefix_len(input)?;
236 let (input, format_bytes) = nom_terminated(nom_take(3_usize), nom_take(1_usize))(input)?;
237 if len < 4 {
238 e!(RedisParseError::new_custom(
239 "parse_verbatimstring",
240 "Invalid prefix length."
241 ));
242 }
243 let (input, _) = nom_terminated(nom_take(len - 4), nom_take(2_usize))(input)?;
244
245 Ok((
246 (input, prefix_offset.saturating_add(len).saturating_add(2)),
247 RangeFrame::VerbatimString {
248 data: (prefix_offset + 4, prefix_offset.saturating_add(len)),
249 format: etry!(to_verbatimstring_format(format_bytes)),
250 attributes: None,
251 },
252 ))
253}
254
255fn d_parse_bignumber(input: (&[u8], usize)) -> DResult<RangeFrame> {
256 let offset = input.1;
257 let ((input, next_offset), len) = d_read_to_crlf(input)?;
258
259 Ok(((input, next_offset), RangeFrame::BigNumber {
260 data: (offset, offset + len),
261 attributes: None,
262 }))
263}
264
265fn d_parse_array_frames(input: (&[u8], usize), len: usize) -> DResult<Vec<RangeFrame>> {
266 nom_count(
267 nom_map_res(d_parse_frame_or_attribute, expect_complete_index_frame::<&[u8]>),
268 len,
269 )(input)
270}
271
272fn d_parse_kv_pairs(input: (&[u8], usize), len: usize) -> DResult<FrameMap<RangeFrame, RangeFrame>> {
273 nom_map_res(
274 nom_count(
275 nom_map_res(d_parse_frame_or_attribute, expect_complete_index_frame::<&[u8]>),
276 len.saturating_mul(2),
277 ),
278 to_map::<&[u8]>,
279 )(input)
280}
281
282fn d_parse_array(input: (&[u8], usize), len: usize) -> DResult<RangeFrame> {
283 let (input, data) = d_parse_array_frames(input, len)?;
284 Ok((input, RangeFrame::Array { data, attributes: None }))
285}
286
287fn d_parse_push(input: (&[u8], usize)) -> DResult<RangeFrame> {
288 let (input, len) = d_read_prefix_len(input)?;
289 let (input, data) = d_parse_array_frames(input, len)?;
290 Ok((input, RangeFrame::Push { data, attributes: None }))
291}
292
293fn d_parse_set(input: (&[u8], usize), len: usize) -> DResult<RangeFrame> {
294 let (input, frames) = d_parse_array_frames(input, len)?;
295
296 Ok((input, RangeFrame::Set {
297 data: etry!(to_set(frames)),
298 attributes: None,
299 }))
300}
301
302fn d_parse_map(input: (&[u8], usize), len: usize) -> DResult<RangeFrame> {
303 let (input, frames) = d_parse_kv_pairs(input, len)?;
304
305 Ok((input, RangeFrame::Map {
306 data: frames,
307 attributes: None,
308 }))
309}
310
311fn d_parse_attribute(input: (&[u8], usize)) -> DResult<RangeAttributes> {
312 let (input, len) = d_read_prefix_len(input)?;
313 let (input, attributes) = d_parse_kv_pairs(input, len)?;
314 Ok((input, attributes))
315}
316
317fn d_parse_hello(input: (&[u8], usize)) -> DResult<RangeFrame> {
318 let start_offset = input.1;
322 let ((input, offset), data) = d_read_to_crlf_take(input)?;
323 let parts: Vec<_> = data.split(|b| *b == b' ').collect();
324
325 if parts.len() < 2 || parts[0] != HELLO.as_bytes() {
326 e!(RedisParseError::new_custom("parse_hello", "Invalid HELLO."));
327 }
328 let version = match etry!(str::from_utf8(parts[1])) {
329 "2" => RespVersion::RESP2,
330 "3" => RespVersion::RESP3,
331 _ => e!(RedisParseError::new_custom("parse_hello", "Invalid RESP version")),
332 };
333
334 let (username, password, setname) = if parts.len() == 5 {
335 if parts[2] == AUTH.as_bytes() {
337 let username_start = start_offset + HELLO.as_bytes().len() + 3 + AUTH.as_bytes().len() + 1;
338 let username_end = username_start + parts[3].len();
339 let password_start = username_end + 1;
340 let password_end = password_start + parts[4].len();
341
342 (
343 Some((username_start, username_end)),
344 Some((password_start, password_end)),
345 None,
346 )
347 } else {
348 e!(RedisParseError::new_custom("parse_hello", "Invalid AUTH"))
349 }
350 } else if parts.len() == 7 {
351 if parts[2] == AUTH.as_bytes() && parts[5] == SETNAME.as_bytes() {
353 let username_start = start_offset + HELLO.as_bytes().len() + 3 + AUTH.as_bytes().len() + 1;
354 let username_end = username_start + parts[3].len();
355 let password_start = username_end + 1;
356 let password_end = password_start + parts[4].len();
357 let name_start = password_end + 1 + SETNAME.as_bytes().len() + 1;
358 let name_end = name_start + parts[6].len();
359
360 (
361 Some((username_start, username_end)),
362 Some((password_start, password_end)),
363 Some((name_start, name_end)),
364 )
365 } else {
366 e!(RedisParseError::new_custom("parse_hello", "Invalid AUTH or SETNAME"))
367 }
368 } else if parts.len() == 4 {
369 if parts[2] == SETNAME.as_bytes() {
371 let name_start = start_offset + HELLO.as_bytes().len() + 3 + SETNAME.as_bytes().len() + 1;
372 let name_end = name_start + parts[3].len();
373
374 (None, None, Some((name_start, name_end)))
375 } else {
376 e!(RedisParseError::new_custom("parse_hello", "Invalid SETNAME"))
377 }
378 } else if parts.len() == 2 {
379 (None, None, None)
380 } else {
381 e!(RedisParseError::new_custom("parse_hello", "Invalid HELLO arguments"))
382 };
383
384 Ok(((input, offset), RangeFrame::Hello {
385 version,
386 username,
387 password,
388 setname,
389 }))
390}
391
392fn d_check_streaming(input: (&[u8], usize), kind: FrameKind) -> DResult<DecodedRangeFrame> {
397 let (input, len) = d_read_prefix_len_signed(input)?;
398 let (input, frame) = if len == -1 {
399 (input, DecodedRangeFrame::Streaming(StreamedRangeFrame::new(kind)))
400 } else {
401 let len = etry!(utils::isize_to_usize(len));
402 let (input, frame) = match kind {
403 FrameKind::Array => d_parse_array(input, len)?,
404 FrameKind::Set => d_parse_set(input, len)?,
405 FrameKind::Map => d_parse_map(input, len)?,
406 FrameKind::BlobString => d_parse_blobstring(input, len)?,
407 _ => e!(RedisParseError::new_custom(
408 "check_streaming",
409 format!("Invalid frame type: {:?}", kind)
410 )),
411 };
412
413 (input, DecodedRangeFrame::Complete(frame))
414 };
415
416 Ok((input, frame))
417}
418
419fn d_parse_chunked_string(input: (&[u8], usize)) -> DResult<DecodedRangeFrame> {
420 let (input, len) = d_read_prefix_len(input)?;
421 let (input, frame) = if len == 0 {
422 (input, RangeFrame::new_end_stream())
423 } else {
424 let offset = input.1;
425 let (input, contents) = nom_terminated(nom_take(len), nom_take(2_usize))(input.0)?;
426
427 (
428 (input, offset + contents.len() + 2),
429 RangeFrame::ChunkedString((offset, offset + len)),
430 )
431 };
432
433 Ok((input, DecodedRangeFrame::Complete(frame)))
434}
435
436fn d_return_end_stream(input: (&[u8], usize)) -> DResult<DecodedRangeFrame> {
437 let (input, _) = d_read_to_crlf(input)?;
438 Ok((input, DecodedRangeFrame::Complete(RangeFrame::new_end_stream())))
439}
440
441fn d_parse_non_attribute_frame(input: (&[u8], usize), kind: FrameKind) -> DResult<DecodedRangeFrame> {
442 let (input, frame) = match kind {
443 FrameKind::Array => d_check_streaming(input, kind)?,
444 FrameKind::BlobString => d_check_streaming(input, kind)?,
445 FrameKind::Map => d_check_streaming(input, kind)?,
446 FrameKind::Set => d_check_streaming(input, kind)?,
447 FrameKind::SimpleString => nom_map(d_parse_simplestring, map_complete_frame)(input)?,
448 FrameKind::SimpleError => nom_map(d_parse_simpleerror, map_complete_frame)(input)?,
449 FrameKind::Number => nom_map(d_parse_number, map_complete_frame)(input)?,
450 FrameKind::Null => nom_map(d_parse_null, map_complete_frame)(input)?,
451 FrameKind::Double => nom_map(d_parse_double, map_complete_frame)(input)?,
452 FrameKind::Boolean => nom_map(d_parse_boolean, map_complete_frame)(input)?,
453 FrameKind::BlobError => nom_map(d_parse_bloberror, map_complete_frame)(input)?,
454 FrameKind::VerbatimString => nom_map(d_parse_verbatimstring, map_complete_frame)(input)?,
455 FrameKind::Push => nom_map(d_parse_push, map_complete_frame)(input)?,
456 FrameKind::BigNumber => nom_map(d_parse_bignumber, map_complete_frame)(input)?,
457 FrameKind::Hello => nom_map(d_parse_hello, map_complete_frame)(input)?,
458 FrameKind::ChunkedString => d_parse_chunked_string(input)?,
459 FrameKind::EndStream => d_return_end_stream(input)?,
460 FrameKind::Attribute => {
461 e!(RedisParseError::new_custom(
462 "parse_non_attribute_frame",
463 "Unexpected attribute frame.",
464 ));
465 },
466 };
467
468 Ok((input, frame))
469}
470
471fn d_parse_attribute_and_frame(input: (&[u8], usize)) -> DResult<DecodedRangeFrame> {
472 let (input, attributes) = d_parse_attribute(input)?;
473 let (input, kind) = d_frame_type(input)?;
474 let (input, next_frame) = d_parse_non_attribute_frame(input, kind)?;
475 let frame = etry!(attach_attributes(attributes, next_frame));
476
477 Ok((input, frame))
478}
479
480fn d_parse_frame_or_attribute(input: (&[u8], usize)) -> DResult<DecodedRangeFrame> {
481 let (input, kind) = d_frame_type(input)?;
482 let (input, frame) = if kind == FrameKind::Attribute {
483 d_parse_attribute_and_frame(input)?
484 } else {
485 d_parse_non_attribute_frame(input, kind)?
486 };
487
488 Ok((input, frame))
489}
490
491pub mod complete {
493 use super::*;
494
495 pub fn decode_range(buf: &[u8]) -> Result<Option<(RangeFrame, usize)>, RedisProtocolError> {
500 let (offset, _len) = (0, buf.len());
501
502 let (frame, amt) = match d_parse_frame_or_attribute((buf, offset)) {
503 Ok(((_remaining, offset), frame)) => {
504 #[cfg(feature = "std")]
505 debug_assert_eq!(offset, _len - _remaining.len(), "returned offset doesn't match");
506 (frame, offset)
507 },
508 Err(NomErr::Incomplete(_)) => return Ok(None),
509 Err(NomErr::Error(e)) => return Err(e.into()),
510 Err(NomErr::Failure(e)) => return Err(e.into()),
511 };
512
513 Ok(Some((frame.into_complete_frame()?, amt)))
514 }
515
516 pub fn decode(buf: &[u8]) -> Result<Option<(OwnedFrame, usize)>, RedisProtocolError> {
520 let (frame, amt) = match decode_range(buf)? {
521 Some(result) => result,
522 None => return Ok(None),
523 };
524
525 resp3_utils::build_owned_frame(buf, &frame).map(|f| Some((f, amt)))
526 }
527
528 #[cfg(feature = "bytes")]
534 #[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
535 pub fn decode_bytes(buf: &Bytes) -> Result<Option<(BytesFrame, usize)>, RedisProtocolError> {
536 let (frame, amt) = match decode_range(buf)? {
537 Some(result) => result,
538 None => return Ok(None),
539 };
540
541 resp3_utils::build_bytes_frame(buf, &frame).map(|f| Some((f, amt)))
542 }
543
544 #[cfg(feature = "bytes")]
551 #[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
552 pub fn decode_bytes_mut(buf: &mut BytesMut) -> Result<Option<(BytesFrame, usize, Bytes)>, RedisProtocolError> {
553 let (frame, amt) = match decode_range(buf)? {
554 Some(result) => result,
555 None => return Ok(None),
556 };
557
558 resp3_utils::freeze_parse(buf, &frame, amt).map(|(f, b)| Some((f, amt, b)))
559 }
560}
561
562pub mod streaming {
564 use super::*;
565
566 pub fn decode_range(buf: &[u8]) -> Result<Option<(DecodedRangeFrame, usize)>, RedisProtocolError> {
571 let (offset, _len) = (0, buf.len());
572
573 match d_parse_frame_or_attribute((buf, offset)) {
574 Ok(((_remaining, offset), frame)) => {
575 #[cfg(feature = "std")]
576 debug_assert_eq!(offset, _len - _remaining.len(), "returned offset doesn't match");
577 Ok(Some((frame, offset)))
578 },
579 Err(NomErr::Incomplete(_)) => Ok(None),
580 Err(NomErr::Error(e)) => Err(e.into()),
581 Err(NomErr::Failure(e)) => Err(e.into()),
582 }
583 }
584
585 pub fn decode(buf: &[u8]) -> Result<Option<(DecodedFrame<OwnedFrame>, usize)>, RedisProtocolError> {
589 Ok(match decode_range(buf)? {
590 Some((DecodedRangeFrame::Complete(frame), amt)) => Some((
591 DecodedFrame::Complete(resp3_utils::build_owned_frame(buf, &frame)?),
592 amt,
593 )),
594 Some((DecodedRangeFrame::Streaming(frame), amt)) => Some((
595 DecodedFrame::Streaming(resp3_utils::build_owned_streaming_frame(buf, &frame)?),
596 amt,
597 )),
598 None => None,
599 })
600 }
601
602 #[cfg(feature = "bytes")]
608 #[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
609 pub fn decode_bytes(buf: &Bytes) -> Result<Option<(DecodedFrame<BytesFrame>, usize)>, RedisProtocolError> {
610 Ok(match decode_range(buf)? {
611 Some((DecodedRangeFrame::Complete(frame), amt)) => Some((
612 DecodedFrame::Complete(resp3_utils::build_bytes_frame(buf, &frame)?),
613 amt,
614 )),
615 Some((DecodedRangeFrame::Streaming(frame), amt)) => Some((
616 DecodedFrame::Streaming(resp3_utils::build_bytes_streaming_frame(buf, &frame)?),
617 amt,
618 )),
619 _ => None,
620 })
621 }
622
623 #[cfg(feature = "bytes")]
630 #[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
631 pub fn decode_bytes_mut(
632 buf: &mut BytesMut,
633 ) -> Result<Option<(DecodedFrame<BytesFrame>, usize, Bytes)>, RedisProtocolError> {
634 let (frame, amt) = match decode_range(buf)? {
635 Some(result) => result,
636 None => return Ok(None),
637 };
638 let buf = buf.split_to(amt).freeze();
639
640 Ok(match frame {
641 DecodedRangeFrame::Complete(frame) => Some((
642 DecodedFrame::Complete(resp3_utils::build_bytes_frame(&buf, &frame)?),
643 amt,
644 buf,
645 )),
646 DecodedRangeFrame::Streaming(frame) => Some((
647 DecodedFrame::Streaming(resp3_utils::build_bytes_streaming_frame(&buf, &frame)?),
648 amt,
649 buf,
650 )),
651 })
652 }
653}
654
655#[cfg(test)]
656pub mod owned_tests {
657 use super::*;
658 use crate::resp3::decode::{complete::decode, streaming::decode as stream_decode};
659 use std::str;
660
661 pub const PADDING: &str = "FOOBARBAZ";
662
663 fn decode_and_verify_some(bytes: &[u8], expected: &(Option<OwnedFrame>, usize)) {
664 let (frame, len) = match complete::decode(bytes) {
665 Ok(Some((f, l))) => (Some(f), l),
666 Ok(None) => panic!("Failed to decode bytes. None returned."),
667 Err(e) => panic!("{:?}", e),
668 };
669
670 assert_eq!(frame, expected.0, "decoded frame matched");
671 assert_eq!(len, expected.1, "decoded frame len matched");
672 }
673
674 fn decode_and_verify_padded_some(bytes: &[u8], expected: &(Option<OwnedFrame>, usize)) {
675 let mut bytes = bytes.to_vec();
676 bytes.extend_from_slice(PADDING.as_bytes());
677
678 let (frame, len) = match complete::decode(&bytes) {
679 Ok(Some((f, l))) => (Some(f), l),
680 Ok(None) => panic!("Failed to decode bytes. None returned."),
681 Err(e) => panic!("{:?}", e),
682 };
683
684 assert_eq!(frame, expected.0, "decoded frame matched");
685 assert_eq!(len, expected.1, "decoded frame len matched");
686 }
687
688 fn decode_and_verify_none(bytes: &[u8]) {
689 let (frame, len) = match complete::decode(bytes) {
690 Ok(Some((f, l))) => (Some(f), l),
691 Ok(None) => (None, 0),
692 Err(e) => panic!("{:?}", e),
693 };
694
695 assert!(frame.is_none());
696 assert_eq!(len, 0);
697 }
698
699 #[test]
702 fn should_decode_llen_res_example() {
703 let expected = (
704 Some(OwnedFrame::Number {
705 data: 48293,
706 attributes: None,
707 }),
708 8,
709 );
710 let bytes: Vec<u8> = ":48293\r\n".into();
711
712 decode_and_verify_some(&bytes, &expected);
713 decode_and_verify_padded_some(&bytes, &expected);
714 }
715
716 #[test]
717 fn should_decode_simple_string() {
718 let expected = (
719 Some(OwnedFrame::SimpleString {
720 data: "string".into(),
721 attributes: None,
722 }),
723 9,
724 );
725 let bytes: Vec<u8> = "+string\r\n".into();
726
727 decode_and_verify_some(&bytes, &expected);
728 decode_and_verify_padded_some(&bytes, &expected);
729 }
730
731 #[test]
732 #[should_panic]
733 fn should_decode_simple_string_incomplete() {
734 let expected = (
735 Some(OwnedFrame::SimpleString {
736 data: "string".into(),
737 attributes: None,
738 }),
739 9,
740 );
741 let bytes: Vec<u8> = "+stri".into();
742
743 decode_and_verify_some(&bytes, &expected);
744 decode_and_verify_padded_some(&bytes, &expected);
745 }
746
747 #[test]
748 fn should_decode_blob_string() {
749 let expected = (
750 Some(OwnedFrame::BlobString {
751 data: "foo".into(),
752 attributes: None,
753 }),
754 9,
755 );
756 let bytes: Vec<u8> = b"$3\r\nfoo\r\n".into();
757
758 decode_and_verify_some(&bytes, &expected);
759 decode_and_verify_padded_some(&bytes, &expected);
760 }
761
762 #[test]
763 #[should_panic]
764 fn should_decode_blob_string_incomplete() {
765 let expected = (
766 Some(OwnedFrame::BlobString {
767 data: "foo".into(),
768 attributes: None,
769 }),
770 9,
771 );
772 let bytes: Vec<u8> = b"$3\r\nfo".into();
773
774 decode_and_verify_some(&bytes, &expected);
775 decode_and_verify_padded_some(&bytes, &expected);
776 }
777
778 #[test]
779 fn should_decode_array_no_nulls() {
780 let expected = (
781 Some(OwnedFrame::Array {
782 data: vec![
783 OwnedFrame::SimpleString {
784 data: "Foo".into(),
785 attributes: None,
786 },
787 OwnedFrame::SimpleString {
788 data: "Bar".into(),
789 attributes: None,
790 },
791 ],
792 attributes: None,
793 }),
794 16,
795 );
796 let bytes: Vec<u8> = b"*2\r\n+Foo\r\n+Bar\r\n".into();
797
798 decode_and_verify_some(&bytes, &expected);
799 decode_and_verify_padded_some(&bytes, &expected);
800 }
801
802 #[test]
803 fn should_decode_array_nulls() {
804 let bytes: Vec<u8> = b"*3\r\n$3\r\nFoo\r\n_\r\n$3\r\nBar\r\n".into();
805
806 let expected = (
807 Some(OwnedFrame::Array {
808 data: vec![
809 OwnedFrame::BlobString {
810 data: "Foo".into(),
811 attributes: None,
812 },
813 OwnedFrame::Null,
814 OwnedFrame::BlobString {
815 data: "Bar".into(),
816 attributes: None,
817 },
818 ],
819 attributes: None,
820 }),
821 bytes.len(),
822 );
823
824 decode_and_verify_some(&bytes, &expected);
825 decode_and_verify_padded_some(&bytes, &expected);
826 }
827
828 #[test]
829 fn should_decode_normal_error() {
830 let bytes: Vec<u8> = b"-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".into();
831 let expected = (
832 Some(OwnedFrame::SimpleError {
833 data: "WRONGTYPE Operation against a key holding the wrong kind of value".into(),
834 attributes: None,
835 }),
836 bytes.len(),
837 );
838
839 decode_and_verify_some(&bytes, &expected);
840 decode_and_verify_padded_some(&bytes, &expected);
841 }
842
843 #[test]
844 fn should_decode_moved_error() {
845 let bytes: Vec<u8> = b"-MOVED 3999 127.0.0.1:6381\r\n".into();
846 let expected = (
847 Some(OwnedFrame::SimpleError {
848 data: "MOVED 3999 127.0.0.1:6381".into(),
849 attributes: None,
850 }),
851 bytes.len(),
852 );
853
854 decode_and_verify_some(&bytes, &expected);
855 decode_and_verify_padded_some(&bytes, &expected);
856 }
857
858 #[test]
859 fn should_decode_ask_error() {
860 let bytes: Vec<u8> = b"-ASK 3999 127.0.0.1:6381\r\n".into();
861 let expected = (
862 Some(OwnedFrame::SimpleError {
863 data: "ASK 3999 127.0.0.1:6381".into(),
864 attributes: None,
865 }),
866 bytes.len(),
867 );
868
869 decode_and_verify_some(&bytes, &expected);
870 decode_and_verify_padded_some(&bytes, &expected);
871 }
872
873 #[test]
874 fn should_decode_incomplete() {
875 let bytes: Vec<u8> = b"*3\r\n$3\r\nFoo\r\n_\r\n$3\r\nBar".into();
876 decode_and_verify_none(&bytes);
877 }
878
879 #[test]
880 #[should_panic]
881 fn should_error_on_junk() {
882 complete::decode(b"foobarbazwibblewobble").unwrap();
883 }
884
885 #[test]
888 fn should_decode_blob_error() {
889 let expected = (
890 Some(OwnedFrame::BlobError {
891 data: "foo".into(),
892 attributes: None,
893 }),
894 9,
895 );
896 let bytes: Vec<u8> = b"!3\r\nfoo\r\n".into();
897
898 decode_and_verify_some(&bytes, &expected);
899 decode_and_verify_padded_some(&bytes, &expected);
900 }
901
902 #[test]
903 #[should_panic]
904 fn should_decode_blob_error_incomplete() {
905 let expected = (
906 Some(OwnedFrame::BlobError {
907 data: "foo".into(),
908 attributes: None,
909 }),
910 9,
911 );
912 let bytes: Vec<u8> = b"!3\r\nfo".into();
913
914 decode_and_verify_some(&bytes, &expected);
915 decode_and_verify_padded_some(&bytes, &expected);
916 }
917
918 #[test]
919 fn should_decode_simple_error() {
920 let expected = (
921 Some(OwnedFrame::SimpleError {
922 data: "string".into(),
923 attributes: None,
924 }),
925 9,
926 );
927 let bytes: Vec<u8> = b"-string\r\n".into();
928
929 decode_and_verify_some(&bytes, &expected);
930 decode_and_verify_padded_some(&bytes, &expected);
931 }
932
933 #[test]
934 #[should_panic]
935 fn should_decode_simple_error_incomplete() {
936 let expected = (
937 Some(OwnedFrame::SimpleError {
938 data: "string".into(),
939 attributes: None,
940 }),
941 9,
942 );
943 let bytes: Vec<u8> = b"-strin".into();
944
945 decode_and_verify_some(&bytes, &expected);
946 decode_and_verify_padded_some(&bytes, &expected);
947 }
948
949 #[test]
950 fn should_decode_boolean_true() {
951 let expected = (
952 Some(OwnedFrame::Boolean {
953 data: true,
954 attributes: None,
955 }),
956 4,
957 );
958 let bytes: Vec<u8> = b"#t\r\n".into();
959
960 decode_and_verify_some(&bytes, &expected);
961 decode_and_verify_padded_some(&bytes, &expected);
962 }
963
964 #[test]
965 fn should_decode_boolean_false() {
966 let expected = (
967 Some(OwnedFrame::Boolean {
968 data: false,
969 attributes: None,
970 }),
971 4,
972 );
973 let bytes: Vec<u8> = b"#f\r\n".into();
974
975 decode_and_verify_some(&bytes, &expected);
976 decode_and_verify_padded_some(&bytes, &expected);
977 }
978
979 #[test]
980 fn should_decode_number() {
981 let expected = (
982 Some(OwnedFrame::Number {
983 data: 42,
984 attributes: None,
985 }),
986 5,
987 );
988 let bytes: Vec<u8> = b":42\r\n".into();
989
990 decode_and_verify_some(&bytes, &expected);
991 decode_and_verify_padded_some(&bytes, &expected);
992 }
993
994 #[test]
995 fn should_decode_double_inf() {
996 let expected = (
997 Some(OwnedFrame::Double {
998 data: f64::INFINITY,
999 attributes: None,
1000 }),
1001 6,
1002 );
1003 let bytes: Vec<u8> = b",inf\r\n".into();
1004
1005 decode_and_verify_some(&bytes, &expected);
1006 decode_and_verify_padded_some(&bytes, &expected);
1007 }
1008
1009 #[test]
1010 fn should_decode_double_neg_inf() {
1011 let expected = (
1012 Some(OwnedFrame::Double {
1013 data: f64::NEG_INFINITY,
1014 attributes: None,
1015 }),
1016 7,
1017 );
1018 let bytes: Vec<u8> = b",-inf\r\n".into();
1019
1020 decode_and_verify_some(&bytes, &expected);
1021 decode_and_verify_padded_some(&bytes, &expected);
1022 }
1023
1024 #[test]
1025 #[should_panic]
1026 fn should_decode_double_nan() {
1027 let expected = (
1028 Some(OwnedFrame::Double {
1029 data: f64::NAN,
1030 attributes: None,
1031 }),
1032 7,
1033 );
1034 let bytes: Vec<u8> = b",foo\r\n".into();
1035
1036 decode_and_verify_some(&bytes, &expected);
1037 decode_and_verify_padded_some(&bytes, &expected);
1038 }
1039
1040 #[test]
1041 fn should_decode_double() {
1042 let expected = (
1043 Some(OwnedFrame::Double {
1044 data: 4.59193,
1045 attributes: None,
1046 }),
1047 10,
1048 );
1049 let bytes: Vec<u8> = b",4.59193\r\n".into();
1050
1051 decode_and_verify_some(&bytes, &expected);
1052 decode_and_verify_padded_some(&bytes, &expected);
1053
1054 let expected = (
1055 Some(OwnedFrame::Double {
1056 data: 4_f64,
1057 attributes: None,
1058 }),
1059 4,
1060 );
1061 let bytes: Vec<u8> = b",4\r\n".into();
1062
1063 decode_and_verify_some(&bytes, &expected);
1064 decode_and_verify_padded_some(&bytes, &expected);
1065 }
1066
1067 #[test]
1068 fn should_decode_bignumber() {
1069 let expected = (
1070 Some(OwnedFrame::BigNumber {
1071 data: "3492890328409238509324850943850943825024385".into(),
1072 attributes: None,
1073 }),
1074 46,
1075 );
1076 let bytes: Vec<u8> = b"(3492890328409238509324850943850943825024385\r\n".into();
1077
1078 decode_and_verify_some(&bytes, &expected);
1079 decode_and_verify_padded_some(&bytes, &expected);
1080 }
1081
1082 #[test]
1083 fn should_decode_null() {
1084 let expected = (Some(OwnedFrame::Null), 3);
1085 let bytes: Vec<u8> = b"_\r\n".into();
1086
1087 decode_and_verify_some(&bytes, &expected);
1088 decode_and_verify_padded_some(&bytes, &expected);
1089 }
1090
1091 #[test]
1092 fn should_decode_verbatim_string_mkd() {
1093 let expected = (
1094 Some(OwnedFrame::VerbatimString {
1095 data: "Some string".into(),
1096 format: VerbatimStringFormat::Markdown,
1097 attributes: None,
1098 }),
1099 22,
1100 );
1101 let bytes: Vec<u8> = b"=15\r\nmkd:Some string\r\n".into();
1102
1103 decode_and_verify_some(&bytes, &expected);
1104 decode_and_verify_padded_some(&bytes, &expected);
1105 }
1106
1107 #[test]
1108 fn should_decode_verbatim_string_txt() {
1109 let expected = (
1110 Some(OwnedFrame::VerbatimString {
1111 data: "Some string".into(),
1112 format: VerbatimStringFormat::Text,
1113 attributes: None,
1114 }),
1115 22,
1116 );
1117 let bytes: Vec<u8> = b"=15\r\ntxt:Some string\r\n".into();
1118
1119 decode_and_verify_some(&bytes, &expected);
1120 decode_and_verify_padded_some(&bytes, &expected);
1121 }
1122
1123 #[test]
1124 fn should_decode_map_no_nulls() {
1125 let k1 = OwnedFrame::SimpleString {
1126 data: "first".into(),
1127 attributes: None,
1128 };
1129 let v1 = OwnedFrame::Number {
1130 data: 1,
1131 attributes: None,
1132 };
1133 let k2 = OwnedFrame::BlobString {
1134 data: "second".into(),
1135 attributes: None,
1136 };
1137 let v2 = OwnedFrame::Double {
1138 data: 4.2,
1139 attributes: None,
1140 };
1141
1142 let mut expected_map = resp3_utils::new_map(0);
1143 expected_map.insert(k1, v1);
1144 expected_map.insert(k2, v2);
1145 let expected = (
1146 Some(OwnedFrame::Map {
1147 data: expected_map,
1148 attributes: None,
1149 }),
1150 34,
1151 );
1152 let bytes: Vec<u8> = b"%2\r\n+first\r\n:1\r\n$6\r\nsecond\r\n,4.2\r\n".into();
1153
1154 decode_and_verify_some(&bytes, &expected);
1155 decode_and_verify_padded_some(&bytes, &expected);
1156 }
1157
1158 #[test]
1159 fn should_decode_map_with_nulls() {
1160 let k1 = OwnedFrame::SimpleString {
1161 data: "first".into(),
1162 attributes: None,
1163 };
1164 let v1 = OwnedFrame::Number {
1165 data: 1,
1166 attributes: None,
1167 };
1168 let k2 = OwnedFrame::Number {
1169 data: 2,
1170 attributes: None,
1171 };
1172 let v2 = OwnedFrame::Null;
1173 let k3 = OwnedFrame::BlobString {
1174 data: "second".into(),
1175 attributes: None,
1176 };
1177 let v3 = OwnedFrame::Double {
1178 data: 4.2,
1179 attributes: None,
1180 };
1181
1182 let mut expected_map = resp3_utils::new_map(0);
1183 expected_map.insert(k1, v1);
1184 expected_map.insert(k2, v2);
1185 expected_map.insert(k3, v3);
1186 let expected = (
1187 Some(OwnedFrame::Map {
1188 data: expected_map,
1189 attributes: None,
1190 }),
1191 41,
1192 );
1193 let bytes: Vec<u8> = b"%3\r\n+first\r\n:1\r\n:2\r\n_\r\n$6\r\nsecond\r\n,4.2\r\n".into();
1194
1195 decode_and_verify_some(&bytes, &expected);
1196 decode_and_verify_padded_some(&bytes, &expected);
1197 }
1198
1199 #[test]
1200 fn should_decode_set_no_nulls() {
1201 let mut expected_set = resp3_utils::new_set(0);
1202 expected_set.insert(OwnedFrame::Number {
1203 data: 1,
1204 attributes: None,
1205 });
1206 expected_set.insert(OwnedFrame::SimpleString {
1207 data: "2".into(),
1208 attributes: None,
1209 });
1210 expected_set.insert(OwnedFrame::BlobString {
1211 data: "foobar".into(),
1212 attributes: None,
1213 });
1214 expected_set.insert(OwnedFrame::Double {
1215 data: 4.2,
1216 attributes: None,
1217 });
1218 let expected = (
1219 Some(OwnedFrame::Set {
1220 data: expected_set,
1221 attributes: None,
1222 }),
1223 30,
1224 );
1225 let bytes: Vec<u8> = b"~4\r\n:1\r\n+2\r\n$6\r\nfoobar\r\n,4.2\r\n".into();
1226
1227 decode_and_verify_some(&bytes, &expected);
1228 decode_and_verify_padded_some(&bytes, &expected);
1229 }
1230
1231 #[test]
1232 fn should_decode_set_with_nulls() {
1233 let mut expected_set = resp3_utils::new_set(0);
1234 expected_set.insert(OwnedFrame::Number {
1235 data: 1,
1236 attributes: None,
1237 });
1238 expected_set.insert(OwnedFrame::SimpleString {
1239 data: "2".into(),
1240 attributes: None,
1241 });
1242 expected_set.insert(OwnedFrame::Null);
1243 expected_set.insert(OwnedFrame::Double {
1244 data: 4.2,
1245 attributes: None,
1246 });
1247 let expected = (
1248 Some(OwnedFrame::Set {
1249 data: expected_set,
1250 attributes: None,
1251 }),
1252 21,
1253 );
1254 let bytes: Vec<u8> = b"~4\r\n:1\r\n+2\r\n_\r\n,4.2\r\n".into();
1255
1256 decode_and_verify_some(&bytes, &expected);
1257 decode_and_verify_padded_some(&bytes, &expected);
1258 }
1259
1260 #[test]
1261 fn should_decode_push_pubsub() {
1262 let expected = (
1263 Some(OwnedFrame::Push {
1264 data: vec![
1265 OwnedFrame::SimpleString {
1266 data: "pubsub".into(),
1267 attributes: None,
1268 },
1269 OwnedFrame::SimpleString {
1270 data: "message".into(),
1271 attributes: None,
1272 },
1273 OwnedFrame::SimpleString {
1274 data: "somechannel".into(),
1275 attributes: None,
1276 },
1277 OwnedFrame::SimpleString {
1278 data: "this is the message".into(),
1279 attributes: None,
1280 },
1281 ],
1282 attributes: None,
1283 }),
1284 59,
1285 );
1286 let bytes: Vec<u8> = b">4\r\n+pubsub\r\n+message\r\n+somechannel\r\n+this is the message\r\n".into();
1287
1288 decode_and_verify_some(&bytes, &expected);
1289 decode_and_verify_padded_some(&bytes, &expected);
1290
1291 let (frame, _) = decode(&bytes).unwrap().unwrap();
1292 assert!(frame.is_normal_pubsub_message());
1293 }
1294
1295 #[test]
1296 fn should_decode_push_pattern_pubsub() {
1297 let expected = (
1298 Some(OwnedFrame::Push {
1299 data: vec![
1300 OwnedFrame::SimpleString {
1301 data: "pubsub".into(),
1302 attributes: None,
1303 },
1304 OwnedFrame::SimpleString {
1305 data: "pmessage".into(),
1306 attributes: None,
1307 },
1308 OwnedFrame::SimpleString {
1309 data: "pattern".into(),
1310 attributes: None,
1311 },
1312 OwnedFrame::SimpleString {
1313 data: "somechannel".into(),
1314 attributes: None,
1315 },
1316 OwnedFrame::SimpleString {
1317 data: "this is the message".into(),
1318 attributes: None,
1319 },
1320 ],
1321 attributes: None,
1322 }),
1323 70,
1324 );
1325 let bytes: Vec<u8> = b">5\r\n+pubsub\r\n+pmessage\r\n+pattern\r\n+somechannel\r\n+this is the message\r\n".into();
1326
1327 decode_and_verify_some(&bytes, &expected);
1328 decode_and_verify_padded_some(&bytes, &expected);
1329
1330 let (frame, _) = decode(&bytes).unwrap().unwrap();
1331 assert!(frame.is_pattern_pubsub_message());
1332 }
1333
1334 #[test]
1335 fn should_decode_keyevent_message() {
1336 let expected = (
1337 Some(OwnedFrame::Push {
1338 data: vec![
1339 OwnedFrame::SimpleString {
1340 data: "pubsub".into(),
1341 attributes: None,
1342 },
1343 OwnedFrame::SimpleString {
1344 data: "pmessage".into(),
1345 attributes: None,
1346 },
1347 OwnedFrame::SimpleString {
1348 data: "__key*".into(),
1349 attributes: None,
1350 },
1351 OwnedFrame::SimpleString {
1352 data: "__keyevent@0__:set".into(),
1353 attributes: None,
1354 },
1355 OwnedFrame::SimpleString {
1356 data: "foo".into(),
1357 attributes: None,
1358 },
1359 ],
1360 attributes: None,
1361 }),
1362 60,
1363 );
1364 let bytes: Vec<u8> = b">5\r\n+pubsub\r\n+pmessage\r\n+__key*\r\n+__keyevent@0__:set\r\n+foo\r\n".into();
1365
1366 decode_and_verify_some(&bytes, &expected);
1367 decode_and_verify_padded_some(&bytes, &expected);
1368
1369 let (frame, _) = decode(&bytes).unwrap().unwrap();
1370 assert!(frame.is_pattern_pubsub_message());
1371 }
1372
1373 #[test]
1374 fn should_parse_outer_attributes() {
1375 let mut expected_inner_attrs = resp3_utils::new_map(0);
1376 expected_inner_attrs.insert(
1377 OwnedFrame::BlobString {
1378 data: "a".into(),
1379 attributes: None,
1380 },
1381 OwnedFrame::Double {
1382 data: 0.1923,
1383 attributes: None,
1384 },
1385 );
1386 expected_inner_attrs.insert(
1387 OwnedFrame::BlobString {
1388 data: "b".into(),
1389 attributes: None,
1390 },
1391 OwnedFrame::Double {
1392 data: 0.0012,
1393 attributes: None,
1394 },
1395 );
1396 let expected_inner_attrs = OwnedFrame::Map {
1397 data: expected_inner_attrs,
1398 attributes: None,
1399 };
1400
1401 let mut expected_attrs = resp3_utils::new_map(0);
1402 expected_attrs.insert(
1403 OwnedFrame::SimpleString {
1404 data: "key-popularity".into(),
1405 attributes: None,
1406 },
1407 expected_inner_attrs,
1408 );
1409
1410 let expected = (
1411 Some(OwnedFrame::Array {
1412 data: vec![
1413 OwnedFrame::Number {
1414 data: 2039123,
1415 attributes: None,
1416 },
1417 OwnedFrame::Number {
1418 data: 9543892,
1419 attributes: None,
1420 },
1421 ],
1422 attributes: Some(expected_attrs),
1423 }),
1424 81,
1425 );
1426
1427 let bytes: Vec<u8> =
1428 b"|1\r\n+key-popularity\r\n%2\r\n$1\r\na\r\n,0.1923\r\n$1\r\nb\r\n,0.0012\r\n*2\r\n:2039123\r\n:9543892\r\n"
1429 .into();
1430
1431 decode_and_verify_some(&bytes, &expected);
1432 decode_and_verify_padded_some(&bytes, &expected);
1433 }
1434
1435 #[test]
1436 fn should_parse_inner_attributes() {
1437 let mut expected_attrs = resp3_utils::new_map(0);
1438 expected_attrs.insert(
1439 OwnedFrame::SimpleString {
1440 data: "ttl".into(),
1441 attributes: None,
1442 },
1443 OwnedFrame::Number {
1444 data: 3600,
1445 attributes: None,
1446 },
1447 );
1448
1449 let expected = (
1450 Some(OwnedFrame::Array {
1451 data: vec![
1452 OwnedFrame::Number {
1453 data: 1,
1454 attributes: None,
1455 },
1456 OwnedFrame::Number {
1457 data: 2,
1458 attributes: None,
1459 },
1460 OwnedFrame::Number {
1461 data: 3,
1462 attributes: Some(expected_attrs),
1463 },
1464 ],
1465 attributes: None,
1466 }),
1467 33,
1468 );
1469 let bytes: Vec<u8> = b"*3\r\n:1\r\n:2\r\n|1\r\n+ttl\r\n:3600\r\n:3\r\n".into();
1470
1471 decode_and_verify_some(&bytes, &expected);
1472 decode_and_verify_padded_some(&bytes, &expected);
1473 }
1474
1475 #[test]
1476 fn should_decode_end_stream() {
1477 let bytes: Vec<u8> = b";0\r\n".into();
1478 let (frame, _) = stream_decode(&bytes).unwrap().unwrap();
1479 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::new_end_stream()))
1480 }
1481
1482 #[test]
1483 fn should_decode_streaming_string() {
1484 let mut bytes: Vec<u8> = "$?\r\n;4\r\nHell\r\n;6\r\no worl\r\n;1\r\nd\r\n;0\r\n".into();
1485
1486 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1487 assert_eq!(
1488 frame,
1489 DecodedFrame::Streaming(StreamedFrame::new(FrameKind::BlobString))
1490 );
1491 assert_eq!(amt, 4);
1492 bytes = bytes[amt ..].to_vec();
1493
1494 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1495 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::ChunkedString("Hell".into())));
1496 assert_eq!(amt, 10);
1497 bytes = bytes[amt ..].to_vec();
1498
1499 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1500 assert_eq!(
1501 frame,
1502 DecodedFrame::Complete(OwnedFrame::ChunkedString("o worl".into()))
1503 );
1504 assert_eq!(amt, 12);
1505 bytes = bytes[amt ..].to_vec();
1506
1507 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1508 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::ChunkedString("d".into())));
1509 assert_eq!(amt, 7);
1510 bytes = bytes[amt ..].to_vec();
1511
1512 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1513 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::new_end_stream()));
1514 assert_eq!(amt, 4);
1515 }
1516
1517 #[test]
1518 fn should_decode_streaming_array() {
1519 let mut bytes: Vec<u8> = "*?\r\n:1\r\n:2\r\n:3\r\n.\r\n".into();
1520
1521 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1522 assert_eq!(frame, DecodedFrame::Streaming(StreamedFrame::new(FrameKind::Array)));
1523 assert_eq!(amt, 4);
1524 bytes = bytes[amt ..].to_vec();
1525 let mut streamed = frame.into_streaming_frame().unwrap();
1526
1527 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1528 assert_eq!(
1529 frame,
1530 DecodedFrame::Complete(OwnedFrame::Number {
1531 data: 1,
1532 attributes: None,
1533 })
1534 );
1535 assert_eq!(amt, 4);
1536 bytes = bytes[amt ..].to_vec();
1537 streamed.add_frame(frame.into_complete_frame().unwrap());
1538
1539 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1540 assert_eq!(
1541 frame,
1542 DecodedFrame::Complete(OwnedFrame::Number {
1543 data: 2,
1544 attributes: None,
1545 })
1546 );
1547 assert_eq!(amt, 4);
1548 bytes = bytes[amt ..].to_vec();
1549 streamed.add_frame(frame.into_complete_frame().unwrap());
1550
1551 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1552 assert_eq!(
1553 frame,
1554 DecodedFrame::Complete(OwnedFrame::Number {
1555 data: 3,
1556 attributes: None,
1557 })
1558 );
1559 assert_eq!(amt, 4);
1560 bytes = bytes[amt ..].to_vec();
1561 streamed.add_frame(frame.into_complete_frame().unwrap());
1562
1563 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1564 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::new_end_stream()));
1565 assert_eq!(amt, 3);
1566 streamed.add_frame(frame.into_complete_frame().unwrap());
1567
1568 assert!(streamed.is_finished());
1569 let actual = streamed.take().unwrap();
1570 let expected = OwnedFrame::Array {
1571 data: vec![
1572 OwnedFrame::Number {
1573 data: 1,
1574 attributes: None,
1575 },
1576 OwnedFrame::Number {
1577 data: 2,
1578 attributes: None,
1579 },
1580 OwnedFrame::Number {
1581 data: 3,
1582 attributes: None,
1583 },
1584 ],
1585 attributes: None,
1586 };
1587
1588 assert_eq!(actual, expected);
1589 }
1590
1591 #[test]
1592 fn should_decode_streaming_set() {
1593 let mut bytes: Vec<u8> = "~?\r\n:1\r\n:2\r\n:3\r\n.\r\n".into();
1594
1595 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1596 assert_eq!(frame, DecodedFrame::Streaming(StreamedFrame::new(FrameKind::Set)));
1597 assert_eq!(amt, 4);
1598 bytes = bytes[amt ..].to_vec();
1599 let mut streamed = frame.into_streaming_frame().unwrap();
1600
1601 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1602 assert_eq!(
1603 frame,
1604 DecodedFrame::Complete(OwnedFrame::Number {
1605 data: 1,
1606 attributes: None,
1607 })
1608 );
1609 assert_eq!(amt, 4);
1610 bytes = bytes[amt ..].to_vec();
1611 streamed.add_frame(frame.into_complete_frame().unwrap());
1612
1613 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1614 assert_eq!(
1615 frame,
1616 DecodedFrame::Complete(OwnedFrame::Number {
1617 data: 2,
1618 attributes: None,
1619 })
1620 );
1621 assert_eq!(amt, 4);
1622 bytes = bytes[amt ..].to_vec();
1623 streamed.add_frame(frame.into_complete_frame().unwrap());
1624
1625 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1626 assert_eq!(
1627 frame,
1628 DecodedFrame::Complete(OwnedFrame::Number {
1629 data: 3,
1630 attributes: None,
1631 })
1632 );
1633 assert_eq!(amt, 4);
1634 bytes = bytes[amt ..].to_vec();
1635 streamed.add_frame(frame.into_complete_frame().unwrap());
1636
1637 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1638 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::new_end_stream()));
1639 assert_eq!(amt, 3);
1640 streamed.add_frame(frame.into_complete_frame().unwrap());
1641
1642 assert!(streamed.is_finished());
1643 let actual = streamed.take().unwrap();
1644 let mut expected_result = resp3_utils::new_set(0);
1645 expected_result.insert(OwnedFrame::Number {
1646 data: 1,
1647 attributes: None,
1648 });
1649 expected_result.insert(OwnedFrame::Number {
1650 data: 2,
1651 attributes: None,
1652 });
1653 expected_result.insert(OwnedFrame::Number {
1654 data: 3,
1655 attributes: None,
1656 });
1657
1658 let expected = OwnedFrame::Set {
1659 data: expected_result,
1660 attributes: None,
1661 };
1662
1663 assert_eq!(actual, expected);
1664 }
1665
1666 #[test]
1667 fn should_decode_streaming_map() {
1668 let mut bytes: Vec<u8> = "%?\r\n+a\r\n:1\r\n+b\r\n:2\r\n.\r\n".into();
1669
1670 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1671 assert_eq!(frame, DecodedFrame::Streaming(StreamedFrame::new(FrameKind::Map)));
1672 assert_eq!(amt, 4);
1673 bytes = bytes[amt ..].to_vec();
1674 let mut streamed = frame.into_streaming_frame().unwrap();
1675
1676 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1677 assert_eq!(
1678 frame,
1679 DecodedFrame::Complete(OwnedFrame::SimpleString {
1680 data: "a".into(),
1681 attributes: None,
1682 })
1683 );
1684 assert_eq!(amt, 4);
1685 bytes = bytes[amt ..].to_vec();
1686 streamed.add_frame(frame.into_complete_frame().unwrap());
1687
1688 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1689 assert_eq!(
1690 frame,
1691 DecodedFrame::Complete(OwnedFrame::Number {
1692 data: 1.into(),
1693 attributes: None,
1694 })
1695 );
1696 assert_eq!(amt, 4);
1697 bytes = bytes[amt ..].to_vec();
1698 streamed.add_frame(frame.into_complete_frame().unwrap());
1699
1700 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1701 assert_eq!(
1702 frame,
1703 DecodedFrame::Complete(OwnedFrame::SimpleString {
1704 data: "b".into(),
1705 attributes: None,
1706 })
1707 );
1708 assert_eq!(amt, 4);
1709 bytes = bytes[amt ..].to_vec();
1710 streamed.add_frame(frame.into_complete_frame().unwrap());
1711
1712 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1713 assert_eq!(
1714 frame,
1715 DecodedFrame::Complete(OwnedFrame::Number {
1716 data: 2.into(),
1717 attributes: None,
1718 })
1719 );
1720 assert_eq!(amt, 4);
1721 bytes = bytes[amt ..].to_vec();
1722 streamed.add_frame(frame.into_complete_frame().unwrap());
1723
1724 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
1725 assert_eq!(frame, DecodedFrame::Complete(OwnedFrame::new_end_stream()));
1726 assert_eq!(amt, 3);
1727 streamed.add_frame(frame.into_complete_frame().unwrap());
1728
1729 assert!(streamed.is_finished());
1730 let actual = streamed.take().unwrap();
1731 let mut expected_result = resp3_utils::new_map(0);
1732 expected_result.insert(
1733 OwnedFrame::SimpleString {
1734 data: "a".into(),
1735 attributes: None,
1736 },
1737 OwnedFrame::Number {
1738 data: 1,
1739 attributes: None,
1740 },
1741 );
1742 expected_result.insert(
1743 OwnedFrame::SimpleString {
1744 data: "b".into(),
1745 attributes: None,
1746 },
1747 OwnedFrame::Number {
1748 data: 2,
1749 attributes: None,
1750 },
1751 );
1752 let expected = OwnedFrame::Map {
1753 data: expected_result,
1754 attributes: None,
1755 };
1756
1757 assert_eq!(actual, expected);
1758 }
1759
1760 #[test]
1761 fn should_decode_hello_no_auth() {
1762 let expected = (
1763 Some(OwnedFrame::Hello {
1764 version: RespVersion::RESP3,
1765 auth: None,
1766 setname: None,
1767 }),
1768 9,
1769 );
1770 let bytes = "HELLO 3\r\n";
1771
1772 decode_and_verify_some(bytes.as_bytes(), &expected);
1773 decode_and_verify_padded_some(bytes.as_bytes(), &expected);
1774 }
1775
1776 #[test]
1777 fn should_decode_hello_with_auth_no_setname() {
1778 let expected = (
1779 Some(OwnedFrame::Hello {
1780 version: RespVersion::RESP3,
1781 auth: Some(("foo".into(), "bar".into())),
1782 setname: None,
1783 }),
1784 22,
1785 );
1786 let bytes = "HELLO 3 AUTH foo bar\r\n";
1787
1788 decode_and_verify_some(bytes.as_bytes(), &expected);
1789 decode_and_verify_padded_some(bytes.as_bytes(), &expected);
1790 }
1791
1792 #[test]
1793 fn should_decode_hello_with_setname() {
1794 let expected = (
1795 Some(OwnedFrame::Hello {
1796 version: RespVersion::RESP3,
1797 auth: None,
1798 setname: Some("baz".into()),
1799 }),
1800 21,
1801 );
1802 let bytes = "HELLO 3 SETNAME baz\r\n";
1803
1804 decode_and_verify_some(bytes.as_bytes(), &expected);
1805 decode_and_verify_padded_some(bytes.as_bytes(), &expected);
1806 }
1807
1808 #[test]
1809 fn should_decode_hello_with_auth_and_setname() {
1810 let expected = (
1811 Some(OwnedFrame::Hello {
1812 version: RespVersion::RESP3,
1813 auth: Some(("foo".into(), "bar".into())),
1814 setname: Some("baz".into()),
1815 }),
1816 34,
1817 );
1818 let bytes = "HELLO 3 AUTH foo bar SETNAME baz\r\n";
1819
1820 decode_and_verify_some(bytes.as_bytes(), &expected);
1821 decode_and_verify_padded_some(bytes.as_bytes(), &expected);
1822 }
1823}
1824
1825#[cfg(test)]
1826#[cfg(feature = "bytes")]
1827pub mod bytes_tests {
1828 use super::*;
1829 use crate::resp3::decode::{complete::decode, streaming::decode_bytes as stream_decode};
1830 use bytes::{Bytes, BytesMut};
1831 use nom::AsBytes;
1832 use std::str;
1833
1834 const PADDING: &str = "FOOBARBAZ";
1835
1836 fn decode_and_verify_some(bytes: &Bytes, expected: &(Option<BytesFrame>, usize)) {
1837 let (frame, len) = match complete::decode_bytes(bytes) {
1838 Ok(Some((f, l))) => (Some(f), l),
1839 Ok(None) => panic!("Failed to decode bytes. None returned."),
1840 Err(e) => panic!("{:?}", e),
1841 };
1842
1843 assert_eq!(frame, expected.0, "decoded frame matched");
1844 assert_eq!(len, expected.1, "decoded frame len matched");
1845 }
1846
1847 fn decode_and_verify_padded_some(bytes: &Bytes, expected: &(Option<BytesFrame>, usize)) {
1848 let mut bytes = BytesMut::from(bytes.as_bytes());
1849 bytes.extend_from_slice(PADDING.as_bytes());
1850 let bytes = bytes.freeze();
1851
1852 let (frame, len) = match complete::decode_bytes(&bytes) {
1853 Ok(Some((f, l))) => (Some(f), l),
1854 Ok(None) => panic!("Failed to decode bytes. None returned."),
1855 Err(e) => panic!("{:?}", e),
1856 };
1857
1858 assert_eq!(frame, expected.0, "decoded frame matched");
1859 assert_eq!(len, expected.1, "decoded frame len matched");
1860 }
1861
1862 fn decode_and_verify_none(bytes: &Bytes) {
1863 let (frame, len) = match complete::decode_bytes(bytes) {
1864 Ok(Some((f, l))) => (Some(f), l),
1865 Ok(None) => (None, 0),
1866 Err(e) => panic!("{:?}", e),
1867 };
1868
1869 assert!(frame.is_none());
1870 assert_eq!(len, 0);
1871 }
1872
1873 #[test]
1876 fn should_decode_llen_res_example() {
1877 let expected = (
1878 Some(BytesFrame::Number {
1879 data: 48293,
1880 attributes: None,
1881 }),
1882 8,
1883 );
1884 let bytes: Bytes = ":48293\r\n".into();
1885
1886 decode_and_verify_some(&bytes, &expected);
1887 decode_and_verify_padded_some(&bytes, &expected);
1888 }
1889
1890 #[test]
1891 fn should_decode_simple_string() {
1892 let expected = (
1893 Some(BytesFrame::SimpleString {
1894 data: "string".into(),
1895 attributes: None,
1896 }),
1897 9,
1898 );
1899 let bytes: Bytes = "+string\r\n".into();
1900
1901 decode_and_verify_some(&bytes, &expected);
1902 decode_and_verify_padded_some(&bytes, &expected);
1903 }
1904
1905 #[test]
1906 #[should_panic]
1907 fn should_decode_simple_string_incomplete() {
1908 let expected = (
1909 Some(BytesFrame::SimpleString {
1910 data: "string".into(),
1911 attributes: None,
1912 }),
1913 9,
1914 );
1915 let bytes: Bytes = "+stri".into();
1916
1917 decode_and_verify_some(&bytes, &expected);
1918 decode_and_verify_padded_some(&bytes, &expected);
1919 }
1920
1921 #[test]
1922 fn should_decode_blob_string() {
1923 let expected = (
1924 Some(BytesFrame::BlobString {
1925 data: "foo".into(),
1926 attributes: None,
1927 }),
1928 9,
1929 );
1930 let bytes: Bytes = "$3\r\nfoo\r\n".into();
1931
1932 decode_and_verify_some(&bytes, &expected);
1933 decode_and_verify_padded_some(&bytes, &expected);
1934 }
1935
1936 #[test]
1937 #[should_panic]
1938 fn should_decode_blob_string_incomplete() {
1939 let expected = (
1940 Some(BytesFrame::BlobString {
1941 data: "foo".into(),
1942 attributes: None,
1943 }),
1944 9,
1945 );
1946 let bytes: Bytes = "$3\r\nfo".into();
1947
1948 decode_and_verify_some(&bytes, &expected);
1949 decode_and_verify_padded_some(&bytes, &expected);
1950 }
1951
1952 #[test]
1953 fn should_decode_array_no_nulls() {
1954 let expected = (
1955 Some(BytesFrame::Array {
1956 data: vec![
1957 BytesFrame::SimpleString {
1958 data: "Foo".into(),
1959 attributes: None,
1960 },
1961 BytesFrame::SimpleString {
1962 data: "Bar".into(),
1963 attributes: None,
1964 },
1965 ],
1966 attributes: None,
1967 }),
1968 16,
1969 );
1970 let bytes: Bytes = "*2\r\n+Foo\r\n+Bar\r\n".into();
1971
1972 decode_and_verify_some(&bytes, &expected);
1973 decode_and_verify_padded_some(&bytes, &expected);
1974 }
1975
1976 #[test]
1977 fn should_decode_array_nulls() {
1978 let bytes: Bytes = "*3\r\n$3\r\nFoo\r\n_\r\n$3\r\nBar\r\n".into();
1979
1980 let expected = (
1981 Some(BytesFrame::Array {
1982 data: vec![
1983 BytesFrame::BlobString {
1984 data: "Foo".into(),
1985 attributes: None,
1986 },
1987 BytesFrame::Null,
1988 BytesFrame::BlobString {
1989 data: "Bar".into(),
1990 attributes: None,
1991 },
1992 ],
1993 attributes: None,
1994 }),
1995 bytes.len(),
1996 );
1997
1998 decode_and_verify_some(&bytes, &expected);
1999 decode_and_verify_padded_some(&bytes, &expected);
2000 }
2001
2002 #[test]
2003 fn should_decode_normal_error() {
2004 let bytes: Bytes = "-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".into();
2005 let expected = (
2006 Some(BytesFrame::SimpleError {
2007 data: "WRONGTYPE Operation against a key holding the wrong kind of value".into(),
2008 attributes: None,
2009 }),
2010 bytes.len(),
2011 );
2012
2013 decode_and_verify_some(&bytes, &expected);
2014 decode_and_verify_padded_some(&bytes, &expected);
2015 }
2016
2017 #[test]
2018 fn should_decode_moved_error() {
2019 let bytes: Bytes = "-MOVED 3999 127.0.0.1:6381\r\n".into();
2020 let expected = (
2021 Some(BytesFrame::SimpleError {
2022 data: "MOVED 3999 127.0.0.1:6381".into(),
2023 attributes: None,
2024 }),
2025 bytes.len(),
2026 );
2027
2028 decode_and_verify_some(&bytes, &expected);
2029 decode_and_verify_padded_some(&bytes, &expected);
2030 }
2031
2032 #[test]
2033 fn should_decode_ask_error() {
2034 let bytes: Bytes = "-ASK 3999 127.0.0.1:6381\r\n".into();
2035 let expected = (
2036 Some(BytesFrame::SimpleError {
2037 data: "ASK 3999 127.0.0.1:6381".into(),
2038 attributes: None,
2039 }),
2040 bytes.len(),
2041 );
2042
2043 decode_and_verify_some(&bytes, &expected);
2044 decode_and_verify_padded_some(&bytes, &expected);
2045 }
2046
2047 #[test]
2048 fn should_decode_incomplete() {
2049 let bytes: Bytes = "*3\r\n$3\r\nFoo\r\n_\r\n$3\r\nBar".into();
2050 decode_and_verify_none(&bytes);
2051 }
2052
2053 #[test]
2054 #[should_panic]
2055 fn should_error_on_junk() {
2056 complete::decode("foobarbazwibblewobble".as_bytes()).unwrap();
2057 }
2058
2059 #[test]
2062 fn should_decode_blob_error() {
2063 let expected = (
2064 Some(BytesFrame::BlobError {
2065 data: "foo".into(),
2066 attributes: None,
2067 }),
2068 9,
2069 );
2070 let bytes: Bytes = "!3\r\nfoo\r\n".into();
2071
2072 decode_and_verify_some(&bytes, &expected);
2073 decode_and_verify_padded_some(&bytes, &expected);
2074 }
2075
2076 #[test]
2077 #[should_panic]
2078 fn should_decode_blob_error_incomplete() {
2079 let expected = (
2080 Some(BytesFrame::BlobError {
2081 data: "foo".into(),
2082 attributes: None,
2083 }),
2084 9,
2085 );
2086 let bytes: Bytes = "!3\r\nfo".into();
2087
2088 decode_and_verify_some(&bytes, &expected);
2089 decode_and_verify_padded_some(&bytes, &expected);
2090 }
2091
2092 #[test]
2093 fn should_decode_simple_error() {
2094 let expected = (
2095 Some(BytesFrame::SimpleError {
2096 data: "string".into(),
2097 attributes: None,
2098 }),
2099 9,
2100 );
2101 let bytes: Bytes = "-string\r\n".into();
2102
2103 decode_and_verify_some(&bytes, &expected);
2104 decode_and_verify_padded_some(&bytes, &expected);
2105 }
2106
2107 #[test]
2108 #[should_panic]
2109 fn should_decode_simple_error_incomplete() {
2110 let expected = (
2111 Some(BytesFrame::SimpleError {
2112 data: "string".into(),
2113 attributes: None,
2114 }),
2115 9,
2116 );
2117 let bytes: Bytes = "-strin".into();
2118
2119 decode_and_verify_some(&bytes, &expected);
2120 decode_and_verify_padded_some(&bytes, &expected);
2121 }
2122
2123 #[test]
2124 fn should_decode_boolean_true() {
2125 let expected = (
2126 Some(BytesFrame::Boolean {
2127 data: true,
2128 attributes: None,
2129 }),
2130 4,
2131 );
2132 let bytes: Bytes = "#t\r\n".into();
2133
2134 decode_and_verify_some(&bytes, &expected);
2135 decode_and_verify_padded_some(&bytes, &expected);
2136 }
2137
2138 #[test]
2139 fn should_decode_boolean_false() {
2140 let expected = (
2141 Some(BytesFrame::Boolean {
2142 data: false,
2143 attributes: None,
2144 }),
2145 4,
2146 );
2147 let bytes: Bytes = "#f\r\n".into();
2148
2149 decode_and_verify_some(&bytes, &expected);
2150 decode_and_verify_padded_some(&bytes, &expected);
2151 }
2152
2153 #[test]
2154 fn should_decode_number() {
2155 let expected = (
2156 Some(BytesFrame::Number {
2157 data: 42,
2158 attributes: None,
2159 }),
2160 5,
2161 );
2162 let bytes: Bytes = ":42\r\n".into();
2163
2164 decode_and_verify_some(&bytes, &expected);
2165 decode_and_verify_padded_some(&bytes, &expected);
2166 }
2167
2168 #[test]
2169 fn should_decode_double_inf() {
2170 let expected = (
2171 Some(BytesFrame::Double {
2172 data: f64::INFINITY,
2173 attributes: None,
2174 }),
2175 6,
2176 );
2177 let bytes: Bytes = ",inf\r\n".into();
2178
2179 decode_and_verify_some(&bytes, &expected);
2180 decode_and_verify_padded_some(&bytes, &expected);
2181 }
2182
2183 #[test]
2184 fn should_decode_double_neg_inf() {
2185 let expected = (
2186 Some(BytesFrame::Double {
2187 data: f64::NEG_INFINITY,
2188 attributes: None,
2189 }),
2190 7,
2191 );
2192 let bytes: Bytes = ",-inf\r\n".into();
2193
2194 decode_and_verify_some(&bytes, &expected);
2195 decode_and_verify_padded_some(&bytes, &expected);
2196 }
2197
2198 #[test]
2199 #[should_panic]
2200 fn should_decode_double_nan() {
2201 let expected = (
2202 Some(BytesFrame::Double {
2203 data: f64::NAN,
2204 attributes: None,
2205 }),
2206 7,
2207 );
2208 let bytes: Bytes = ",foo\r\n".into();
2209
2210 decode_and_verify_some(&bytes, &expected);
2211 decode_and_verify_padded_some(&bytes, &expected);
2212 }
2213
2214 #[test]
2215 fn should_decode_double() {
2216 let expected = (
2217 Some(BytesFrame::Double {
2218 data: 4.59193,
2219 attributes: None,
2220 }),
2221 10,
2222 );
2223 let bytes: Bytes = ",4.59193\r\n".into();
2224
2225 decode_and_verify_some(&bytes, &expected);
2226 decode_and_verify_padded_some(&bytes, &expected);
2227
2228 let expected = (
2229 Some(BytesFrame::Double {
2230 data: 4_f64,
2231 attributes: None,
2232 }),
2233 4,
2234 );
2235 let bytes: Bytes = ",4\r\n".into();
2236
2237 decode_and_verify_some(&bytes, &expected);
2238 decode_and_verify_padded_some(&bytes, &expected);
2239 }
2240
2241 #[test]
2242 fn should_decode_bignumber() {
2243 let expected = (
2244 Some(BytesFrame::BigNumber {
2245 data: "3492890328409238509324850943850943825024385".into(),
2246 attributes: None,
2247 }),
2248 46,
2249 );
2250 let bytes: Bytes = "(3492890328409238509324850943850943825024385\r\n".into();
2251
2252 decode_and_verify_some(&bytes, &expected);
2253 decode_and_verify_padded_some(&bytes, &expected);
2254 }
2255
2256 #[test]
2257 fn should_decode_null() {
2258 let expected = (Some(BytesFrame::Null), 3);
2259 let bytes: Bytes = "_\r\n".into();
2260
2261 decode_and_verify_some(&bytes, &expected);
2262 decode_and_verify_padded_some(&bytes, &expected);
2263 }
2264
2265 #[test]
2266 fn should_decode_verbatim_string_mkd() {
2267 let expected = (
2268 Some(BytesFrame::VerbatimString {
2269 data: "Some string".into(),
2270 format: VerbatimStringFormat::Markdown,
2271 attributes: None,
2272 }),
2273 22,
2274 );
2275 let bytes: Bytes = "=15\r\nmkd:Some string\r\n".into();
2276
2277 decode_and_verify_some(&bytes, &expected);
2278 decode_and_verify_padded_some(&bytes, &expected);
2279 }
2280
2281 #[test]
2282 fn should_decode_verbatim_string_txt() {
2283 let expected = (
2284 Some(BytesFrame::VerbatimString {
2285 data: "Some string".into(),
2286 format: VerbatimStringFormat::Text,
2287 attributes: None,
2288 }),
2289 22,
2290 );
2291 let bytes: Bytes = "=15\r\ntxt:Some string\r\n".into();
2292
2293 decode_and_verify_some(&bytes, &expected);
2294 decode_and_verify_padded_some(&bytes, &expected);
2295 }
2296
2297 #[test]
2298 fn should_decode_map_no_nulls() {
2299 let k1 = BytesFrame::SimpleString {
2300 data: "first".into(),
2301 attributes: None,
2302 };
2303 let v1 = BytesFrame::Number {
2304 data: 1,
2305 attributes: None,
2306 };
2307 let k2 = BytesFrame::BlobString {
2308 data: "second".into(),
2309 attributes: None,
2310 };
2311 let v2 = BytesFrame::Double {
2312 data: 4.2,
2313 attributes: None,
2314 };
2315
2316 let mut expected_map = resp3_utils::new_map(0);
2317 expected_map.insert(k1, v1);
2318 expected_map.insert(k2, v2);
2319 let expected = (
2320 Some(BytesFrame::Map {
2321 data: expected_map,
2322 attributes: None,
2323 }),
2324 34,
2325 );
2326 let bytes: Bytes = "%2\r\n+first\r\n:1\r\n$6\r\nsecond\r\n,4.2\r\n".into();
2327
2328 decode_and_verify_some(&bytes, &expected);
2329 decode_and_verify_padded_some(&bytes, &expected);
2330 }
2331
2332 #[test]
2333 fn should_decode_map_with_nulls() {
2334 let k1 = BytesFrame::SimpleString {
2335 data: "first".into(),
2336 attributes: None,
2337 };
2338 let v1 = BytesFrame::Number {
2339 data: 1,
2340 attributes: None,
2341 };
2342 let k2 = BytesFrame::Number {
2343 data: 2,
2344 attributes: None,
2345 };
2346 let v2 = BytesFrame::Null;
2347 let k3 = BytesFrame::BlobString {
2348 data: "second".into(),
2349 attributes: None,
2350 };
2351 let v3 = BytesFrame::Double {
2352 data: 4.2,
2353 attributes: None,
2354 };
2355
2356 let mut expected_map = resp3_utils::new_map(0);
2357 expected_map.insert(k1, v1);
2358 expected_map.insert(k2, v2);
2359 expected_map.insert(k3, v3);
2360 let expected = (
2361 Some(BytesFrame::Map {
2362 data: expected_map,
2363 attributes: None,
2364 }),
2365 41,
2366 );
2367 let bytes: Bytes = "%3\r\n+first\r\n:1\r\n:2\r\n_\r\n$6\r\nsecond\r\n,4.2\r\n".into();
2368
2369 decode_and_verify_some(&bytes, &expected);
2370 decode_and_verify_padded_some(&bytes, &expected);
2371 }
2372
2373 #[test]
2374 fn should_decode_set_no_nulls() {
2375 let mut expected_set = resp3_utils::new_set(0);
2376 expected_set.insert(BytesFrame::Number {
2377 data: 1,
2378 attributes: None,
2379 });
2380 expected_set.insert(BytesFrame::SimpleString {
2381 data: "2".into(),
2382 attributes: None,
2383 });
2384 expected_set.insert(BytesFrame::BlobString {
2385 data: "foobar".into(),
2386 attributes: None,
2387 });
2388 expected_set.insert(BytesFrame::Double {
2389 data: 4.2,
2390 attributes: None,
2391 });
2392 let expected = (
2393 Some(BytesFrame::Set {
2394 data: expected_set,
2395 attributes: None,
2396 }),
2397 30,
2398 );
2399 let bytes: Bytes = "~4\r\n:1\r\n+2\r\n$6\r\nfoobar\r\n,4.2\r\n".into();
2400
2401 decode_and_verify_some(&bytes, &expected);
2402 decode_and_verify_padded_some(&bytes, &expected);
2403 }
2404
2405 #[test]
2406 fn should_decode_set_with_nulls() {
2407 let mut expected_set = resp3_utils::new_set(0);
2408 expected_set.insert(BytesFrame::Number {
2409 data: 1,
2410 attributes: None,
2411 });
2412 expected_set.insert(BytesFrame::SimpleString {
2413 data: "2".into(),
2414 attributes: None,
2415 });
2416 expected_set.insert(BytesFrame::Null);
2417 expected_set.insert(BytesFrame::Double {
2418 data: 4.2,
2419 attributes: None,
2420 });
2421 let expected = (
2422 Some(BytesFrame::Set {
2423 data: expected_set,
2424 attributes: None,
2425 }),
2426 21,
2427 );
2428 let bytes: Bytes = "~4\r\n:1\r\n+2\r\n_\r\n,4.2\r\n".into();
2429
2430 decode_and_verify_some(&bytes, &expected);
2431 decode_and_verify_padded_some(&bytes, &expected);
2432 }
2433
2434 #[test]
2435 fn should_decode_push_pubsub() {
2436 let expected = (
2437 Some(BytesFrame::Push {
2438 data: vec![
2439 BytesFrame::SimpleString {
2440 data: "pubsub".into(),
2441 attributes: None,
2442 },
2443 BytesFrame::SimpleString {
2444 data: "message".into(),
2445 attributes: None,
2446 },
2447 BytesFrame::SimpleString {
2448 data: "somechannel".into(),
2449 attributes: None,
2450 },
2451 BytesFrame::SimpleString {
2452 data: "this is the message".into(),
2453 attributes: None,
2454 },
2455 ],
2456 attributes: None,
2457 }),
2458 59,
2459 );
2460 let bytes: Bytes = ">4\r\n+pubsub\r\n+message\r\n+somechannel\r\n+this is the message\r\n".into();
2461
2462 decode_and_verify_some(&bytes, &expected);
2463 decode_and_verify_padded_some(&bytes, &expected);
2464
2465 let (frame, _) = decode(&bytes).unwrap().unwrap();
2466 assert!(frame.is_normal_pubsub_message());
2467 }
2468
2469 #[test]
2470 fn should_decode_push_pattern_pubsub() {
2471 let expected = (
2472 Some(BytesFrame::Push {
2473 data: vec![
2474 BytesFrame::SimpleString {
2475 data: "pubsub".into(),
2476 attributes: None,
2477 },
2478 BytesFrame::SimpleString {
2479 data: "pmessage".into(),
2480 attributes: None,
2481 },
2482 BytesFrame::SimpleString {
2483 data: "pattern".into(),
2484 attributes: None,
2485 },
2486 BytesFrame::SimpleString {
2487 data: "somechannel".into(),
2488 attributes: None,
2489 },
2490 BytesFrame::SimpleString {
2491 data: "this is the message".into(),
2492 attributes: None,
2493 },
2494 ],
2495 attributes: None,
2496 }),
2497 70,
2498 );
2499 let bytes: Bytes = ">5\r\n+pubsub\r\n+pmessage\r\n+pattern\r\n+somechannel\r\n+this is the message\r\n".into();
2500
2501 decode_and_verify_some(&bytes, &expected);
2502 decode_and_verify_padded_some(&bytes, &expected);
2503
2504 let (frame, _) = decode(&bytes).unwrap().unwrap();
2505 assert!(frame.is_pattern_pubsub_message());
2506 }
2507
2508 #[test]
2509 fn should_decode_keyevent_message() {
2510 let expected = (
2511 Some(BytesFrame::Push {
2512 data: vec![
2513 BytesFrame::SimpleString {
2514 data: "pubsub".into(),
2515 attributes: None,
2516 },
2517 BytesFrame::SimpleString {
2518 data: "pmessage".into(),
2519 attributes: None,
2520 },
2521 BytesFrame::SimpleString {
2522 data: "__key*".into(),
2523 attributes: None,
2524 },
2525 BytesFrame::SimpleString {
2526 data: "__keyevent@0__:set".into(),
2527 attributes: None,
2528 },
2529 BytesFrame::SimpleString {
2530 data: "foo".into(),
2531 attributes: None,
2532 },
2533 ],
2534 attributes: None,
2535 }),
2536 60,
2537 );
2538 let bytes: Bytes = ">5\r\n+pubsub\r\n+pmessage\r\n+__key*\r\n+__keyevent@0__:set\r\n+foo\r\n".into();
2539
2540 decode_and_verify_some(&bytes, &expected);
2541 decode_and_verify_padded_some(&bytes, &expected);
2542
2543 let (frame, _) = decode(&bytes).unwrap().unwrap();
2544 assert!(frame.is_pattern_pubsub_message());
2545 }
2546
2547 #[test]
2548 fn should_parse_outer_attributes() {
2549 let mut expected_inner_attrs = resp3_utils::new_map(0);
2550 expected_inner_attrs.insert(
2551 BytesFrame::BlobString {
2552 data: "a".into(),
2553 attributes: None,
2554 },
2555 BytesFrame::Double {
2556 data: 0.1923,
2557 attributes: None,
2558 },
2559 );
2560 expected_inner_attrs.insert(
2561 BytesFrame::BlobString {
2562 data: "b".into(),
2563 attributes: None,
2564 },
2565 BytesFrame::Double {
2566 data: 0.0012,
2567 attributes: None,
2568 },
2569 );
2570 let expected_inner_attrs = BytesFrame::Map {
2571 data: expected_inner_attrs,
2572 attributes: None,
2573 };
2574
2575 let mut expected_attrs = resp3_utils::new_map(0);
2576 expected_attrs.insert(
2577 BytesFrame::SimpleString {
2578 data: "key-popularity".into(),
2579 attributes: None,
2580 },
2581 expected_inner_attrs,
2582 );
2583
2584 let expected = (
2585 Some(BytesFrame::Array {
2586 data: vec![
2587 BytesFrame::Number {
2588 data: 2039123,
2589 attributes: None,
2590 },
2591 BytesFrame::Number {
2592 data: 9543892,
2593 attributes: None,
2594 },
2595 ],
2596 attributes: Some(expected_attrs),
2597 }),
2598 81,
2599 );
2600
2601 let bytes: Bytes =
2602 "|1\r\n+key-popularity\r\n%2\r\n$1\r\na\r\n,0.1923\r\n$1\r\nb\r\n,0.0012\r\n*2\r\n:2039123\r\n:9543892\r\n"
2603 .into();
2604
2605 decode_and_verify_some(&bytes, &expected);
2606 decode_and_verify_padded_some(&bytes, &expected);
2607 }
2608
2609 #[test]
2610 fn should_parse_inner_attributes() {
2611 let mut expected_attrs = resp3_utils::new_map(0);
2612 expected_attrs.insert(
2613 BytesFrame::SimpleString {
2614 data: "ttl".into(),
2615 attributes: None,
2616 },
2617 BytesFrame::Number {
2618 data: 3600,
2619 attributes: None,
2620 },
2621 );
2622
2623 let expected = (
2624 Some(BytesFrame::Array {
2625 data: vec![
2626 BytesFrame::Number {
2627 data: 1,
2628 attributes: None,
2629 },
2630 BytesFrame::Number {
2631 data: 2,
2632 attributes: None,
2633 },
2634 BytesFrame::Number {
2635 data: 3,
2636 attributes: Some(expected_attrs),
2637 },
2638 ],
2639 attributes: None,
2640 }),
2641 33,
2642 );
2643 let bytes: Bytes = "*3\r\n:1\r\n:2\r\n|1\r\n+ttl\r\n:3600\r\n:3\r\n".into();
2644
2645 decode_and_verify_some(&bytes, &expected);
2646 decode_and_verify_padded_some(&bytes, &expected);
2647 }
2648
2649 #[test]
2650 fn should_decode_end_stream() {
2651 let bytes: Bytes = ";0\r\n".into();
2652 let (frame, _) = stream_decode(&bytes).unwrap().unwrap();
2653 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::new_end_stream()))
2654 }
2655
2656 #[test]
2657 fn should_decode_streaming_string() {
2658 let mut bytes: Bytes = "$?\r\n;4\r\nHell\r\n;6\r\no worl\r\n;1\r\nd\r\n;0\r\n".into();
2659
2660 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2661 assert_eq!(
2662 frame,
2663 DecodedFrame::Streaming(StreamedFrame::new(FrameKind::BlobString))
2664 );
2665 assert_eq!(amt, 4);
2666 let _ = bytes.split_to(amt);
2667
2668 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2669 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::ChunkedString("Hell".into())));
2670 assert_eq!(amt, 10);
2671 let _ = bytes.split_to(amt);
2672
2673 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2674 assert_eq!(
2675 frame,
2676 DecodedFrame::Complete(BytesFrame::ChunkedString("o worl".into()))
2677 );
2678 assert_eq!(amt, 12);
2679 let _ = bytes.split_to(amt);
2680
2681 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2682 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::ChunkedString("d".into())));
2683 assert_eq!(amt, 7);
2684 let _ = bytes.split_to(amt);
2685
2686 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2687 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::new_end_stream()));
2688 assert_eq!(amt, 4);
2689 }
2690
2691 #[test]
2692 fn should_decode_streaming_array() {
2693 let mut bytes: Bytes = "*?\r\n:1\r\n:2\r\n:3\r\n.\r\n".into();
2694
2695 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2696 assert_eq!(frame, DecodedFrame::Streaming(StreamedFrame::new(FrameKind::Array)));
2697 assert_eq!(amt, 4);
2698 let _ = bytes.split_to(amt);
2699 let mut streamed = frame.into_streaming_frame().unwrap();
2700
2701 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2702 assert_eq!(
2703 frame,
2704 DecodedFrame::Complete(BytesFrame::Number {
2705 data: 1,
2706 attributes: None,
2707 })
2708 );
2709 assert_eq!(amt, 4);
2710 let _ = bytes.split_to(amt);
2711 streamed.add_frame(frame.into_complete_frame().unwrap());
2712
2713 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2714 assert_eq!(
2715 frame,
2716 DecodedFrame::Complete(BytesFrame::Number {
2717 data: 2,
2718 attributes: None,
2719 })
2720 );
2721 assert_eq!(amt, 4);
2722 let _ = bytes.split_to(amt);
2723 streamed.add_frame(frame.into_complete_frame().unwrap());
2724
2725 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2726 assert_eq!(
2727 frame,
2728 DecodedFrame::Complete(BytesFrame::Number {
2729 data: 3,
2730 attributes: None,
2731 })
2732 );
2733 assert_eq!(amt, 4);
2734 let _ = bytes.split_to(amt);
2735 streamed.add_frame(frame.into_complete_frame().unwrap());
2736
2737 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2738 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::new_end_stream()));
2739 assert_eq!(amt, 3);
2740 streamed.add_frame(frame.into_complete_frame().unwrap());
2741
2742 assert!(streamed.is_finished());
2743 let actual = streamed.take().unwrap();
2744 let expected = BytesFrame::Array {
2745 data: vec![
2746 BytesFrame::Number {
2747 data: 1,
2748 attributes: None,
2749 },
2750 BytesFrame::Number {
2751 data: 2,
2752 attributes: None,
2753 },
2754 BytesFrame::Number {
2755 data: 3,
2756 attributes: None,
2757 },
2758 ],
2759 attributes: None,
2760 };
2761
2762 assert_eq!(actual, expected);
2763 }
2764
2765 #[test]
2766 fn should_decode_streaming_set() {
2767 let mut bytes: Bytes = "~?\r\n:1\r\n:2\r\n:3\r\n.\r\n".into();
2768
2769 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2770 assert_eq!(frame, DecodedFrame::Streaming(StreamedFrame::new(FrameKind::Set)));
2771 assert_eq!(amt, 4);
2772 let _ = bytes.split_to(amt);
2773 let mut streamed = frame.into_streaming_frame().unwrap();
2774
2775 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2776 assert_eq!(
2777 frame,
2778 DecodedFrame::Complete(BytesFrame::Number {
2779 data: 1,
2780 attributes: None,
2781 })
2782 );
2783 assert_eq!(amt, 4);
2784 let _ = bytes.split_to(amt);
2785 streamed.add_frame(frame.into_complete_frame().unwrap());
2786
2787 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2788 assert_eq!(
2789 frame,
2790 DecodedFrame::Complete(BytesFrame::Number {
2791 data: 2,
2792 attributes: None,
2793 })
2794 );
2795 assert_eq!(amt, 4);
2796 let _ = bytes.split_to(amt);
2797 streamed.add_frame(frame.into_complete_frame().unwrap());
2798
2799 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2800 assert_eq!(
2801 frame,
2802 DecodedFrame::Complete(BytesFrame::Number {
2803 data: 3,
2804 attributes: None,
2805 })
2806 );
2807 assert_eq!(amt, 4);
2808 let _ = bytes.split_to(amt);
2809 streamed.add_frame(frame.into_complete_frame().unwrap());
2810
2811 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2812 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::new_end_stream()));
2813 assert_eq!(amt, 3);
2814 streamed.add_frame(frame.into_complete_frame().unwrap());
2815
2816 assert!(streamed.is_finished());
2817 let actual = streamed.take().unwrap();
2818 let mut expected_result = resp3_utils::new_set(0);
2819 expected_result.insert(BytesFrame::Number {
2820 data: 1,
2821 attributes: None,
2822 });
2823 expected_result.insert(BytesFrame::Number {
2824 data: 2,
2825 attributes: None,
2826 });
2827 expected_result.insert(BytesFrame::Number {
2828 data: 3,
2829 attributes: None,
2830 });
2831
2832 let expected = BytesFrame::Set {
2833 data: expected_result,
2834 attributes: None,
2835 };
2836
2837 assert_eq!(actual, expected);
2838 }
2839
2840 #[test]
2841 fn should_decode_streaming_map() {
2842 let mut bytes: Bytes = "%?\r\n+a\r\n:1\r\n+b\r\n:2\r\n.\r\n".into();
2843
2844 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2845 assert_eq!(frame, DecodedFrame::Streaming(StreamedFrame::new(FrameKind::Map)));
2846 assert_eq!(amt, 4);
2847 let _ = bytes.split_to(amt);
2848 let mut streamed = frame.into_streaming_frame().unwrap();
2849
2850 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2851 assert_eq!(
2852 frame,
2853 DecodedFrame::Complete(BytesFrame::SimpleString {
2854 data: "a".into(),
2855 attributes: None,
2856 })
2857 );
2858 assert_eq!(amt, 4);
2859 let _ = bytes.split_to(amt);
2860 streamed.add_frame(frame.into_complete_frame().unwrap());
2861
2862 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2863 assert_eq!(
2864 frame,
2865 DecodedFrame::Complete(BytesFrame::Number {
2866 data: 1.into(),
2867 attributes: None,
2868 })
2869 );
2870 assert_eq!(amt, 4);
2871 let _ = bytes.split_to(amt);
2872 streamed.add_frame(frame.into_complete_frame().unwrap());
2873
2874 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2875 assert_eq!(
2876 frame,
2877 DecodedFrame::Complete(BytesFrame::SimpleString {
2878 data: "b".into(),
2879 attributes: None,
2880 })
2881 );
2882 assert_eq!(amt, 4);
2883 let _ = bytes.split_to(amt);
2884 streamed.add_frame(frame.into_complete_frame().unwrap());
2885
2886 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2887 assert_eq!(
2888 frame,
2889 DecodedFrame::Complete(BytesFrame::Number {
2890 data: 2.into(),
2891 attributes: None,
2892 })
2893 );
2894 assert_eq!(amt, 4);
2895 let _ = bytes.split_to(amt);
2896 streamed.add_frame(frame.into_complete_frame().unwrap());
2897
2898 let (frame, amt) = stream_decode(&bytes).unwrap().unwrap();
2899 assert_eq!(frame, DecodedFrame::Complete(BytesFrame::new_end_stream()));
2900 assert_eq!(amt, 3);
2901 streamed.add_frame(frame.into_complete_frame().unwrap());
2902
2903 assert!(streamed.is_finished());
2904 let actual = streamed.take().unwrap();
2905 let mut expected_result = resp3_utils::new_map(0);
2906 expected_result.insert(
2907 BytesFrame::SimpleString {
2908 data: "a".into(),
2909 attributes: None,
2910 },
2911 BytesFrame::Number {
2912 data: 1,
2913 attributes: None,
2914 },
2915 );
2916 expected_result.insert(
2917 BytesFrame::SimpleString {
2918 data: "b".into(),
2919 attributes: None,
2920 },
2921 BytesFrame::Number {
2922 data: 2,
2923 attributes: None,
2924 },
2925 );
2926 let expected = BytesFrame::Map {
2927 data: expected_result,
2928 attributes: None,
2929 };
2930
2931 assert_eq!(actual, expected);
2932 }
2933
2934 #[test]
2935 fn should_decode_hello_no_auth() {
2936 let expected = (
2937 Some(BytesFrame::Hello {
2938 version: RespVersion::RESP3,
2939 auth: None,
2940 setname: None,
2941 }),
2942 9,
2943 );
2944 let bytes = "HELLO 3\r\n".into();
2945
2946 decode_and_verify_some(&bytes, &expected);
2947 decode_and_verify_padded_some(&bytes, &expected);
2948 }
2949
2950 #[test]
2951 fn should_decode_hello_with_auth_no_setname() {
2952 let expected = (
2953 Some(BytesFrame::Hello {
2954 version: RespVersion::RESP3,
2955 auth: Some(("foo".into(), "bar".into())),
2956 setname: None,
2957 }),
2958 22,
2959 );
2960 let bytes = "HELLO 3 AUTH foo bar\r\n".into();
2961
2962 decode_and_verify_some(&bytes, &expected);
2963 decode_and_verify_padded_some(&bytes, &expected);
2964 }
2965
2966 #[test]
2967 fn should_decode_hello_with_setname() {
2968 let expected = (
2969 Some(BytesFrame::Hello {
2970 version: RespVersion::RESP3,
2971 auth: None,
2972 setname: Some("baz".into()),
2973 }),
2974 21,
2975 );
2976 let bytes = "HELLO 3 SETNAME baz\r\n".into();
2977
2978 decode_and_verify_some(&bytes, &expected);
2979 decode_and_verify_padded_some(&bytes, &expected);
2980 }
2981
2982 #[test]
2983 fn should_decode_hello_with_auth_and_setname() {
2984 let expected = (
2985 Some(BytesFrame::Hello {
2986 version: RespVersion::RESP3,
2987 auth: Some(("foo".into(), "bar".into())),
2988 setname: Some("baz".into()),
2989 }),
2990 34,
2991 );
2992 let bytes = "HELLO 3 AUTH foo bar SETNAME baz\r\n".into();
2993
2994 decode_and_verify_some(&bytes, &expected);
2995 decode_and_verify_padded_some(&bytes, &expected);
2996 }
2997}