redis_protocol/lib.rs
1#![cfg_attr(docsrs, deny(rustdoc::broken_intra_doc_links))]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![cfg_attr(docsrs, allow(unused_attributes))]
4#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
5
6//! # Redis Protocol
7//!
8//! Structs and functions for implementing the [RESP2](https://redis.io/topics/protocol) and [RESP3](https://github.com/antirez/RESP3/blob/master/spec.md) protocol.
9//!
10//! ## Examples
11//!
12//! ```rust
13//! # extern crate redis_protocol;
14//! # extern crate bytes;
15//!
16//! use redis_protocol::resp2::prelude::*;
17//! use bytes::{Bytes, BytesMut};
18//!
19//! fn main() {
20//! let frame = Frame::BulkString("foobar".into());
21//! let mut buf = BytesMut::new();
22//!
23//! let len = match encode_bytes(&mut buf, &frame) {
24//! Ok(l) => l,
25//! Err(e) => panic!("Error encoding frame: {:?}", e)
26//! };
27//! println!("Encoded {} bytes into buffer with contents {:?}", len, buf);
28//!
29//! let buf: Bytes = "*3\r\n$3\r\nFoo\r\n$-1\r\n$3\r\nBar\r\n".into();
30//! let (frame, consumed) = match decode(&buf) {
31//! Ok(Some((f, c))) => (f, c),
32//! Ok(None) => panic!("Incomplete frame."),
33//! Err(e) => panic!("Error parsing bytes: {:?}", e)
34//! };
35//! println!("Parsed frame {:?} and consumed {} bytes", frame, consumed);
36//!
37//! let key = "foobarbaz";
38//! println!("Hash slot for {}: {}", key, redis_keyslot(key.as_bytes()));
39//! }
40//! ```
41//!
42//! Note: if callers are not using the `index-map` feature then substitute `std::collections::HashMap` for any `IndexMap` types in these docs. `rustdoc` doesn't have a great way to show type substitutions based on feature flags.
43
44extern crate alloc;
45extern crate core;
46
47#[macro_use]
48extern crate log;
49#[macro_use]
50extern crate cookie_factory;
51
52#[cfg(test)]
53extern crate pretty_env_logger;
54
55#[cfg(feature = "index-map")]
56extern crate indexmap;
57
58#[macro_use]
59pub(crate) mod utils;
60pub(crate) mod nom_bytes;
61
62#[cfg(feature = "decode-mut")]
63mod decode_mut;
64/// Types and functions for implementing the RESP2 protocol.
65pub mod resp2;
66/// Types and functions for implementing the RESP3 protocol.
67pub mod resp3;
68/// Error types and general redis protocol types.
69pub mod types;
70
71pub use utils::{digits_in_number, redis_keyslot, resp2_frame_to_resp3, ZEROED_KB};