(&mut self, msg: RedisServerMsg<'_>, buf: &mut BytesMut)
| 248 | type Error = io::Error; |
| 249 | |
| 250 | fn encode(&mut self, msg: RedisServerMsg<'_>, buf: &mut BytesMut) -> io::Result<()> { |
| 251 | // error!("wframe - cap: {}: len: {}", buf.capacity(), buf.len()); |
| 252 | match msg { |
| 253 | RedisServerMsg::Ok => { |
| 254 | buf.put(&b"+OK\r\n"[..]); |
| 255 | } |
| 256 | RedisServerMsg::Pong => { |
| 257 | buf.put(&b"+PONG\r\n"[..]); |
| 258 | } |
| 259 | RedisServerMsg::Null => { |
| 260 | buf.put(&b"$-1\r\n"[..]); |
| 261 | } |
| 262 | RedisServerMsg::Info { used_memory } => { |
| 263 | // Build the raw buffer. |
| 264 | let r = format!("# Memory\r\nused_memory:{}\r\n", used_memory); |
| 265 | let b = r.as_bytes(); |
| 266 | let bs_hdr = format!("${}\r\n", b.len()); |
| 267 | buf.put_slice(bs_hdr.as_bytes()); |
| 268 | buf.put_slice(b); |
| 269 | buf.put(&b"\r\n"[..]); |
| 270 | |
| 271 | debug!("buf {:?}", String::from_utf8(buf.to_vec())); |
| 272 | } |
| 273 | RedisServerMsg::KvPair { k, v } => { |
| 274 | // Identify our response has 2 values. |
| 275 | buf.put(&b"*2\r\n"[..]); |
| 276 | // How long is the key? |
| 277 | let kb = k.as_bytes(); |
| 278 | let kbs_hdr = format!("${}\r\n", k.len()); |
| 279 | buf.put_slice(kbs_hdr.as_bytes()); |
| 280 | buf.put_slice(kb); |
| 281 | buf.put(&b"\r\n"[..]); |
| 282 | // Add the value |
| 283 | let vb = v.as_bytes(); |
| 284 | let vbs_hdr = format!("${}\r\n", v.len()); |
| 285 | buf.put_slice(vbs_hdr.as_bytes()); |
| 286 | buf.put_slice(vb); |
| 287 | buf.put(&b"\r\n"[..]); |
| 288 | } |
| 289 | // We split this up into "parts". |
| 290 | RedisServerMsg::DataHdr { sz } => { |
| 291 | let kbs_hdr = format!("${}\r\n", sz); |
| 292 | buf.put_slice(kbs_hdr.as_bytes()); |
| 293 | } |
| 294 | RedisServerMsg::DataChunk { slice } => { |
| 295 | // extend from slice will auto-resize. |
| 296 | buf.extend_from_slice(slice) |
| 297 | } |
| 298 | RedisServerMsg::DataEof => { |
| 299 | buf.put(&b"\r\n"[..]); |
| 300 | } |
| 301 | RedisServerMsg::Error(err) => { |
| 302 | buf.put(&b"-SERVER_ERROR "[..]); |
| 303 | buf.put_slice(err.as_bytes()); |
| 304 | buf.put(&b"\r\n"[..]); |
| 305 | } |
| 306 | } |
| 307 |
nothing calls this directly
no outgoing calls
no test coverage detected