resp_rs/lib.rs
1//! Zero-copy RESP2 and RESP3 protocol parser and serializer.
2//!
3//! `resp-rs` provides high-performance parsing and serialization for the
4//! Redis Serialization Protocol (RESP), supporting both RESP2 and RESP3.
5//!
6//! # Features
7//!
8//! - **Zero-copy parsing** using `bytes::Bytes` for efficient memory management
9//! - **RESP2 and RESP3** support with separate frame types
10//! - **Streaming parser** for incremental data (handles partial reads and pipelining)
11//! - **High performance**: 4.8-8.0 GB/s throughput in benchmarks
12//!
13//! # Quick Start
14//!
15//! ## RESP3 (recommended for new projects)
16//!
17//! ```
18//! use bytes::Bytes;
19//! use resp_rs::resp3;
20//!
21//! let data = Bytes::from("+OK\r\n");
22//! let (frame, remaining) = resp3::parse_frame(data).unwrap();
23//! assert_eq!(frame, resp3::Frame::SimpleString(Bytes::from("OK")));
24//! ```
25//!
26//! ## RESP2
27//!
28//! ```
29//! use bytes::Bytes;
30//! use resp_rs::resp2;
31//!
32//! let data = Bytes::from("+OK\r\n");
33//! let (frame, remaining) = resp2::parse_frame(data).unwrap();
34//! assert_eq!(frame, resp2::Frame::SimpleString(Bytes::from("OK")));
35//! ```
36//!
37//! ## Streaming parser (handles partial reads)
38//!
39//! ```
40//! use bytes::Bytes;
41//! use resp_rs::resp3::Parser;
42//!
43//! let mut parser = Parser::new();
44//!
45//! // Feed partial data
46//! parser.feed(Bytes::from("+HEL"));
47//! assert!(parser.next_frame().unwrap().is_none()); // Incomplete
48//!
49//! // Feed the rest
50//! parser.feed(Bytes::from("LO\r\n"));
51//! let frame = parser.next_frame().unwrap().unwrap(); // Complete!
52//! ```
53
54pub mod resp2;
55pub mod resp3;
56
57/// Errors that can occur during RESP parsing.
58#[derive(Debug, Clone, PartialEq, thiserror::Error)]
59pub enum ParseError {
60 /// Not enough data to parse a complete frame.
61 #[error("incomplete data")]
62 Incomplete,
63
64 /// Invalid type tag byte at the start of a frame.
65 #[error("invalid tag byte: 0x{0:02x}")]
66 InvalidTag(u8),
67
68 /// Invalid length in a bulk string or collection.
69 #[error("invalid length")]
70 BadLength,
71
72 /// Invalid UTF-8 in a string value.
73 #[error("invalid UTF-8")]
74 Utf8Error,
75
76 /// Invalid frame format.
77 #[error("invalid format")]
78 InvalidFormat,
79
80 /// Invalid boolean value (not 't' or 'f').
81 #[error("invalid boolean value")]
82 InvalidBoolean,
83
84 /// Invalid special float (not 'inf', '-inf', or 'nan').
85 #[error("invalid special float")]
86 InvalidSpecialFloat,
87
88 /// Integer value overflowed i64 range.
89 #[error("integer overflow")]
90 Overflow,
91}