redust/
lib.rs

1//! A simple Redis client & RESP parser for Rust.
2//!
3//! ```
4//! use redust::{resp::{Data, from_data}, Connection};
5//! # use redust::Error;
6//!
7//! # tokio_test::block_on(async {
8//! let mut conn = Connection::new("localhost:6379").await?;
9//! let res: Data = conn.cmd(["PING"]).await?;
10//!
11//! let deserialized: &str = from_data(res)?;
12//! assert_eq!(deserialized, "PONG");
13//! # Ok::<_, Error>(())
14//! # });
15//! ```
16//!
17//! [`Connection`]s implement [`TryStream`](futures::TryStream) and [`Sink`](futures::Sink) for ergonomic
18//! and idiomatic use.
19//!
20//! Data is returned to the client as static [`resp::Data`]. The [resp] crate contains several
21//! [serde] utilities for converting RESP into Rust structures. For reading data from a connection,
22//! use [`resp::from_data`].
23//!
24//! # Additional Features
25//!
26//! - [`command`]: type-safe Redis interactions
27//! - [`pool`]: connection pooling with [bb8]
28//! - [`model`]: complex Redis responses, based on [serde]
29//! - [`script`]: Redis scripting utilities
30
31/// [`Command`](crate::command::Command) trait + impelementations.
32///
33/// Enables sending and receiving data to and from Redis using type-safe methods.
34///
35/// ```rust
36/// use redust::{command::connection::Hello, Connection};
37/// # use redust::Error;
38///
39/// # tokio_test::block_on(async {
40/// let mut conn = Connection::new("localhost:6379").await?;
41/// conn.run(Hello {
42///     username: Some("foo"),
43///     password: Some("bar"),
44/// }).await?;
45/// # Ok::<_, Error>(())
46/// # });
47/// ```
48#[cfg(feature = "command")]
49pub mod command;
50
51#[cfg(not(test))]
52mod connection;
53#[cfg(test)]
54pub mod connection;
55
56/// Redis models.
57#[cfg(feature = "model")]
58pub mod model;
59
60/// Manage Redis connections with [deadpool].
61///
62/// ```rust
63/// use redust::pool::{Pool, Manager};
64///
65/// # tokio_test::block_on(async {
66/// let manager = Manager::new("localhost:6379");
67/// let pool = Pool::builder(manager).build().expect("pool should be built");
68/// # });
69/// ```
70#[cfg(feature = "pool")]
71pub mod pool;
72
73/// Script utilities to handle SHA1 hash-based invocation.
74///
75/// ```rust
76/// use redust::{resp::Data, script::Script, Connection};
77/// # use redust::Error;
78/// use lazy_static::lazy_static;
79///
80/// lazy_static! {
81///     static ref MY_SCRIPT: Script =
82///         Script::new(b"return 'Hello ' .. redis.call('GET', KEYS[1]) .. ARGV[1]");
83/// }
84///
85/// # tokio_test::block_on(async {
86/// let mut conn = Connection::new("localhost:6379").await?;
87/// conn.cmd(["SET", "hello", "world"]).await?;
88///
89/// let res: Data = MY_SCRIPT
90///     .exec(&mut conn)
91///     .args(["!"])
92///     .keys(["hello"])
93///     .invoke()
94///     .await?;
95///
96/// assert_eq!(res, b"Hello world!");
97/// # Ok::<_, Error>(())
98/// # });
99/// ```
100#[cfg(feature = "script")]
101pub mod script;
102
103pub use redust_resp as resp;
104
105pub use connection::{Connection, SharedConnection};
106pub use resp::Codec;
107
108/// Static [`resp::Error`] returned from [`Connection`] and [`Codec`].
109pub type Error = resp::Error<'static>;
110/// Result with an error type defaulting to [`Error`].
111pub type Result<T, E = Error> = std::result::Result<T, E>;