Parse a RESP value (expected to be an array of bulk strings) into a command. Returns `None` if the value is not a valid command (empty array, non-array, etc.).
(value: &RespValue)
| 18 | /// |
| 19 | /// Returns `None` if the value is not a valid command (empty array, non-array, etc.). |
| 20 | pub fn parse(value: &RespValue) -> Option<Self> { |
| 21 | let items = match value { |
| 22 | RespValue::Array(Some(items)) if !items.is_empty() => items, |
| 23 | _ => return None, |
| 24 | }; |
| 25 | |
| 26 | let name = match &items[0] { |
| 27 | RespValue::BulkString(Some(data)) => String::from_utf8_lossy(data).to_uppercase(), |
| 28 | _ => return None, |
| 29 | }; |
| 30 | |
| 31 | let args: Vec<Vec<u8>> = items[1..] |
| 32 | .iter() |
| 33 | .filter_map(|item| match item { |
| 34 | RespValue::BulkString(Some(data)) => Some(data.clone()), |
| 35 | _ => None, |
| 36 | }) |
| 37 | .collect(); |
| 38 | |
| 39 | Some(Self { name, args }) |
| 40 | } |
| 41 | |
| 42 | /// Get argument at index as bytes. Returns None if out of bounds. |
| 43 | pub fn arg(&self, index: usize) -> Option<&[u8]> { |