RESP mini-server (Redis) — Rust
Topics: wire-protocol parsing, RESP, in-memory store
Problem
Implement the core of a Redis-style server: parse one client command in the RESP wire format,
run it against an in-memory store, and return the RESP reply. (Connection handling and framing are
out of scope — see the line-protocol and message-framing challenges.)
pub struct Server { /* … */ }
impl Server {
pub fn new() -> Self;
pub fn handle(&mut self, request: &[u8]) -> Vec<u8>;
}
RESP shapes:
array *<count>\r\n <element>...
bulk string $<len>\r\n<bytes>\r\n (null bulk = $-1\r\n)
simple +OK\r\n integer :<n>\r\n error -ERR <msg>\r\n
Clients send a command as an array of bulk strings, e.g. *2\r\n$3\r\nGET\r\n$3\r\nfoo\r\n.
Commands (case-insensitive):
PING → +PONG\r\n; PING <msg> → bulk reply of <msg>.
SET key val → +OK\r\n.
GET key → bulk reply, or null bulk $-1\r\n if missing.
INCR key → integer reply; error if the stored value isn't an integer.
DEL key... → integer count of keys removed.
- Unknown command or malformed input →
-ERR ...\r\n.
The store, new, and the bulk/err encoders are provided; you implement handle — parse the RESP
array (walk the buffer, reading each $<len> bulk string), then dispatch.
Grading
Your solution.rs is compiled together with a trusted tests.rs (which include!s it) using
rustc --test. Only solution.rs is yours to edit.
Sign in to submit your solution.