pub enum Value {
SimpleString(Bytes),
Error(Bytes),
Integer(i64),
BulkString(Bytes),
Null,
Array(Vec<Value>),
}Expand description
A RESP protocol value.
This enum supports both RESP2 and RESP3 types. RESP3 types are only
available when the resp3 feature is enabled.
Variants§
SimpleString(Bytes)
Simple string: +OK\r\n
Error(Bytes)
Error: -ERR message\r\n
Integer(i64)
Integer: :1000\r\n
BulkString(Bytes)
Bulk string: $6\r\nfoobar\r\n
Null
Null value.
RESP2: $-1\r\n or *-1\r\n
RESP3: _\r\n
Array(Vec<Value>)
Array: *2\r\n...
Implementations§
Source§impl Value
impl Value
Sourcepub fn simple_string(s: &[u8]) -> Self
pub fn simple_string(s: &[u8]) -> Self
Create a simple string value.
Sourcepub fn bulk_string(data: &[u8]) -> Self
pub fn bulk_string(data: &[u8]) -> Self
Create a bulk string value.
Sourcepub fn is_simple_string(&self) -> bool
pub fn is_simple_string(&self) -> bool
Returns true if this is a simple string.
Sourcepub fn is_bulk_string(&self) -> bool
pub fn is_bulk_string(&self) -> bool
Returns true if this is a bulk string.
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if this is an integer.
Sourcepub fn as_bytes(&self) -> Option<&[u8]>
pub fn as_bytes(&self) -> Option<&[u8]>
Returns the value as bytes if it’s a string type (simple or bulk).
Sourcepub fn as_integer(&self) -> Option<i64>
pub fn as_integer(&self) -> Option<i64>
Returns the value as an integer.
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), ParseError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), ParseError>
Parse a RESP value from a byte buffer.
Returns the parsed value and the number of bytes consumed.
§Errors
Returns ParseError::Incomplete if more data is needed to complete parsing.
Returns other errors for malformed data.
Sourcepub fn parse_with_options(
data: &[u8],
options: &ParseOptions,
) -> Result<(Self, usize), ParseError>
pub fn parse_with_options( data: &[u8], options: &ParseOptions, ) -> Result<(Self, usize), ParseError>
Parse a RESP value from raw bytes with custom options.
This allows configuring DoS protection limits like maximum collection size and bulk string length.
Sourcepub fn parse_bytes(data: Bytes) -> Result<(Self, usize), ParseError>
pub fn parse_bytes(data: Bytes) -> Result<(Self, usize), ParseError>
Parse a RESP value zero-copy from a Bytes buffer.
String variants (BulkString, SimpleString, etc.) are returned as
Bytes::slice() references into the input — no allocation or copy.
Returns the parsed value and the number of bytes consumed.
Sourcepub fn parse_bytes_with_options(
data: Bytes,
options: &ParseOptions,
) -> Result<(Self, usize), ParseError>
pub fn parse_bytes_with_options( data: Bytes, options: &ParseOptions, ) -> Result<(Self, usize), ParseError>
Parse a RESP value zero-copy from a Bytes buffer with custom options.
Sourcepub fn encode(&self, buf: &mut [u8]) -> usize
pub fn encode(&self, buf: &mut [u8]) -> usize
Encode this value into a byte buffer.
Returns the number of bytes written.
§Panics
Panics if the buffer is too small. Use encoded_len() to check the required size.
Sourcepub fn encoded_len(&self) -> usize
pub fn encoded_len(&self) -> usize
Calculate the encoded length of this value.
Source§impl Value
impl Value
Sourcepub const EMPTY_ARRAY: &'static [u8] = b"*0\r\n"
pub const EMPTY_ARRAY: &'static [u8] = b"*0\r\n"
Empty array response.
Sourcepub fn encode_pong(buf: &mut [u8]) -> usize
pub fn encode_pong(buf: &mut [u8]) -> usize
Encode a PONG response directly to a buffer.
Sourcepub fn encode_null_bulk(buf: &mut [u8]) -> usize
pub fn encode_null_bulk(buf: &mut [u8]) -> usize
Encode a null bulk string response directly to a buffer.
Sourcepub fn encode_int(buf: &mut [u8], n: i64) -> usize
pub fn encode_int(buf: &mut [u8], n: i64) -> usize
Encode an integer response directly to a buffer.
Sourcepub fn encode_bulk(buf: &mut [u8], data: &[u8]) -> usize
pub fn encode_bulk(buf: &mut [u8], data: &[u8]) -> usize
Encode a bulk string response directly to a buffer (zero-copy for data).
Sourcepub fn encode_err(buf: &mut [u8], msg: &[u8]) -> usize
pub fn encode_err(buf: &mut [u8], msg: &[u8]) -> usize
Encode an error response directly to a buffer.