Module parser

Module parser 

Source
Expand description

Zero-Copy RESP Protocol Parser

This module implements a high-performance, zero-copy parser for the RESP protocol. Zero-copy means we avoid copying data wherever possible, instead using references and Bytes which can be cheaply cloned (it’s just incrementing a reference count).

§Design Philosophy

  1. Zero-Copy: We use bytes::Bytes to avoid memory allocations during parsing.
  2. Incremental: The parser can handle partial data and resume when more arrives.
  3. Error Recovery: Clear error messages for debugging protocol issues.

§How the Parser Works

The parser reads from a buffer and returns either:

  • Ok(Some((value, consumed))) - Successfully parsed a value, consumed bytes were used
  • Ok(None) - Need more data, the message is incomplete
  • Err(ParseError) - Invalid protocol data

This design allows the caller to:

  1. Append incoming network data to a buffer
  2. Call parse() to attempt parsing
  3. If successful, advance the buffer by consumed bytes
  4. If incomplete, wait for more data
  5. If error, handle or disconnect the client

Structs§

RespParser
A zero-copy RESP protocol parser.

Enums§

ParseError
Errors that can occur during RESP parsing.

Constants§

MAX_BULK_SIZE
Maximum size for a single bulk string (512 MB, same as Redis)
MAX_NESTING_DEPTH
Maximum array nesting depth (prevent stack overflow)

Functions§

parse_message
Helper function to parse a single RESP message from bytes.

Type Aliases§

ParseResult
Result type for parsing operations.