Parse a RESP value starting at `buf[0]`. Returns `(value, bytes_consumed)`.
(buf: &[u8])
| 210 | |
| 211 | /// Parse a RESP value starting at `buf[0]`. Returns `(value, bytes_consumed)`. |
| 212 | fn parse_value(buf: &[u8]) -> io::Result<Option<(RespValue, usize)>> { |
| 213 | if buf.is_empty() { |
| 214 | return Ok(None); |
| 215 | } |
| 216 | |
| 217 | match buf[0] { |
| 218 | b'+' => parse_simple_string(&buf[1..]) |
| 219 | .map(|opt| opt.map(|(s, n)| (RespValue::SimpleString(s), n + 1))), |
| 220 | b'-' => { |
| 221 | parse_simple_string(&buf[1..]).map(|opt| opt.map(|(s, n)| (RespValue::Error(s), n + 1))) |
| 222 | } |
| 223 | b':' => { |
| 224 | parse_integer(&buf[1..]).map(|opt| opt.map(|(i, n)| (RespValue::Integer(i), n + 1))) |
| 225 | } |
| 226 | b'$' => parse_bulk_string(&buf[1..]).map(|opt| opt.map(|(v, n)| (v, n + 1))), |
| 227 | b'*' => parse_array(&buf[1..]).map(|opt| opt.map(|(v, n)| (v, n + 1))), |
| 228 | other => Err(io::Error::new( |
| 229 | io::ErrorKind::InvalidData, |
| 230 | format!("unexpected RESP type byte: 0x{other:02x}"), |
| 231 | )), |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | fn parse_simple_string(buf: &[u8]) -> io::Result<Option<(String, usize)>> { |
| 236 | match find_crlf(buf) { |
no test coverage detected