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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! # commands
//!
//! Defines the redis commands exposed by the redis client.
//!

#[cfg(test)]
#[path = "./commands_test.rs"]
mod commands_test;

use crate::client::Client;
use crate::types::{RedisArg, RedisBoolResult, RedisEmptyResult, RedisResult, RedisStringResult};
use std::collections::HashMap;
use std::str::FromStr;

/// Defines the redis commands exposed by the redis client.
impl Client {
    /// See redis [AUTH](https://redis.io/commands/auth) command.
    ///
    /// # Example
    ///
    /// ```
    /// # match simple_redis::create("redis://127.0.0.1:6379/") {
    /// #     Ok(mut client) =>  {
    ///           match client.auth("my_password") {
    ///               Err(error) => println!("Auth error: {}", error),
    ///               _ => println!("Authenticated")
    ///           }
    /// #     },
    /// #     Err(error) => println!("Unable to create Redis client: {}", error)
    /// # }
    /// ```
    ///
    pub fn auth(&mut self, password: &str) -> RedisEmptyResult {
        self.run_command_empty_response("AUTH", vec![password])
    }

    /// See redis [ECHO](https://redis.io/commands/echo) command.
    pub fn echo(&mut self, value: &str) -> RedisStringResult {
        self.run_command_string_response("ECHO", vec![value])
    }

    /// See redis [PUBLISH](https://redis.io/commands/publish) command.
    ///
    /// # Example
    ///
    /// ```
    /// # let mut client = simple_redis::create("redis://127.0.0.1:6379/").unwrap();
    /// match client.publish("important_notifications", "message text") {
    ///   Err(error) => println!("Publish error: {}", error),
    ///   _ => println!("Message published")
    /// }
    /// ```
    ///
    pub fn publish(&mut self, channel: &str, message: &str) -> RedisEmptyResult {
        self.run_command_empty_response("PUBLISH", vec![channel, message])
    }

    /// See redis [GET](https://redis.io/commands/get) command.
    ///
    /// # Example
    ///
    /// ```
    /// # let mut client = simple_redis::create("redis://127.0.0.1:6379/").unwrap();
    /// match client.get::<i64>("my_key") {
    ///     Ok(value) => println!("Read value from Redis: {}", value),
    ///     Err(error) => println!("Unable to get value from Redis: {}", error)
    /// }
    /// ```
    ///
    pub fn get<T: FromStr>(self: &mut Client, key: &str) -> RedisResult<T> {
        self.run_command_from_string_response("GET", vec![key])
    }

    /// See redis [GET](https://redis.io/commands/get) command.<br>
    /// This function will always return a String response.
    ///
    /// # Example
    ///
    /// ```
    /// # let mut client = simple_redis::create("redis://127.0.0.1:6379/").unwrap();
    /// match client.get_string("my_key") {
    ///     Ok(value) => println!("Read value from Redis: {}", value),
    ///     Err(error) => println!("Unable to get value from Redis: {}", error)
    /// }
    /// ```
    ///
    pub fn get_string(self: &mut Client, key: &str) -> RedisStringResult {
        self.run_command_string_response("GET", vec![key])
    }

    /// See redis [SET](https://redis.io/commands/set) command.
    ///
    /// # Example
    ///
    /// ```
    /// # let mut client = simple_redis::create("redis://127.0.0.1:6379/").unwrap();
    /// match client.set("my_key", "my_value") {
    ///     Err(error) => println!("Unable to set value in Redis: {}", error),
    ///     _ => println!("Value set in Redis")
    /// }
    /// ```
    ///
    pub fn set<T: RedisArg>(self: &mut Client, key: &str, value: T) -> RedisEmptyResult {
        self.run_command_empty_response("SET", vec![key, &value.to_string()])
    }

    /// See redis [SETEX](https://redis.io/commands/setex) command.
    ///
    /// # Example
    ///
    /// ```
    /// # let mut client = simple_redis::create("redis://127.0.0.1:6379/").unwrap();
    /// match client.setex("my_key", "my_value", 10) {
    ///     Err(error) => println!("Unable to set value in Redis: {}", error),
    ///     _ => println!("Value set in Redis and will expire in 10 seconds")
    /// }
    /// ```
    ///
    pub fn setex<T: RedisArg>(&mut self, key: &str, value: T, seconds: usize) -> RedisEmptyResult {
        self.run_command_empty_response(
            "SETEX",
            vec![key, &*seconds.to_string(), &value.to_string()],
        )
    }

    /// See redis [SETNX](https://redis.io/commands/setnx) command.
    pub fn setnx<T: RedisArg>(&mut self, key: &str, value: T) -> RedisEmptyResult {
        self.run_command_empty_response("SETNX", vec![key, &value.to_string()])
    }

    /// See redis [GETSET](https://redis.io/commands/getset) command.
    pub fn getset<T: RedisArg, V: FromStr>(&mut self, key: &str, value: T) -> RedisResult<V> {
        self.run_command_from_string_response::<V>("GETSET", vec![key, &value.to_string()])
    }

    /// See redis [GETSET](https://redis.io/commands/getset) command.
    pub fn getset_string<T: RedisArg>(&mut self, key: &str, value: T) -> RedisStringResult {
        self.run_command_string_response("GETSET", vec![key, &value.to_string()])
    }

    /// See redis [DEL](https://redis.io/commands/del) command.
    pub fn del(&mut self, key: &str) -> RedisEmptyResult {
        self.run_command_empty_response("DEL", vec![key])
    }

    /// See redis [EXISTS](https://redis.io/commands/exists) command.
    pub fn exists(&mut self, key: &str) -> RedisBoolResult {
        self.run_command_bool_response("EXISTS", vec![key])
    }

    /// See redis [EXPIRE](https://redis.io/commands/expire) command.
    pub fn expire(&mut self, key: &str, seconds: usize) -> RedisEmptyResult {
        self.run_command_empty_response("EXPIRE", vec![key, &*seconds.to_string()])
    }

    /// See redis [PEXPIRE](https://redis.io/commands/pexpire) command.
    pub fn pexpire(&mut self, key: &str, millies: usize) -> RedisEmptyResult {
        self.run_command_empty_response("PEXPIRE", vec![key, &*millies.to_string()])
    }

    /// See redis [PERSIST](https://redis.io/commands/persist) command.
    pub fn persist(&mut self, key: &str) -> RedisEmptyResult {
        self.run_command_empty_response("PERSIST", vec![key])
    }

    /// See redis [RENAME](https://redis.io/commands/rename) command.
    pub fn rename(&mut self, key: &str, new_key: &str) -> RedisEmptyResult {
        self.run_command_empty_response("RENAME", vec![key, new_key])
    }

    /// See redis [RENAMENX](https://redis.io/commands/renamenx) command.
    pub fn renamenx(&mut self, key: &str, new_key: &str) -> RedisEmptyResult {
        self.run_command_empty_response("RENAMENX", vec![key, new_key])
    }

    /// See redis [APPEND](https://redis.io/commands/append) command.
    pub fn append(&mut self, key: &str, value: &str) -> RedisEmptyResult {
        self.run_command_empty_response("APPEND", vec![key, value])
    }

    /// See redis [INCR](https://redis.io/commands/incr) command.
    pub fn incr(&mut self, key: &str) -> RedisResult<i64> {
        self.run_command::<i64>("INCR", vec![key])
    }

    /// See redis [INCRBY](https://redis.io/commands/incrby) command.
    pub fn incrby<T: RedisArg>(&mut self, key: &str, value: T) -> RedisResult<i64> {
        self.run_command::<i64>("INCRBY", vec![key, &*value.to_string()])
    }

    /// See redis [INCRBYFLOAT](https://redis.io/commands/incrbyfloat) command.
    pub fn incrbyfloat<T: RedisArg>(&mut self, key: &str, value: T) -> RedisResult<f64> {
        self.run_command::<f64>("INCRBYFLOAT", vec![key, &*value.to_string()])
    }

    /// See redis [STRLEN](https://redis.io/commands/strlen) command.
    pub fn strlen(&mut self, key: &str) -> RedisResult<i32> {
        self.run_command::<i32>("STRLEN", vec![key])
    }

    /// See redis [KEYS](https://redis.io/commands/keys) command.
    pub fn keys(&mut self, pattern: &str) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("KEYS", vec![pattern])
    }

    /// See redis [HGET](https://redis.io/commands/hget) command.
    pub fn hget<T: FromStr>(self: &mut Client, key: &str, field: &str) -> RedisResult<T> {
        self.run_command_from_string_response("HGET", vec![key, field])
    }

    /// See redis [HGET](https://redis.io/commands/hget) command.
    pub fn hget_string(self: &mut Client, key: &str, field: &str) -> RedisStringResult {
        self.run_command_string_response("HGET", vec![key, field])
    }

    /// See redis [HGETALL](https://redis.io/commands/hgetall) command.
    ///
    /// # Example
    ///
    /// ```
    /// # let mut client = simple_redis::create("redis://127.0.0.1:6379/").unwrap();
    /// match client.hgetall("my_map") {
    ///     Ok(map) => {
    ///         match map.get("my_field") {
    ///             Some(value) => println!("Got field value from map: {}", value),
    ///             None => println!("Map field is emtpy"),
    ///         }
    ///     },
    ///     Err(error) => println!("Unable to read map from Redis: {}", error),
    /// }
    /// ```
    ///
    pub fn hgetall(self: &mut Client, key: &str) -> RedisResult<HashMap<String, String>> {
        self.run_command::<HashMap<String, String>>("HGETALL", vec![key])
    }

    /// See redis [HSET](https://redis.io/commands/hset) command.
    pub fn hset<T: RedisArg>(
        self: &mut Client,
        key: &str,
        field: &str,
        value: T,
    ) -> RedisEmptyResult {
        self.run_command_empty_response("HSET", vec![key, field, &value.to_string()])
    }

    /// See redis [HSETNX](https://redis.io/commands/hsetnx) command.
    pub fn hsetnx<T: RedisArg>(
        self: &mut Client,
        key: &str,
        field: &str,
        value: T,
    ) -> RedisEmptyResult {
        self.run_command_empty_response("HSETNX", vec![key, field, &value.to_string()])
    }

    /// See redis [HDEL](https://redis.io/commands/hdel) command.
    pub fn hdel(self: &mut Client, key: &str, field: &str) -> RedisEmptyResult {
        self.run_command_empty_response("HDEL", vec![key, field])
    }

    /// See redis [HEXISTS](https://redis.io/commands/hexists) command.
    pub fn hexists(self: &mut Client, key: &str, field: &str) -> RedisBoolResult {
        self.run_command_bool_response("HEXISTS", vec![key, field])
    }

    /// See redis [HKEYS](https://redis.io/commands/hkeys) command.
    pub fn hkeys(&mut self, key: &str) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("HKEYS", vec![key])
    }

    /// See redis [HVALS](https://redis.io/commands/hvals) command.
    pub fn hvals(&mut self, key: &str) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("HVALS", vec![key])
    }

    /// See redis [LSET](https://redis.io/commands/lset) command.
    pub fn lset<T: RedisArg>(
        self: &mut Client,
        key: &str,
        index: isize,
        value: T,
    ) -> RedisEmptyResult {
        self.run_command_empty_response("LSET", vec![key, &index.to_string(), &value.to_string()])
    }

    /// See redis [HGET](https://redis.io/commands/lindex) command.
    pub fn lindex<T: FromStr>(self: &mut Client, key: &str, index: isize) -> RedisResult<T> {
        self.run_command_from_string_response("LINDEX", vec![key, &index.to_string()])
    }

    /// See redis [HGET](https://redis.io/commands/lindex) command.
    pub fn lindex_string(self: &mut Client, key: &str, index: isize) -> RedisStringResult {
        self.run_command_string_response("LINDEX", vec![key, &index.to_string()])
    }

    /// See redis [LLEN](https://redis.io/commands/llen) command.
    pub fn llen(self: &mut Client, key: &str) -> RedisResult<i32> {
        self.run_command::<i32>("LLEN", vec![key])
    }

    /// See redis [LPOP](https://redis.io/commands/lpop) command.
    pub fn lpop<T: FromStr>(self: &mut Client, key: &str) -> RedisResult<T> {
        self.run_command_from_string_response("LPOP", vec![key])
    }

    /// See redis [LPUSH](https://redis.io/commands/lpush) command.
    pub fn lpush<T: RedisArg>(self: &mut Client, key: &str, value: T) -> RedisEmptyResult {
        self.run_command_empty_response("LPUSH", vec![key, &value.to_string()])
    }

    /// See redis [LPUSHX](https://redis.io/commands/lpushx) command.
    pub fn lpushx<T: RedisArg>(self: &mut Client, key: &str, value: T) -> RedisEmptyResult {
        self.run_command_empty_response("LPUSHX", vec![key, &value.to_string()])
    }

    /// See redis [LRANGE](https://redis.io/commands/lrange) command.
    pub fn lrange(
        self: &mut Client,
        key: &str,
        start: isize,
        stop: isize,
    ) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("LRANGE", vec![key, &start.to_string(), &stop.to_string()])
    }

    /// See redis [LREM](https://redis.io/commands/lrem) command.
    pub fn lrem<T: RedisArg>(
        self: &mut Client,
        key: &str,
        count: isize,
        value: T,
    ) -> RedisEmptyResult {
        self.run_command_empty_response("LREM", vec![key, &count.to_string(), &value.to_string()])
    }

    /// See redis [LTRIM](https://redis.io/commands/ltrim) command.
    pub fn ltrim(self: &mut Client, key: &str, start: isize, stop: isize) -> RedisEmptyResult {
        self.run_command_empty_response("LTRIM", vec![key, &start.to_string(), &stop.to_string()])
    }

    /// See redis [RPOP](https://redis.io/commands/rpop) command.
    pub fn rpop<T: FromStr>(self: &mut Client, key: &str) -> RedisResult<T> {
        self.run_command_from_string_response("RPOP", vec![key])
    }

    /// See redis [RPUSH](https://redis.io/commands/rpush) command.
    pub fn rpush<T: RedisArg>(self: &mut Client, key: &str, value: T) -> RedisEmptyResult {
        self.run_command_empty_response("RPUSH", vec![key, &value.to_string()])
    }

    /// See redis [RPUSHX](https://redis.io/commands/rpushx) command.
    pub fn rpushx<T: RedisArg>(self: &mut Client, key: &str, value: T) -> RedisEmptyResult {
        self.run_command_empty_response("RPUSHX", vec![key, &value.to_string()])
    }

    /// See redis [SADD](https://redis.io/commands/sadd) command.
    pub fn sadd(self: &mut Client, key: &str, member: &str) -> RedisResult<i32> {
        self.run_command::<i32>("SADD", vec![key, member])
    }

    /// See redis [SCARD](https://redis.io/commands/scard) command.
    pub fn scard(self: &mut Client, key: &str) -> RedisResult<i32> {
        self.run_command::<i32>("SCARD", vec![key])
    }

    /// See redis [SDIFF](https://redis.io/commands/sdiff) command.
    pub fn sdiff(self: &mut Client, keys: Vec<&str>) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("SDIFF", keys)
    }

    /// See redis [SISMEMBER](https://redis.io/commands/sismember) command.
    pub fn sismember(self: &mut Client, key: &str, member: &str) -> RedisBoolResult {
        self.run_command("SISMEMBER", vec![key, member])
    }

    /// See redis [SMEMBERS](https://redis.io/commands/smembers) command.
    pub fn smembers(self: &mut Client, key: &str) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("SMEMBERS", vec![key])
    }

    /// See redis [SMOVE](https://redis.io/commands/smove) command.
    pub fn smove(
        self: &mut Client,
        source_key: &str,
        destination_key: &str,
        member: &str,
    ) -> RedisEmptyResult {
        self.run_command("SMOVE", vec![source_key, destination_key, member])
    }

    /// See redis [SREM](https://redis.io/commands/srem) command.
    pub fn srem(self: &mut Client, key: &str, member: &str) -> RedisEmptyResult {
        self.run_command("SREM", vec![key, member])
    }

    /// See redis [ZADD](https://redis.io/commands/zadd) command.
    pub fn zadd(self: &mut Client, key: &str, score: isize, member: &str) -> RedisResult<i32> {
        self.run_command("ZADD", vec![key, &score.to_string(), member])
    }

    /// See redis [ZRANGE](https://redis.io/commands/zrange) command.
    pub fn zrange(
        self: &mut Client,
        key: &str,
        start: isize,
        stop: isize,
    ) -> RedisResult<Vec<String>> {
        self.run_command::<Vec<String>>("ZRANGE", vec![key, &start.to_string(), &stop.to_string()])
    }
}