|
18 | 18 | //! This module provides methods to deserialize an incoming response packet |
19 | 19 |
|
20 | 20 | use crate::terrapipe::RespCode; |
| 21 | +use std::hint::unreachable_unchecked; |
21 | 22 |
|
22 | 23 | #[derive(Debug, PartialEq)] |
23 | 24 | /// A response datagroup |
@@ -232,22 +233,31 @@ pub fn parse(buf: &[u8]) -> ClientResult { |
232 | 233 | } |
233 | 234 | } |
234 | 235 | if buf.get(pos).is_none() { |
235 | | - // Either more data was sent or some data was missing |
236 | 236 | if items.len() == action_size { |
237 | 237 | if items.len() == 1 { |
238 | 238 | 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 | + ) |
240 | 247 | } else { |
| 248 | + // More than one time returned, so we can return this as ClientResult::Response |
241 | 249 | ClientResult::Response(items, pos) |
242 | 250 | } |
243 | 251 | } else { |
244 | | - // The CLI does not support batch queries |
245 | | - unimplemented!(); |
| 252 | + todo!("Pipelined queries aren't implemented yet!") |
246 | 253 | } |
247 | 254 | } else { |
| 255 | + // Since the number of items we got is not equal to the action size - not all data was |
| 256 | + // transferred |
248 | 257 | ClientResult::Incomplete |
249 | 258 | } |
250 | 259 | } else { |
| 260 | + // Either more data was sent or some data was missing |
251 | 261 | ClientResult::InvalidResponse |
252 | 262 | } |
253 | 263 | } |
@@ -302,4 +312,20 @@ fn test_parser() { |
302 | 312 | parse(&res), |
303 | 313 | ClientResult::ResponseItem(DataType::RespCode(RespCode::Okay), res.len()) |
304 | 314 | ); |
| 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 | + ); |
305 | 331 | } |
0 commit comments