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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use crate::{
    resp::{
        cmd, BulkString, CommandArgs, FromSingleValueArray, FromValue, IntoArgs,
        SingleArgOrCollection,
    },
    CommandResult, PrepareCommand,
};

/// A group of Redis commands related to [`Lists`](https://redis.io/docs/data-types/lists/)
///
/// # See Also
/// [Redis List Commands](https://redis.io/commands/?group=list)
pub trait ListCommands<T>: PrepareCommand<T> {
    /// This command is the blocking variant of [`lmove`](crate::ListCommands::lmove).
    ///
    /// # Return
    /// the element being popped from `source` and pushed to `destination`. 
    /// If timeout is reached, a None reply is returned.
    ///
    /// # See Also
    /// [<https://redis.io/commands/blmove/>](https://redis.io/commands/blmove/)
    #[must_use]
    fn blmove<S, D, E>(
        &self,
        source: S,
        destination: D,
        where_from: LMoveWhere,
        where_to: LMoveWhere,
        timeout: f64,
    ) -> CommandResult<T, E>
    where
        S: Into<BulkString>,
        D: Into<BulkString>,
        E: FromValue,
    {
        self.prepare_command(
            cmd("BLMOVE")
                .arg(source)
                .arg(destination)
                .arg(where_from)
                .arg(where_to)
                .arg(timeout),
        )
    }

    /// This command is the blocking variant of [`lmpop`](crate::ListCommands::lmpop).
    ///
    /// # Return
    /// - None when no element could be popped, and timeout is reached.
    /// - Tuple composed by the name of the key from which elements were popped and the list of popped element
    ///
    /// # See Also
    /// [<https://redis.io/commands/blmpop/>](https://redis.io/commands/blmpop/)
    #[must_use]
    fn blmpop<K, E, C>(
        &self,
        timeout: f64,
        keys: C,
        where_: LMoveWhere,
        count: usize,
    ) -> CommandResult<T, Option<(String, Vec<E>)>>
    where
        K: Into<BulkString>,
        E: FromValue,
        C: SingleArgOrCollection<K>,
    {
        self.prepare_command(
            cmd("BLMPOP")
                .arg(timeout)
                .arg(keys.num_args())
                .arg(keys)
                .arg(where_)
                .arg("COUNT")
                .arg(count),
        )
    }

    /// This command is a blocking list pop primitive.
    ///
    /// It is the blocking version of [`lpop`](crate::ListCommands::lpop) because it
    /// blocks the connection when there are no elements to pop from any of the given lists.
    ///
    /// An element is popped from the head of the first list that is non-empty,
    /// with the given keys being checked in the order that they are given.
    ///
    /// # Return
    /// - `None` when no element could be popped and the timeout expired
    /// - a tuple with the first element being the name of the key where an element was popped
    /// and the second element being the value of the popped element.
    ///
    /// # See Also
    /// [<https://redis.io/commands/blpop/>](https://redis.io/commands/blpop/)
    #[must_use]
    fn blpop<K, KK, K1, V>(&self, keys: KK, timeout: f64) -> CommandResult<T, Option<(K1, V)>>
    where
        K: Into<BulkString>,
        KK: SingleArgOrCollection<K>,
        K1: FromValue,
        V: FromValue,
    {
        self.prepare_command(cmd("BLPOP").arg(keys).arg(timeout))
    }

    /// This command is a blocking list pop primitive.
    ///
    /// It is the blocking version of [`rpop`](crate::ListCommands::rpop) because it
    /// blocks the connection when there are no elements to pop from any of the given lists.
    ///
    /// An element is popped from the tail of the first list that is non-empty,
    /// with the given keys being checked in the order that they are given.
    ///
    /// # Return
    /// - `None` when no element could be popped and the timeout expired
    /// - a tuple with the first element being the name of the key where an element was popped
    /// and the second element being the value of the popped element.
    ///
    /// # See Also
    /// [<https://redis.io/commands/brpop/>](https://redis.io/commands/brpop/)
    #[must_use]
    fn brpop<K, KK, K1, V>(&self, keys: KK, timeout: f64) -> CommandResult<T, Option<(K1, V)>>
    where
        K: Into<BulkString>,
        KK: SingleArgOrCollection<K>,
        K1: FromValue,
        V: FromValue,
    {
        self.prepare_command(cmd("BRPOP").arg(keys).arg(timeout))
    }

    /// Returns the element at index index in the list stored at key.
    ///
    /// # Return
    /// The requested element, or nil when index is out of range.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lindex/>](https://redis.io/commands/lindex/)
    #[must_use]
    fn lindex<K, E>(&self, key: K, index: isize) -> CommandResult<T, E>
    where
        K: Into<BulkString>,
        E: FromValue,
    {
        self.prepare_command(cmd("LINDEX").arg(key).arg(index))
    }

    /// Inserts element in the list stored at key either before or after the reference value pivot.
    ///
    /// # Return
    /// The length of the list after the insert operation, or -1 when the value pivot was not found.
    ///
    /// # See Also
    /// [<https://redis.io/commands/linsert/>](https://redis.io/commands/linsert/)
    #[must_use]
    fn linsert<K, E>(
        &self,
        key: K,
        where_: LInsertWhere,
        pivot: E,
        element: E,
    ) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
    {
        self.prepare_command(cmd("LINSERT").arg(key).arg(where_).arg(pivot).arg(element))
    }

    /// Inserts element in the list stored at key either before or after the reference value pivot.
    ///
    /// # Return
    /// The length of the list at key.
    ///
    /// # See Also
    /// [<https://redis.io/commands/llen/>](https://redis.io/commands/llen/)
    #[must_use]
    fn llen<K>(&self, key: K) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
    {
        self.prepare_command(cmd("LLEN").arg(key))
    }

    /// Atomically returns and removes the first/last element (head/tail depending on the wherefrom argument)
    /// of the list stored at source, and pushes the element at the first/last element
    /// (head/tail depending on the whereto argument) of the list stored at destination.
    ///
    /// # Return
    /// The element being popped and pushed.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lmove/>](https://redis.io/commands/lmove/)
    #[must_use]
    fn lmove<S, D, E>(
        &self,
        source: S,
        destination: D,
        where_from: LMoveWhere,
        where_to: LMoveWhere,
    ) -> CommandResult<T, E>
    where
        S: Into<BulkString>,
        D: Into<BulkString>,
        E: FromValue,
    {
        self.prepare_command(
            cmd("LMOVE")
                .arg(source)
                .arg(destination)
                .arg(where_from)
                .arg(where_to),
        )
    }

    /// Pops one or more elements from the first non-empty list key from the list of provided key names.
    ///
    /// # Return
    /// Tuple composed by the name of the key from which elements were popped and the list of popped element
    ///
    /// # See Also
    /// [<https://redis.io/commands/lmpop/>](https://redis.io/commands/lmpop/)
    #[must_use]
    fn lmpop<K, E, C>(
        &self,
        keys: C,
        where_: LMoveWhere,
        count: usize,
    ) -> CommandResult<T, (String, Vec<E>)>
    where
        K: Into<BulkString>,
        E: FromValue,
        C: SingleArgOrCollection<K>,
    {
        self.prepare_command(
            cmd("LMPOP")
                .arg(keys.num_args())
                .arg(keys)
                .arg(where_)
                .arg("COUNT")
                .arg(count),
        )
    }

    /// Removes and returns the first elements of the list stored at key.
    ///
    /// # Return
    /// The list of popped elements, or empty collection when key does not exist.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lpop/>](https://redis.io/commands/lpop/)
    #[must_use]
    fn lpop<K, E, A>(&self, key: K, count: usize) -> CommandResult<T, A>
    where
        K: Into<BulkString>,
        E: FromValue,
        A: FromSingleValueArray<E>,
    {
        self.prepare_command(cmd("LPOP").arg(key).arg(count))
    }

    /// Returns the index of matching elements inside a Redis list.
    ///
    /// # Return
    /// The integer representing the matching element, or nil if there is no match.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lpos/>](https://redis.io/commands/lpos/)
    #[must_use]
    fn lpos<K, E>(
        &self,
        key: K,
        element: E,
        rank: Option<usize>,
        max_len: Option<usize>,
    ) -> CommandResult<T, Option<usize>>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
    {
        self.prepare_command(
            cmd("LPOS")
                .arg(key)
                .arg(element)
                .arg(rank.map(|r| ("RANK", r)))
                .arg(max_len.map(|l| ("MAXLEN", l))),
        )
    }

    /// Returns the index of matching elements inside a Redis list.
    ///
    /// # Return
    /// An array of integers representing the matching elements.
    /// (empty if there are no matches).
    ///
    /// # See Also
    /// [<https://redis.io/commands/lpos/>](https://redis.io/commands/lpos/)
    #[must_use]
    fn lpos_with_count<K, E, A>(
        &self,
        key: K,
        element: E,
        num_matches: usize,
        rank: Option<usize>,
        max_len: Option<usize>,
    ) -> CommandResult<T, A>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
        A: FromSingleValueArray<usize>,
    {
        self.prepare_command(
            cmd("LPOS")
                .arg(key)
                .arg(element)
                .arg(rank.map(|r| ("RANK", r)))
                .arg("COUNT")
                .arg(num_matches)
                .arg(max_len.map(|l| ("MAXLEN", l))),
        )
    }

    /// Insert all the specified values at the head of the list stored at key
    ///
    /// # Return
    /// The length of the list after the push operations.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lpush/>](https://redis.io/commands/lpush/)
    #[must_use]
    fn lpush<K, E, C>(&self, key: K, elements: C) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
        C: SingleArgOrCollection<E>,
    {
        self.prepare_command(cmd("LPUSH").arg(key).arg(elements))
    }

    /// Inserts specified values at the head of the list stored at key,
    /// only if key already exists and holds a list.
    ///
    /// # Return
    /// The length of the list after the push operation.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lpushx/>](https://redis.io/commands/lpushx/)
    #[must_use]
    fn lpushx<K, E, C>(&self, key: K, elements: C) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
        C: SingleArgOrCollection<E>,
    {
        self.prepare_command(cmd("LPUSHX").arg(key).arg(elements))
    }

    /// Returns the specified elements of the list stored at key.
    ///
    /// # Return
    /// The list of elements in the specified range.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lrange/>](https://redis.io/commands/lrange/)
    #[must_use]
    fn lrange<K, E, A>(&self, key: K, start: isize, stop: isize) -> CommandResult<T, A>
    where
        K: Into<BulkString>,
        E: FromValue,
        A: FromSingleValueArray<E>,
    {
        self.prepare_command(cmd("LRANGE").arg(key).arg(start).arg(stop))
    }

    /// Removes the first count occurrences of elements equal to element from the list stored at key.
    ///
    /// # Return
    /// The number of removed elements.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lrem/>](https://redis.io/commands/lrem/)
    #[must_use]
    fn lrem<K, E>(&self, key: K, count: isize, element: E) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
    {
        self.prepare_command(cmd("LREM").arg(key).arg(count).arg(element))
    }

    /// Sets the list element at index to element.
    ///
    /// # See Also
    /// [<https://redis.io/commands/lset/>](https://redis.io/commands/lset/)
    #[must_use]
    fn lset<K, E>(&self, key: K, index: isize, element: E) -> CommandResult<T, ()>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
    {
        self.prepare_command(cmd("LSET").arg(key).arg(index).arg(element))
    }

    /// Trim an existing list so that it will contain only the specified range of elements specified.
    ///
    /// # See Also
    /// [<https://redis.io/commands/ltrim/>](https://redis.io/commands/ltrim/)
    #[must_use]
    fn ltrim<K>(&self, key: K, start: isize, stop: isize) -> CommandResult<T, ()>
    where
        K: Into<BulkString>,
    {
        self.prepare_command(cmd("LTRIM").arg(key).arg(start).arg(stop))
    }

    /// Removes and returns the first elements of the list stored at key.
    ///
    /// # Return
    /// The list of popped elements, or empty collection when key does not exist.
    ///
    /// # See Also
    /// [<https://redis.io/commands/rpop/>](https://redis.io/commands/rpop/)
    #[must_use]
    fn rpop<K, E, C>(&self, key: K, count: usize) -> CommandResult<T, C>
    where
        K: Into<BulkString>,
        E: FromValue,
        C: FromSingleValueArray<E>,
    {
        self.prepare_command(cmd("RPOP").arg(key).arg(count))
    }

    /// Insert all the specified values at the tail of the list stored at key
    ///
    /// # Return
    /// The length of the list after the push operations.
    ///
    /// # See Also
    /// [<https://redis.io/commands/rpush/>](https://redis.io/commands/rpush/)
    #[must_use]
    fn rpush<K, E, C>(&self, key: K, elements: C) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
        C: SingleArgOrCollection<E>,
    {
        self.prepare_command(cmd("RPUSH").arg(key).arg(elements))
    }

    /// Inserts specified values at the tail of the list stored at key,
    /// only if key already exists and holds a list.
    ///
    /// # Return
    /// The length of the list after the push operations.
    ///
    /// # See Also
    /// [<https://redis.io/commands/rpushx/>](https://redis.io/commands/rpushx/)
    #[must_use]
    fn rpushx<K, E, C>(&self, key: K, elements: C) -> CommandResult<T, usize>
    where
        K: Into<BulkString>,
        E: Into<BulkString>,
        C: SingleArgOrCollection<E>,
    {
        self.prepare_command(cmd("RPUSHX").arg(key).arg(elements))
    }
}

pub enum LInsertWhere {
    Before,
    After,
}

impl IntoArgs for LInsertWhere {
    fn into_args(self, args: CommandArgs) -> CommandArgs {
        args.arg(match self {
            LInsertWhere::Before => BulkString::Str("BEFORE"),
            LInsertWhere::After => BulkString::Str("AFTER"),
        })
    }
}

pub enum LMoveWhere {
    Left,
    Right,
}

impl IntoArgs for LMoveWhere {
    fn into_args(self, args: CommandArgs) -> CommandArgs {
        args.arg(match self {
            LMoveWhere::Left => BulkString::Str("LEFT"),
            LMoveWhere::Right => BulkString::Str("RIGHT"),
        })
    }
}