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
use crate::types::{RedisKey, RedisValue};
use std::{collections::VecDeque, iter::FromIterator};

/// Convenience struct for commands that take 1 or more keys.
///
/// **Note: this can also be used to represent an empty array of keys by passing `None` to any function that takes
/// `Into<MultipleKeys>`.** This is mostly useful for `EVAL` and `EVALSHA`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MultipleKeys {
  pub(crate) keys: Vec<RedisKey>,
}

impl MultipleKeys {
  pub fn new() -> MultipleKeys {
    MultipleKeys { keys: Vec::new() }
  }

  pub fn inner(self) -> Vec<RedisKey> {
    self.keys
  }

  pub fn into_values(self) -> Vec<RedisValue> {
    self.keys.into_iter().map(|k| k.into()).collect()
  }

  pub fn len(&self) -> usize {
    self.keys.len()
  }
}

impl From<Option<RedisKey>> for MultipleKeys {
  fn from(key: Option<RedisKey>) -> Self {
    let keys = if let Some(key) = key { vec![key] } else { vec![] };
    MultipleKeys { keys }
  }
}

impl<T> From<T> for MultipleKeys
where
  T: Into<RedisKey>,
{
  fn from(d: T) -> Self {
    MultipleKeys { keys: vec![d.into()] }
  }
}

impl<T> FromIterator<T> for MultipleKeys
where
  T: Into<RedisKey>,
{
  fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
    MultipleKeys {
      keys: iter.into_iter().map(|k| k.into()).collect(),
    }
  }
}

impl<'a, K, const N: usize> From<&'a [K; N]> for MultipleKeys
where
  K: Into<RedisKey> + Clone,
{
  fn from(value: &'a [K; N]) -> Self {
    MultipleKeys {
      keys: value.iter().map(|k| k.clone().into()).collect(),
    }
  }
}

impl<T> From<Vec<T>> for MultipleKeys
where
  T: Into<RedisKey>,
{
  fn from(d: Vec<T>) -> Self {
    MultipleKeys {
      keys: d.into_iter().map(|k| k.into()).collect(),
    }
  }
}

impl<T> From<VecDeque<T>> for MultipleKeys
where
  T: Into<RedisKey>,
{
  fn from(d: VecDeque<T>) -> Self {
    MultipleKeys {
      keys: d.into_iter().map(|k| k.into()).collect(),
    }
  }
}

impl From<()> for MultipleKeys {
  fn from(_: ()) -> Self {
    MultipleKeys { keys: Vec::new() }
  }
}

/// Convenience interface for commands that take 1 or more strings.
pub type MultipleStrings = MultipleKeys;

/// Convenience interface for commands that take 1 or more values.
pub type MultipleValues = RedisValue;

/// A convenience struct for functions that take one or more hash slot values.
pub struct MultipleHashSlots {
  inner: Vec<u16>,
}

impl MultipleHashSlots {
  pub fn inner(self) -> Vec<u16> {
    self.inner
  }

  pub fn len(&self) -> usize {
    self.inner.len()
  }
}

impl From<u16> for MultipleHashSlots {
  fn from(d: u16) -> Self {
    MultipleHashSlots { inner: vec![d] }
  }
}

impl From<Vec<u16>> for MultipleHashSlots {
  fn from(d: Vec<u16>) -> Self {
    MultipleHashSlots { inner: d }
  }
}

impl<'a> From<&'a [u16]> for MultipleHashSlots {
  fn from(d: &'a [u16]) -> Self {
    MultipleHashSlots { inner: d.to_vec() }
  }
}

impl From<VecDeque<u16>> for MultipleHashSlots {
  fn from(d: VecDeque<u16>) -> Self {
    MultipleHashSlots {
      inner: d.into_iter().collect(),
    }
  }
}

impl FromIterator<u16> for MultipleHashSlots {
  fn from_iter<I: IntoIterator<Item = u16>>(iter: I) -> Self {
    MultipleHashSlots {
      inner: iter.into_iter().collect(),
    }
  }
}