Skip to content

Commit 00f428a

Browse files
committed
Add tests for longer responses
Also optimized returning of `ResponseItem`s
1 parent 9403a55 commit 00f428a

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/deserializer.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! This module provides methods to deserialize an incoming response packet
1919
2020
use crate::terrapipe::RespCode;
21+
use std::hint::unreachable_unchecked;
2122

2223
#[derive(Debug, PartialEq)]
2324
/// A response datagroup
@@ -232,22 +233,31 @@ pub fn parse(buf: &[u8]) -> ClientResult {
232233
}
233234
}
234235
if buf.get(pos).is_none() {
235-
// Either more data was sent or some data was missing
236236
if items.len() == action_size {
237237
if items.len() == 1 {
238238
if items[0].size() == 1 {
239-
ClientResult::ResponseItem(items.pop().unwrap().__unpack_0(), pos)
239+
// Single item returned, so we can return this as ClientResult::ResponseItem
240+
ClientResult::ResponseItem(
241+
items
242+
.pop()
243+
.unwrap_or_else(|| unsafe { unreachable_unchecked() })
244+
.__unpack_0(),
245+
pos,
246+
)
240247
} else {
248+
// More than one time returned, so we can return this as ClientResult::Response
241249
ClientResult::Response(items, pos)
242250
}
243251
} else {
244-
// The CLI does not support batch queries
245-
unimplemented!();
252+
todo!("Pipelined queries aren't implemented yet!")
246253
}
247254
} else {
255+
// Since the number of items we got is not equal to the action size - not all data was
256+
// transferred
248257
ClientResult::Incomplete
249258
}
250259
} else {
260+
// Either more data was sent or some data was missing
251261
ClientResult::InvalidResponse
252262
}
253263
}
@@ -302,4 +312,20 @@ fn test_parser() {
302312
parse(&res),
303313
ClientResult::ResponseItem(DataType::RespCode(RespCode::Okay), res.len())
304314
);
315+
let res = "#2\n*1\n#2\n&5\n!1\n1\n!1\n0\n+5\nsayan\n+2\nis\n+4\nbusy\n"
316+
.as_bytes()
317+
.to_owned();
318+
assert_eq!(
319+
parse(&res),
320+
ClientResult::Response(
321+
vec![DataGroup(vec![
322+
DataType::RespCode(RespCode::NotFound),
323+
DataType::RespCode(RespCode::Okay),
324+
DataType::Str("sayan".to_owned()),
325+
DataType::Str("is".to_owned()),
326+
DataType::Str("busy".to_owned())
327+
])],
328+
res.len()
329+
)
330+
);
305331
}

0 commit comments

Comments
 (0)