Serialize this value to RESP2 wire format.
(&self, buf: &mut Vec<u8>)
| 70 | |
| 71 | /// Serialize this value to RESP2 wire format. |
| 72 | pub fn serialize(&self, buf: &mut Vec<u8>) { |
| 73 | match self { |
| 74 | Self::SimpleString(s) => { |
| 75 | buf.push(b'+'); |
| 76 | buf.extend_from_slice(s.as_bytes()); |
| 77 | buf.extend_from_slice(b"\r\n"); |
| 78 | } |
| 79 | Self::Error(s) => { |
| 80 | buf.push(b'-'); |
| 81 | buf.extend_from_slice(s.as_bytes()); |
| 82 | buf.extend_from_slice(b"\r\n"); |
| 83 | } |
| 84 | Self::Integer(n) => { |
| 85 | buf.push(b':'); |
| 86 | buf.extend_from_slice(n.to_string().as_bytes()); |
| 87 | buf.extend_from_slice(b"\r\n"); |
| 88 | } |
| 89 | Self::BulkString(None) => { |
| 90 | buf.extend_from_slice(b"$-1\r\n"); |
| 91 | } |
| 92 | Self::BulkString(Some(data)) => { |
| 93 | buf.push(b'$'); |
| 94 | buf.extend_from_slice(data.len().to_string().as_bytes()); |
| 95 | buf.extend_from_slice(b"\r\n"); |
| 96 | buf.extend_from_slice(data); |
| 97 | buf.extend_from_slice(b"\r\n"); |
| 98 | } |
| 99 | Self::Array(None) => { |
| 100 | buf.extend_from_slice(b"*-1\r\n"); |
| 101 | } |
| 102 | Self::Array(Some(items)) => { |
| 103 | buf.push(b'*'); |
| 104 | buf.extend_from_slice(items.len().to_string().as_bytes()); |
| 105 | buf.extend_from_slice(b"\r\n"); |
| 106 | for item in items { |
| 107 | item.serialize(buf); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// Serialize to a new Vec. |
| 114 | pub fn to_bytes(&self) -> Vec<u8> { |