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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
use crate::{
  commands,
  error::RedisError,
  interfaces::{ClientLike, RedisResult},
  types::{
    AggregateOptions,
    FromRedis,
    Limit,
    MultipleKeys,
    MultipleValues,
    MultipleWeights,
    MultipleZaddValues,
    Ordering,
    RedisKey,
    RedisValue,
    SetOptions,
    ZCmp,
    ZRange,
    ZSort,
  },
};
use std::convert::TryInto;

/// Functions that implement the [sorted sets](https://redis.io/commands#sorted_set) interface.
#[async_trait]
pub trait SortedSetsInterface: ClientLike + Sized {
  /// The blocking variant of [Self::zmpop].
  ///
  /// <https://redis.io/commands/bzmpop/>
  async fn bzmpop<R, K>(&self, timeout: f64, keys: K, sort: ZCmp, count: Option<i64>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
  {
    into!(keys);
    commands::sorted_sets::bzmpop(self, timeout, keys, sort, count)
      .await?
      .convert()
  }

  /// The blocking variant of [Self::zpopmin].
  ///
  /// <https://redis.io/commands/bzpopmin>
  async fn bzpopmin<R, K>(&self, keys: K, timeout: f64) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
  {
    into!(keys);
    commands::sorted_sets::bzpopmin(self, keys, timeout).await?.convert()
  }

  /// The blocking variant of [Self::zpopmax].
  ///
  /// <https://redis.io/commands/bzpopmax>
  async fn bzpopmax<R, K>(&self, keys: K, timeout: f64) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
  {
    into!(keys);
    commands::sorted_sets::bzpopmax(self, keys, timeout).await?.convert()
  }

  /// Adds all the specified members with the specified scores to the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zadd>
  async fn zadd<R, K, V>(
    &self,
    key: K,
    options: Option<SetOptions>,
    ordering: Option<Ordering>,
    changed: bool,
    incr: bool,
    values: V,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<MultipleZaddValues> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(values);
    commands::sorted_sets::zadd(self, key, options, ordering, changed, incr, values)
      .await?
      .convert()
  }

  /// Returns the sorted set cardinality (number of elements) of the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zcard>
  async fn zcard<R, K>(&self, key: K) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zcard(self, key).await?.convert()
  }

  /// Returns the number of elements in the sorted set at `key` with a score between `min` and `max`.
  ///
  /// <https://redis.io/commands/zcount>
  async fn zcount<R, K>(&self, key: K, min: f64, max: f64) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zcount(self, key, min, max).await?.convert()
  }

  /// This command is similar to ZDIFFSTORE, but instead of storing the resulting sorted set, it is returned to the
  /// client.
  ///
  /// <https://redis.io/commands/zdiff>
  async fn zdiff<R, K>(&self, keys: K, withscores: bool) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
  {
    into!(keys);
    commands::sorted_sets::zdiff(self, keys, withscores).await?.convert()
  }

  /// Computes the difference between the first and all successive input sorted sets and stores the result in
  /// `destination`.
  ///
  /// <https://redis.io/commands/zdiffstore>
  async fn zdiffstore<R, D, K>(&self, dest: D, keys: K) -> RedisResult<R>
  where
    R: FromRedis,
    D: Into<RedisKey> + Send,
    K: Into<MultipleKeys> + Send,
  {
    into!(dest, keys);
    commands::sorted_sets::zdiffstore(self, dest, keys).await?.convert()
  }

  /// Increments the score of `member` in the sorted set stored at `key` by `increment`.
  ///
  /// <https://redis.io/commands/zincrby>
  async fn zincrby<R, K, V>(&self, key: K, increment: f64, member: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<RedisValue> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(member);
    commands::sorted_sets::zincrby(self, key, increment, member)
      .await?
      .convert()
  }

  /// This command is similar to ZINTERSTORE, but instead of storing the resulting sorted set, it is returned to the
  /// client.
  ///
  /// <https://redis.io/commands/zinter>
  async fn zinter<R, K, W>(
    &self,
    keys: K,
    weights: W,
    aggregate: Option<AggregateOptions>,
    withscores: bool,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
    W: Into<MultipleWeights> + Send,
  {
    into!(keys, weights);
    commands::sorted_sets::zinter(self, keys, weights, aggregate, withscores)
      .await?
      .convert()
  }

  /// Computes the intersection of the sorted sets given by the specified keys, and stores the result in
  /// `destination`.
  ///
  /// <https://redis.io/commands/zinterstore>
  async fn zinterstore<R, D, K, W>(
    &self,
    dest: D,
    keys: K,
    weights: W,
    aggregate: Option<AggregateOptions>,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    D: Into<RedisKey> + Send,
    K: Into<MultipleKeys> + Send,
    W: Into<MultipleWeights> + Send,
  {
    into!(dest, keys, weights);
    commands::sorted_sets::zinterstore(self, dest, keys, weights, aggregate)
      .await?
      .convert()
  }

  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
  /// ordering, this command returns the number of elements in the sorted set at key with a value between min and
  /// max.
  ///
  /// <https://redis.io/commands/zlexcount>
  async fn zlexcount<R, K, M, N>(&self, key: K, min: M, max: N) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(min, max);
    commands::sorted_sets::zlexcount(self, key, min, max).await?.convert()
  }

  /// Removes and returns up to count members with the highest scores in the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zpopmax>
  async fn zpopmax<R, K>(&self, key: K, count: Option<usize>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zpopmax(self, key, count).await?.convert()
  }

  /// Removes and returns up to count members with the lowest scores in the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zpopmin>
  async fn zpopmin<R, K>(&self, key: K, count: Option<usize>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zpopmin(self, key, count).await?.convert()
  }

  /// Pops one or more elements, that are member-score pairs, from the first non-empty sorted set in the provided list
  /// of key names.
  ///
  /// <https://redis.io/commands/zmpop/>
  async fn zmpop<R, K>(&self, keys: K, sort: ZCmp, count: Option<i64>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<MultipleKeys> + Send,
  {
    into!(keys);
    commands::sorted_sets::zmpop(self, keys, sort, count).await?.convert()
  }

  /// When called with just the key argument, return a random element from the sorted set value stored at `key`.
  ///
  /// <https://redis.io/commands/zrandmember>
  async fn zrandmember<R, K>(&self, key: K, count: Option<(i64, bool)>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zrandmember(self, key, count).await?.convert()
  }

  /// This command is like ZRANGE, but stores the result in the `destination` key.
  ///
  /// <https://redis.io/commands/zrangestore>
  async fn zrangestore<R, D, S, M, N>(
    &self,
    dest: D,
    source: S,
    min: M,
    max: N,
    sort: Option<ZSort>,
    rev: bool,
    limit: Option<Limit>,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    D: Into<RedisKey> + Send,
    S: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(dest, source);
    try_into!(min, max);
    commands::sorted_sets::zrangestore(self, dest, source, min, max, sort, rev, limit)
      .await?
      .convert()
  }

  /// Returns the specified range of elements in the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zrange>
  async fn zrange<R, K, M, N>(
    &self,
    key: K,
    min: M,
    max: N,
    sort: Option<ZSort>,
    rev: bool,
    limit: Option<Limit>,
    withscores: bool,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(min, max);
    commands::sorted_sets::zrange(self, key, min, max, sort, rev, limit, withscores)
      .await?
      .convert()
  }

  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
  /// ordering, this command returns all the elements in the sorted set at `key` with a value between `min` and `max`.
  ///
  /// <https://redis.io/commands/zrangebylex>
  async fn zrangebylex<R, K, M, N>(&self, key: K, min: M, max: N, limit: Option<Limit>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(min, max);
    commands::sorted_sets::zrangebylex(self, key, min, max, limit)
      .await?
      .convert()
  }

  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
  /// ordering, this command returns all the elements in the sorted set at `key` with a value between `max` and `min`.
  ///
  /// <https://redis.io/commands/zrevrangebylex>
  async fn zrevrangebylex<R, K, M, N>(&self, key: K, max: M, min: N, limit: Option<Limit>) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(max, min);
    commands::sorted_sets::zrevrangebylex(self, key, max, min, limit)
      .await?
      .convert()
  }

  /// Returns all the elements in the sorted set at key with a score between `min` and `max` (including elements
  /// with score equal to `min` or `max`).
  ///
  /// <https://redis.io/commands/zrangebyscore>
  async fn zrangebyscore<R, K, M, N>(
    &self,
    key: K,
    min: M,
    max: N,
    withscores: bool,
    limit: Option<Limit>,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(min, max);
    commands::sorted_sets::zrangebyscore(self, key, min, max, withscores, limit)
      .await?
      .convert()
  }

  /// Returns all the elements in the sorted set at `key` with a score between `max` and `min` (including
  /// elements with score equal to `max` or `min`).
  ///
  /// <https://redis.io/commands/zrevrangebyscore>
  async fn zrevrangebyscore<R, K, M, N>(
    &self,
    key: K,
    max: M,
    min: N,
    withscores: bool,
    limit: Option<Limit>,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(max, min);
    commands::sorted_sets::zrevrangebyscore(self, key, max, min, withscores, limit)
      .await?
      .convert()
  }

  /// Returns the rank of member in the sorted set stored at `key`, with the scores ordered from low to high.
  ///
  /// <https://redis.io/commands/zrank>
  async fn zrank<R, K, V>(&self, key: K, member: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<RedisValue> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(member);
    commands::sorted_sets::zrank(self, key, member).await?.convert()
  }

  /// Removes the specified members from the sorted set stored at `key`. Non existing members are ignored.
  ///
  /// <https://redis.io/commands/zrem>
  async fn zrem<R, K, V>(&self, key: K, members: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<MultipleValues> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(members);
    commands::sorted_sets::zrem(self, key, members).await?.convert()
  }

  /// When all the elements in a sorted set are inserted with the same score, in order to force lexicographical
  /// ordering, this command removes all elements in the sorted set stored at `key` between the lexicographical range
  /// specified by `min` and `max`.
  ///
  /// <https://redis.io/commands/zremrangebylex>
  async fn zremrangebylex<R, K, M, N>(&self, key: K, min: M, max: N) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(min, max);
    commands::sorted_sets::zremrangebylex(self, key, min, max)
      .await?
      .convert()
  }

  /// Removes all elements in the sorted set stored at `key` with rank between `start` and `stop`.
  ///
  /// <https://redis.io/commands/zremrangebyrank>
  async fn zremrangebyrank<R, K>(&self, key: K, start: i64, stop: i64) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zremrangebyrank(self, key, start, stop)
      .await?
      .convert()
  }

  /// Removes all elements in the sorted set stored at `key` with a score between `min` and `max`.
  ///
  /// <https://redis.io/commands/zremrangebyscore>
  async fn zremrangebyscore<R, K, M, N>(&self, key: K, min: M, max: N) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    M: TryInto<ZRange> + Send,
    M::Error: Into<RedisError> + Send,
    N: TryInto<ZRange> + Send,
    N::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(min, max);
    commands::sorted_sets::zremrangebyscore(self, key, min, max)
      .await?
      .convert()
  }

  /// Returns the specified range of elements in the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zrevrange>
  async fn zrevrange<R, K>(&self, key: K, start: i64, stop: i64, withscores: bool) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
  {
    into!(key);
    commands::sorted_sets::zrevrange(self, key, start, stop, withscores)
      .await?
      .convert()
  }

  /// Returns the rank of `member` in the sorted set stored at `key`, with the scores ordered from high to low.
  ///
  /// <https://redis.io/commands/zrevrank>
  async fn zrevrank<R, K, V>(&self, key: K, member: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<RedisValue> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(member);
    commands::sorted_sets::zrevrank(self, key, member).await?.convert()
  }

  /// Returns the score of `member` in the sorted set at `key`.
  ///
  /// <https://redis.io/commands/zscore>
  async fn zscore<R, K, V>(&self, key: K, member: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<RedisValue> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(member);
    commands::sorted_sets::zscore(self, key, member).await?.convert()
  }

  /// This command is similar to ZUNIONSTORE, but instead of storing the resulting sorted set, it is returned to the
  /// client.
  ///
  /// <https://redis.io/commands/zunion>
  async fn zunion<K, W>(
    &self,
    keys: K,
    weights: W,
    aggregate: Option<AggregateOptions>,
    withscores: bool,
  ) -> RedisResult<RedisValue>
  where
    K: Into<MultipleKeys> + Send,
    W: Into<MultipleWeights> + Send,
  {
    into!(keys, weights);
    commands::sorted_sets::zunion(self, keys, weights, aggregate, withscores).await
  }

  /// Computes the union of the sorted sets given by the specified keys, and stores the result in `destination`.
  ///
  /// <https://redis.io/commands/zunionstore>
  async fn zunionstore<R, D, K, W>(
    &self,
    dest: D,
    keys: K,
    weights: W,
    aggregate: Option<AggregateOptions>,
  ) -> RedisResult<R>
  where
    R: FromRedis,
    D: Into<RedisKey> + Send,
    K: Into<MultipleKeys> + Send,
    W: Into<MultipleWeights> + Send,
  {
    into!(dest, keys, weights);
    commands::sorted_sets::zunionstore(self, dest, keys, weights, aggregate)
      .await?
      .convert()
  }

  /// Returns the scores associated with the specified members in the sorted set stored at `key`.
  ///
  /// <https://redis.io/commands/zmscore>
  async fn zmscore<R, K, V>(&self, key: K, members: V) -> RedisResult<R>
  where
    R: FromRedis,
    K: Into<RedisKey> + Send,
    V: TryInto<MultipleValues> + Send,
    V::Error: Into<RedisError> + Send,
  {
    into!(key);
    try_into!(members);
    commands::sorted_sets::zmscore(self, key, members).await?.convert()
  }
}