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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
//! # serde-RESP //! Redis RESP protocol serialization and deserialization with serde. //! [Read Specification](https://redis.io/topics/protocol) //! //! ## Usage //! **IMPORTANT: Do NOT (de)serialize with any other types besides `RESP/RESPType`! You may get panic or incorrect results!** //! [Why?](https://github.com/DEDZTBH/serde-RESP/blob/master/README.md#why-resptype-wrapper) //! //! Here are the RESP types and their corresponding Rust types for (de)serialize operations. //! //! - `Simple String` //! + [RESP::SimpleString(String)](RESPType::SimpleString) //! - `Error` //! + [RESP::Error(String)](RESPType::Error) //! - `Integer` //! + [RESP::Integer(i64)](RESPType::Integer) //! - `Bulk String` //! + [RESP::BulkString(Option<Vec<u8>>)](RESPType::BulkString) //! + Use `None` for null bulk strings and `Some` for non-null ones. //! - `Array` //! + [RESP::Array(Option<Vec<RESP>>)](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 [RESP](RESP) //! //! ## Macros //! //! Since 0.3.0, you can start using very handy macros! Here is a demo: //! ``` //! use serde_resp::{array, array_null, bulk, bulk_null, de, err_str, int, ser, simple, RESP}; //! let resp_array = array![ //! simple!("simple string".to_owned()), //! err_str!("error string".to_owned()), //! int!(42), //! bulk!(b"bulk string".to_vec()), //! bulk_null!(), //! array![ //! simple!("arrays of arrays!".to_owned()), //! array![simple!("OK ENOUGH!".to_owned())], //! ], //! array_null!(), //! ]; //! let serialized = ser::to_string(&resp_array).unwrap(); //! assert_eq!( //! "*7\r\n+simple string\r\n-error string\r\n:42\r\n$11\r\nbulk string\r\n$-1\r\n*2\r\n+arrays of arrays!\r\n*1\r\n+OK ENOUGH!\r\n*-1\r\n", //! serialized //! ); //! let deserialized = de::from_str(&serialized).unwrap(); //! assert_eq!(resp_array, deserialized); //! ``` pub mod de; mod error; mod macros; pub mod ser; pub use error::{Error, Result}; /// This enum creates a one-to-one type mapping with RESP types. /// Please only use variants of this type for (de)serialize operations. #[derive(Eq, PartialEq, Clone, Debug)] pub enum RESPType { /// Correspond to simple string in RESP. /// Also refer to [simple!](simple!) macro. /// /// # Examples /// ``` /// use serde_resp::{de, ser, simple, RESP}; /// /// /// Serialization /// let obj = simple!("OK".to_owned()); // equivalent to RESP::SimpleString("OK".to_owned()); /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("+OK\r\n".to_owned(), serialized); /// /// /// Deserialization /// let deserialized: RESP = de::from_str("+OK\r\n").unwrap(); /// assert_eq!(simple!("OK".to_owned()), deserialized); /// ``` SimpleString(String), /// Correspond to error string in RESP. /// Also refer to [err_str!](err_str!) macro. /// /// # Examples /// ``` /// use serde_resp::{de, err_str, ser, RESP}; /// /// /// Serialization /// // Example 1 /// let obj = err_str!("ERR unknown command 'foobar'".to_owned()); // equivalent to RESP::Error(...) /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("-ERR unknown command 'foobar'\r\n".to_owned(), serialized); /// /// // Example 2 /// let obj = err_str!("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: RESP = de::from_str("-ERR unknown command 'foobar'\r\n").unwrap(); /// assert_eq!(err_str!("ERR unknown command 'foobar'".to_owned()), deserialized); /// ``` Error(String), /// Correspond to integer in RESP. /// Also refer to [int!](int!) macro. /// /// # Examples /// ``` /// use serde_resp::{de, int, ser, RESP}; /// /// /// Serialization /// // Regular Example /// let obj = int!(1000); // equivalent to RESP::Integer(1000); /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!(":1000\r\n".to_owned(), serialized); /// /// /// Deserialization /// let deserialized: RESP = de::from_str(":1000\r\n").unwrap(); /// assert_eq!(int!(1000), deserialized); /// ``` Integer(i64), /// Correspond to bulk string in RESP. Use `None` for null bulk string and Some for non-null ones. /// Also refer to [bulk!](bulk!) macro and [bulk_null!](bulk_null!) macro. /// /// 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::{bulk, bulk_null, de, ser, RESP}; /// /// /// Serialization /// // Regular Example /// let obj = bulk!(b"foobar".to_vec()); // equivalent to RESP::BulkString(Some(...)) /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("$6\r\nfoobar\r\n".to_owned(), serialized); /// /// // Empty /// let obj = bulk!(b"".to_vec()); /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("$0\r\n\r\n".to_owned(), serialized); /// /// // Null /// let obj = bulk_null!(); // equivalent to RESP::BulkString(None) /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("$-1\r\n".to_owned(), serialized); /// /// /// Deserialization /// // Regular Example /// let deserialized: RESP = de::from_str("$6\r\nfoobar\r\n").unwrap(); /// assert_eq!(bulk!(b"foobar".to_vec()), deserialized); /// /// // Empty /// let deserialized: RESP = de::from_str("$0\r\n\r\n").unwrap(); /// assert_eq!(bulk!(b"".to_vec()), deserialized); /// /// // Null /// let deserialized: RESP = de::from_str("$-1\r\n").unwrap(); /// assert_eq!(bulk_null!(), deserialized); /// ``` BulkString(Option<Vec<u8>>), /// Correspond to array in RESP. Use None for null array and Some for non-null ones. /// Also refer to [array!](array!) macro and [array_null!](array_null!) macro. /// /// Mixed type, arrays of arrays are allowed. /// /// # Examples /// ``` /// use serde_resp::{array, array_null, bulk, bulk_null, de, err_str, int, ser, simple, RESP}; /// /// /// Serialization /// // Empty /// let obj = array![]; // equivalent to RESP::Array(Some(vec![])) /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("*0\r\n".to_owned(), serialized); /// /// // Regular Example /// let obj = array![ /// bulk!(b"foo".to_vec()), /// bulk!(b"bar".to_vec()), /// ]; // equivalent to RESP::Array(Some(vec![bulk!(...), bulk!(...)])) /// 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 = array![ /// int!(1), /// int!(2), /// int!(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 = array![ /// int!(1), /// int!(2), /// int!(3), /// int!(4), /// bulk!(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 = array_null!(); // equivalent to RESP::Array(None) /// let serialized = ser::to_string(&obj).unwrap(); /// assert_eq!("*-1\r\n".to_owned(), serialized); /// /// // Arrays of Arrays /// let obj = array![ /// array![ /// int!(1), /// int!(2), /// int!(3), /// ], /// array![ /// simple!("Foo".to_owned()), /// err_str!("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 = array![ /// bulk!(b"foo".to_vec()), /// bulk_null!(), /// bulk!(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: RESP = de::from_str("*-1\r\n").unwrap(); /// assert_eq!(array_null!(), deserialized); /// /// // Mixed Type /// let deserialized: RESP = de::from_str("*2\r\n:1\r\n$6\r\nfoobar\r\n").unwrap(); /// let expected = array![ /// int!(1), /// bulk!(b"foobar".to_vec()), /// ]; /// assert_eq!(expected, deserialized); /// /// // Arrays of Arrays with Null Bulk String /// let deserialized: RESP = 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 = array![ /// int!(1), /// bulk!(b"foobar".to_vec()), /// ]; /// let expected = array![ /// array![ /// int!(1), /// int!(2), /// int!(3), /// ], /// bulk_null!(), /// array![ /// simple!("Foo".to_owned()), /// err_str!("Bar".to_owned()), /// ], /// ]; /// assert_eq!(expected, deserialized); /// ``` Array(Option<Vec<RESPType>>), } /// Refer to [RESPType](RESPType). This is just an alias. pub type RESP = RESPType;