mini_redis/
lib.rs

1//! A minimal (i.e. very incomplete) implementation of a Redis server and
2//! client.
3//!
4//! The purpose of this project is to provide a larger example of an
5//! asynchronous Rust project built with Tokio. Do not attempt to run this in
6//! production... seriously.
7//!
8//! # Layout
9//!
10//! The library is structured such that it can be used with guides. There are
11//! modules that are public that probably would not be public in a "real" redis
12//! client library.
13//!
14//! The major components are:
15//!
16//! * `server`: Redis server implementation. Includes a single `run` function
17//!   that takes a `TcpListener` and starts accepting redis client connections.
18//!
19//! * `client`: an asynchronous Redis client implementation. Demonstrates how to
20//!   build clients with Tokio.
21//!
22//! * `cmd`: implementations of the supported Redis commands.
23//!
24//! * `frame`: represents a single Redis protocol frame. A frame is used as an
25//!   intermediate representation between a "command" and the byte
26//!   representation.
27
28pub mod blocking_client;
29pub mod client;
30
31pub mod cmd;
32pub use cmd::Command;
33
34mod connection;
35pub use connection::Connection;
36
37pub mod frame;
38pub use frame::Frame;
39
40mod db;
41use db::Db;
42
43mod parse;
44use parse::{Parse, ParseError};
45
46pub mod server;
47
48mod buffer;
49pub use buffer::{buffer, Buffer};
50
51mod shutdown;
52use shutdown::Shutdown;
53
54/// Default port that a redis server listens on.
55///
56/// Used if no port is specified.
57pub const DEFAULT_PORT: &str = "6379";
58
59/// Error returned by most functions.
60///
61/// When writing a real application, one might want to consider a specialized
62/// error handling crate or defining an error type as an `enum` of causes.
63/// However, for our example, using a boxed `std::error::Error` is sufficient.
64///
65/// For performance reasons, boxing is avoided in any hot path. For example, in
66/// `parse`, a custom error `enum` is defined. This is because the error is hit
67/// and handled during normal execution when a partial frame is received on a
68/// socket. `std::error::Error` is implemented for `parse::Error` which allows
69/// it to be converted to `Box<dyn std::error::Error>`.
70pub type Error = Box<dyn std::error::Error + Send + Sync>;
71
72/// A specialized `Result` type for mini-redis operations.
73///
74/// This is defined as a convenience.
75pub type Result<T> = std::result::Result<T, Error>;