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
use crate::{
prepare_command,
resp::{
cmd, BulkString, CommandArgs, FromSingleValueArray, FromValue, IntoArgs,
SingleArgOrCollection,
},
PreparedCommand,
};
use std::hash::Hash;
/// A group of Redis commands related to [`Sets`](https://redis.io/docs/data-types/sets/)
/// # See Also
/// [Redis Set Commands](https://redis.io/commands/?group=set)
pub trait SetCommands {
/// Add the specified members to the set stored at key.
///
/// # See Also
/// [<https://redis.io/commands/sadd/>](https://redis.io/commands/sadd/)
#[must_use]
fn sadd<K, M, C>(&mut self, key: K, members: C) -> PreparedCommand<Self, usize>
where
Self: Sized,
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
prepare_command(self, cmd("SADD").arg(key).arg(members))
}
/// Returns the set cardinality (number of elements) of the set stored at key.
///
/// # Return
/// The cardinality (number of elements) of the set, or 0 if key does not exist.
///
/// # See Also
/// [<https://redis.io/commands/scard/>](https://redis.io/commands/scard/)
#[must_use]
fn scard<K>(&mut self, key: K) -> PreparedCommand<Self, usize>
where
Self: Sized,
K: Into<BulkString>,
{
prepare_command(self, cmd("SCARD").arg(key))
}
/// Returns the members of the set resulting from the difference
/// between the first set and all the successive sets.
///
/// # Return
/// A list with members of the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sdiff/>](https://redis.io/commands/sdiff/)
#[must_use]
fn sdiff<K, M, C, A>(&mut self, keys: C) -> PreparedCommand<Self, A>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue + Eq + Hash,
C: SingleArgOrCollection<K>,
A: FromSingleValueArray<M>,
{
prepare_command(self, cmd("SDIFF").arg(keys))
}
/// This command is equal to [sdiff](crate::SetCommands::sdiff), but instead of returning the resulting set,
/// it is stored in destination.
///
/// # Return
/// The number of elements in the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sdiffstore/>](https://redis.io/commands/sdiffstore/)
#[must_use]
fn sdiffstore<D, K, C>(&mut self, destination: D, keys: C) -> PreparedCommand<Self, usize>
where
Self: Sized,
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
prepare_command(self, cmd("SDIFFSTORE").arg(destination).arg(keys))
}
/// Returns the members of the set resulting from the intersection of all the given sets.
///
/// # Return
/// A list with members of the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sinter/>](https://redis.io/commands/sinter/)
#[must_use]
fn sinter<K, M, C, A>(&mut self, keys: C) -> PreparedCommand<Self, A>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue + Eq + Hash,
C: SingleArgOrCollection<K>,
A: FromSingleValueArray<M>,
{
prepare_command(self, cmd("SINTER").arg(keys))
}
/// This command is similar to [sinter](crate::SetCommands::sinter), but instead of returning the result set,
/// it returns just the cardinality of the result.
///
/// limit: if the intersection cardinality reaches limit partway through the computation,
/// the algorithm will exit and yield limit as the cardinality. 0 means unlimited
///
/// # Return
/// A list with members of the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sintercard/>](https://redis.io/commands/sintercard/)
#[must_use]
fn sintercard<K, C>(&mut self, keys: C, limit: usize) -> PreparedCommand<Self, usize>
where
Self: Sized,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
prepare_command(
self,
cmd("SINTERCARD")
.arg(keys.num_args())
.arg(keys)
.arg("LIMIT")
.arg(limit),
)
}
/// This command is equal to [sinter](crate::SetCommands::sinter), but instead of returning the resulting set,
/// it is stored in destination.
///
/// # Return
/// The number of elements in the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sinterstore/>](https://redis.io/commands/sinterstore/)
#[must_use]
fn sinterstore<D, K, C>(&mut self, destination: D, keys: C) -> PreparedCommand<Self, usize>
where
Self: Sized,
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
prepare_command(self, cmd("SINTERSTORE").arg(destination).arg(keys))
}
/// Returns if member is a member of the set stored at key.
///
/// # Return
/// * `true` - if the element is a member of the set.
/// * `false` - if the element is not a member of the set, or if key does not exist.
///
/// # See Also
/// [<https://redis.io/commands/sismember/>](https://redis.io/commands/sismember/)
#[must_use]
fn sismember<K, M>(&mut self, key: K, member: M) -> PreparedCommand<Self, bool>
where
Self: Sized,
K: Into<BulkString>,
M: Into<BulkString>,
{
prepare_command(self, cmd("SISMEMBER").arg(key).arg(member))
}
/// Returns all the members of the set value stored at key.
///
/// # See Also
/// [<https://redis.io/commands/smembers/>](https://redis.io/commands/smembers/)
#[must_use]
fn smembers<K, M, A>(&mut self, key: K) -> PreparedCommand<Self, A>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue + Eq + Hash,
A: FromSingleValueArray<M>,
{
prepare_command(self, cmd("SMEMBERS").arg(key))
}
/// Returns whether each member is a member of the set stored at key.
///
/// # Return
/// list representing the membership of the given elements, in the same order as they are requested.
///
/// # See Also
/// [<https://redis.io/commands/smismember/>](https://redis.io/commands/smismember/)
#[must_use]
fn smismember<K, M, C>(&mut self, key: K, members: C) -> PreparedCommand<Self, Vec<bool>>
where
Self: Sized,
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
prepare_command(self, cmd("SMISMEMBER").arg(key).arg(members))
}
/// Move member from the set at source to the set at destination.
///
/// # Return
/// * `true` - if the element is moved.
/// * `false` - if the element is not a member of source and no operation was performed.
///
/// # See Also
/// [<https://redis.io/commands/smove/>](https://redis.io/commands/smove/)
#[must_use]
fn smove<S, D, M>(
&mut self,
source: S,
destination: D,
member: M,
) -> PreparedCommand<Self, bool>
where
Self: Sized,
S: Into<BulkString>,
D: Into<BulkString>,
M: Into<BulkString>,
{
prepare_command(self, cmd("SMOVE").arg(source).arg(destination).arg(member))
}
/// Removes and returns one or more random members from the set value store at key.
///
/// # Return
/// the list of popped elements
///
/// # See Also
/// [<https://redis.io/commands/spop/>](https://redis.io/commands/spop/)
#[must_use]
fn spop<K, M, A>(&mut self, key: K, count: usize) -> PreparedCommand<Self, A>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue + Eq + Hash,
A: FromSingleValueArray<M>,
{
prepare_command(self, cmd("SPOP").arg(key).arg(count))
}
/// Removes and returns one or more random members from the set value store at key.
///
/// # Return
/// the list of popped elements
///
/// # See Also
/// [<https://redis.io/commands/srandmember/>](https://redis.io/commands/srandmember/)
#[must_use]
fn srandmember<K, M, A>(&mut self, key: K, count: usize) -> PreparedCommand<Self, A>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue + Eq + Hash,
A: FromSingleValueArray<M>,
{
prepare_command(self, cmd("SRANDMEMBER").arg(key).arg(count))
}
/// Remove the specified members from the set stored at key.
///
/// # Return
/// the number of members that were removed from the set, not including non existing members.
///
/// # See Also
/// [<https://redis.io/commands/srem/>](https://redis.io/commands/srem/)
#[must_use]
fn srem<K, M, C>(&mut self, key: K, members: C) -> PreparedCommand<Self, usize>
where
Self: Sized,
K: Into<BulkString>,
M: Into<BulkString>,
C: SingleArgOrCollection<M>,
{
prepare_command(self, cmd("SREM").arg(key).arg(members))
}
/// Iterates elements of Sets types.
///
/// # Return
/// a list of Set members.
///
/// # See Also
/// [<https://redis.io/commands/sscan/>](https://redis.io/commands/sscan/)
#[must_use]
fn sscan<K, M>(
&mut self,
key: K,
cursor: u64,
options: SScanOptions,
) -> PreparedCommand<Self, (u64, Vec<M>)>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue,
{
prepare_command(self, cmd("SSCAN").arg(key).arg(cursor).arg(options))
}
/// Returns the members of the set resulting from the union of all the given sets.
///
/// # Return
/// A list with members of the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sunion/>](https://redis.io/commands/sunion/)
#[must_use]
fn sunion<K, M, C, A>(&mut self, keys: C) -> PreparedCommand<Self, A>
where
Self: Sized,
K: Into<BulkString>,
M: FromValue + Eq + Hash,
C: SingleArgOrCollection<K>,
A: FromSingleValueArray<M>,
{
prepare_command(self, cmd("SUNION").arg(keys))
}
/// This command is equal to [sunion](crate::SetCommands::sunion), but instead of returning the resulting set,
/// it is stored in destination.
///
/// # Return
/// The number of elements in the resulting set.
///
/// # See Also
/// [<https://redis.io/commands/sunionstore/>](https://redis.io/commands/sunionstore/)
#[must_use]
fn sunionstore<D, K, C>(&mut self, destination: D, keys: C) -> PreparedCommand<Self, usize>
where
Self: Sized,
D: Into<BulkString>,
K: Into<BulkString>,
C: SingleArgOrCollection<K>,
{
prepare_command(self, cmd("SUNIONSTORE").arg(destination).arg(keys))
}
}
/// Options for the [`sscan`](crate::SetCommands::sscan) command
#[derive(Default)]
pub struct SScanOptions {
command_args: CommandArgs,
}
impl SScanOptions {
#[must_use]
pub fn match_pattern<P: Into<BulkString>>(self, match_pattern: P) -> Self {
Self {
command_args: self.command_args.arg("MATCH").arg(match_pattern),
}
}
#[must_use]
pub fn count(self, count: usize) -> Self {
Self {
command_args: self.command_args.arg("COUNT").arg(count),
}
}
}
impl IntoArgs for SScanOptions {
fn into_args(self, args: CommandArgs) -> CommandArgs {
args.arg(self.command_args)
}
}