1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! # serde-RESP
//! Redis RESP protocol serialization and deserialization with serde.
//! [Read Specification](https://redis.io/topics/protocol)
//!
//! ## Usage
//! IMPORTANT: Do NOT serialize and deserialize with any other types besides [RESPType](RESPType)! You may get panic or incorrect results!
//!
//! Here are the RESP types and their corresponding Rust types for serde.
//!
//! - `SimpleString`
//!     + [RESPType::SimpleString(String)](RESPType::SimpleString)
//! - `Error`
//!     + [RESPType::Error(String)](RESPType::Error)
//! - `Integer`
//!     + [RESPType::Integer(i64)](RESPType::Integer)
//! - `BulkString`
//!     + [RESPType::BulkString(Option<Vec<u8>>)](RESPType::BulkString)
//!         + Use `None` for null bulk strings and `Some` for non-null ones.
//! - `Array`
//!     + [RESPType::Array(Option<Vec<RESPType>>)](RESPType::Array)
//!         + Use `None` for null arrays and `Some` for non-null ones.
//!
//! To serialize, use [ser::to_string](ser::to_string) or [ser::to_writer](ser::to_writer).
//!
//! To deserialize, use [de::from_str](de::from_str) or [de::from_reader](de::from_reader) or [de::from_buf_reader](de::from_buf_reader).
//!
//! For usage examples, refer to [RESPType](RESPType)

pub mod de;
mod error;
pub mod ser;

pub use error::{Error, Result};

#[derive(Eq, PartialEq, Clone, Debug)]
/// This enum creates a one-to-one type mapping with RESP types.
/// Please only use variants of this type for serde operations.
pub enum RESPType {
    /// Correspond to simple string in RESP.
    ///
    /// # Examples
    /// ```
    /// use serde_resp::{ser, de, RESPType};
    ///
    /// /// Serialization
    /// let obj = RESPType::SimpleString("OK".to_owned());
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("+OK\r\n".to_owned(), serialized);
    ///
    /// /// Deserialization
    /// let deserialized: RESPType = de::from_str("+OK\r\n").unwrap();
    /// assert_eq!(RESPType::SimpleString("OK".to_owned()), deserialized);
    /// ```
    SimpleString(String),
    /// Correspond to error string in RESP.
    ///
    /// # Examples
    /// ```
    /// use serde_resp::{ser, de, RESPType};
    ///
    /// /// Serialization
    /// // Example 1
    /// let obj = RESPType::Error("ERR unknown command 'foobar'".to_owned());
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("-ERR unknown command 'foobar'\r\n".to_owned(), serialized);
    ///
    /// // Example 2
    /// let obj = RESPType::Error(
    /// "WRONGTYPE Operation against a key holding the wrong kind of value".to_owned(),
    /// );
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!(
    ///     "-WRONGTYPE Operation against a key holding the wrong kind of value\r\n".to_owned(),
    ///     serialized
    /// );
    ///
    /// /// Deserialization
    /// let deserialized: RESPType = de::from_str("-ERR unknown command 'foobar'\r\n").unwrap();
    /// assert_eq!(RESPType::Error("ERR unknown command 'foobar'".to_owned()), deserialized);
    /// ```
    Error(String),
    /// Correspond to integer in RESP.
    ///
    /// # Examples
    /// ```
    /// use serde_resp::{ser, de, RESPType};
    ///
    /// /// Serialization
    /// // Regular Example
    /// let obj = RESPType::Integer(1000i64);
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!(":1000\r\n".to_owned(), serialized);
    ///
    /// /// Deserialization
    /// let deserialized: RESPType = de::from_str(":1000\r\n").unwrap();
    /// assert_eq!(RESPType::Integer(1000), deserialized);
    /// ```
    Integer(i64),
    /// Correspond to bulk string in RESP. Use `None` for null bulk string and Some for non-null ones.
    ///
    /// According to specification, bulk string is binary-safe so it is NOT recommended to use [ser::to_string](ser::to_string) (may cause [Error::FromUtf8](Error::FromUtf8)).
    /// Use [ser::to_writer](ser::to_writer) to write to a byte buffer instead.
    ///
    /// # Examples
    /// ```
    /// use serde_resp::{ser, de, RESPType};
    ///
    /// /// Serialization
    /// // Regular Example
    /// let obj = RESPType::BulkString(Some(b"foobar".to_vec()));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("$6\r\nfoobar\r\n".to_owned(), serialized);
    ///
    /// // Empty
    /// let obj = RESPType::BulkString(Some(b"".to_vec()));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("$0\r\n\r\n".to_owned(), serialized);
    ///
    /// // Null
    /// let obj = RESPType::BulkString(None);
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("$-1\r\n".to_owned(), serialized);
    ///
    /// /// Deserialization
    /// // Regular Example
    /// let deserialized: RESPType = de::from_str("$6\r\nfoobar\r\n").unwrap();
    /// assert_eq!(RESPType::BulkString(Some(b"foobar".to_vec())), deserialized);
    ///
    /// // Empty
    /// let deserialized: RESPType = de::from_str("$0\r\n\r\n").unwrap();
    /// assert_eq!(RESPType::BulkString(Some(b"".to_vec())), deserialized);
    ///
    /// // Null
    /// let deserialized: RESPType = de::from_str("$-1\r\n").unwrap();
    /// assert_eq!(RESPType::BulkString(None), deserialized);
    /// ```
    BulkString(Option<Vec<u8>>),
    /// Correspond to array in RESP. Use None for null array and Some for non-null ones.
    ///
    /// Mixed type, arrays of arrays are allowed.
    ///
    /// # Examples
    /// ```
    /// use serde_resp::{ser, de, RESPType};
    ///
    /// /// Serialization
    /// // Empty
    /// let obj = RESPType::Array(Some(vec![]));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("*0\r\n".to_owned(), serialized);
    ///
    /// // Regular Example
    /// let obj = RESPType::Array(Some(vec![
    ///     RESPType::BulkString(Some(b"foo".to_vec())),
    ///     RESPType::BulkString(Some(b"bar".to_vec())),
    /// ]));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n".to_owned(), serialized);
    ///
    /// // Another Regular Example
    /// let obj = RESPType::Array(Some(vec![
    ///     RESPType::Integer(1),
    ///     RESPType::Integer(2),
    ///     RESPType::Integer(3),
    /// ]));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("*3\r\n:1\r\n:2\r\n:3\r\n".to_owned(), serialized);
    ///
    /// // Mixed Type
    /// let obj = RESPType::Array(Some(vec![
    ///     RESPType::Integer(1),
    ///     RESPType::Integer(2),
    ///     RESPType::Integer(3),
    ///     RESPType::Integer(4),
    ///     RESPType::BulkString(Some(b"foobar".to_vec())),
    /// ]));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!(
    ///     "*5\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n".to_owned(),
    ///     serialized
    /// );
    ///
    /// // Null Array
    /// let obj = RESPType::Array(None);
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!("*-1\r\n".to_owned(), serialized);
    ///
    /// // Arrays of Arrays
    /// let obj = RESPType::Array(Some(vec![
    ///     RESPType::Array(Some(vec![
    ///         RESPType::Integer(1),
    ///         RESPType::Integer(2),
    ///         RESPType::Integer(3),
    ///     ])),
    ///     RESPType::Array(Some(vec![
    ///         RESPType::SimpleString("Foo".to_owned()),
    ///         RESPType::Error("Bar".to_owned()),
    ///     ])),
    /// ]));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!(
    ///     "*2\r\n*3\r\n:1\r\n:2\r\n:3\r\n*2\r\n+Foo\r\n-Bar\r\n".to_owned(),
    ///     serialized
    /// );
    ///
    /// // Null elements in Arrays
    /// let obj = RESPType::Array(Some(vec![
    ///     RESPType::BulkString(Some(b"foo".to_vec())),
    ///     RESPType::BulkString(None),
    ///     RESPType::BulkString(Some(b"bar".to_vec())),
    /// ]));
    /// let serialized = ser::to_string(&obj).unwrap();
    /// assert_eq!(
    ///     "*3\r\n$3\r\nfoo\r\n$-1\r\n$3\r\nbar\r\n".to_owned(),
    ///     serialized
    /// );
    ///
    /// /// Deserialization
    /// // Null
    /// let deserialized: RESPType = de::from_str("*-1\r\n").unwrap();
    /// assert_eq!(RESPType::Array(None), deserialized);
    ///
    /// // Mixed Type
    /// let deserialized: RESPType = de::from_str("*2\r\n:1\r\n$6\r\nfoobar\r\n").unwrap();
    /// let expected = RESPType::Array(Some(vec![
    ///     RESPType::Integer(1),
    ///     RESPType::BulkString(Some(b"foobar".to_vec()))
    /// ]));
    /// assert_eq!(expected, deserialized);
    ///
    /// // Arrays of Arrays with Null Bulk String
    /// let deserialized: RESPType = de::from_str("*3\r\n*3\r\n:1\r\n:2\r\n:3\r\n$-1\r\n*2\r\n+Foo\r\n-Bar\r\n").unwrap();
    /// let expected = RESPType::Array(Some(vec![
    ///     RESPType::Integer(1),
    ///     RESPType::BulkString(Some(b"foobar".to_vec()))
    /// ]));
    /// let expected = RESPType::Array(Some(vec![
    ///     RESPType::Array(Some(vec![
    ///         RESPType::Integer(1),
    ///         RESPType::Integer(2),
    ///         RESPType::Integer(3),
    ///     ])),
    ///     RESPType::BulkString(None),
    ///     RESPType::Array(Some(vec![
    ///         RESPType::SimpleString("Foo".to_owned()),
    ///         RESPType::Error("Bar".to_owned()),
    ///     ])),
    /// ]));
    /// assert_eq!(expected, deserialized);
    /// ```
    Array(Option<Vec<RESPType>>),
}