(buf: &[u8])
| 286 | } |
| 287 | |
| 288 | fn parse_array(buf: &[u8]) -> io::Result<Option<(RespValue, usize)>> { |
| 289 | let crlf_pos = match find_crlf(buf) { |
| 290 | Some(p) => p, |
| 291 | None => return Ok(None), |
| 292 | }; |
| 293 | |
| 294 | let count_str = std::str::from_utf8(&buf[..crlf_pos]) |
| 295 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 296 | let count: i64 = count_str |
| 297 | .parse() |
| 298 | .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; |
| 299 | |
| 300 | if count < 0 { |
| 301 | return Ok(Some((RespValue::nil_array(), crlf_pos + 2))); |
| 302 | } |
| 303 | |
| 304 | let count = count as usize; |
| 305 | let mut offset = crlf_pos + 2; |
| 306 | let mut items = Vec::with_capacity(count); |
| 307 | |
| 308 | for _ in 0..count { |
| 309 | match parse_value(&buf[offset..])? { |
| 310 | Some((value, consumed)) => { |
| 311 | items.push(value); |
| 312 | offset += consumed; |
| 313 | } |
| 314 | None => return Ok(None), // Need more data. |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | Ok(Some((RespValue::Array(Some(items)), offset))) |
| 319 | } |
| 320 | |
| 321 | /// Parse an inline command (plain text) into a RESP array of bulk strings. |
| 322 | fn parse_inline_command(line: &[u8]) -> RespValue { |
no test coverage detected