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
use crate::{
    client::{prepare_command, PreparedCommand},
    resp::{
        cmd, CommandArgs, MultipleArgsCollection, SingleArg, SingleArgCollection, ToArgs,
    },
};
/// A group of Redis commands related to [`Bitmaps`](https://redis.io/docs/data-types/bitmaps/)
/// & [`Bitfields`](https://redis.io/docs/data-types/bitfields/)
///
/// # See Also
/// [Redis Generic Commands](https://redis.io/commands/?group=bitmap)
pub trait BitmapCommands<'a> {
    /// Count the number of set bits (population counting) in a string.
    ///
    /// # Return
    /// The number of bits set to 1.
    ///
    /// # See Also
    /// [<https://redis.io/commands/bitcount/>](https://redis.io/commands/bitcount/)
    #[must_use]
    fn bitcount<K>(self, key: K, range: BitRange) -> PreparedCommand<'a, Self, usize>
    where
        Self: Sized,
        K: SingleArg,
    {
        prepare_command(self, cmd("BITCOUNT").arg(key).arg(range))
    }
    /// The command treats a Redis string as an array of bits,
    /// and is capable of addressing specific integer fields
    /// of varying bit widths and arbitrary non (necessary) aligned offset.
    ///
    /// # Return
    /// A collection with each entry being the corresponding result of the sub command
    /// given at the same position. OVERFLOW subcommands don't count as generating a reply.
    ///
    /// # See Also
    /// [<https://redis.io/commands/bitfield/>](https://redis.io/commands/bitfield/)
    #[must_use]
    fn bitfield<K, C, E, O>(self, key: K, sub_commands: C) -> PreparedCommand<'a, Self, Vec<u64>>
    where
        Self: Sized,
        K: SingleArg,
        E: SingleArg,
        O: SingleArg,
        C: MultipleArgsCollection<BitFieldSubCommand<E, O>>,
    {
        prepare_command(self, cmd("BITFIELD").arg(key).arg(sub_commands))
    }
    /// Read-only variant of the BITFIELD command.
    /// It is like the original BITFIELD but only accepts GET subcommand
    /// and can safely be used in read-only replicas.
    ///
    /// # Return
    /// A collection with each entry being the corresponding result of the sub command
    /// given at the same position.
    ///
    /// # See Also
    /// [<https://redis.io/commands/bitfield_ro/>](https://redis.io/commands/bitfield_ro/)
    #[must_use]
    fn bitfield_readonly<K, C, E, O>(
        self,
        key: K,
        get_commands: C,
    ) -> PreparedCommand<'a, Self, Vec<u64>>
    where
        Self: Sized,
        K: SingleArg,
        E: SingleArg,
        O: SingleArg,
        C: MultipleArgsCollection<BitFieldGetSubCommand<E, O>>,
    {
        prepare_command(self, cmd("BITFIELD_RO").arg(key).arg(get_commands))
    }
    /// Perform a bitwise operation between multiple keys (containing string values)
    /// and store the result in the destination key.
    ///
    /// # Return
    /// The size of the string stored in the destination key,
    /// that is equal to the size of the longest input string.
    ///
    /// # See Also
    /// [<https://redis.io/commands/bitop/>](https://redis.io/commands/bitop/)
    #[must_use]
    fn bitop<D, K, KK>(
        self,
        operation: BitOperation,
        dest_key: D,
        keys: KK,
    ) -> PreparedCommand<'a, Self, usize>
    where
        Self: Sized,
        D: SingleArg,
        K: SingleArg,
        KK: SingleArgCollection<K>,
    {
        prepare_command(self, cmd("BITOP").arg(operation).arg(dest_key).arg(keys))
    }
    /// Perform a bitwise operation between multiple keys (containing string values)
    /// and store the result in the destination key.
    ///
    /// # Return
    /// The position of the first bit set to 1 or 0 according to the request.
    ///
    /// # See Also
    /// [<https://redis.io/commands/bitpos/>](https://redis.io/commands/bitpos/)
    #[must_use]
    fn bitpos<K>(self, key: K, bit: u64, range: BitRange) -> PreparedCommand<'a, Self, usize>
    where
        Self: Sized,
        K: SingleArg,
    {
        prepare_command(self, cmd("BITPOS").arg(key).arg(bit).arg(range))
    }
    /// Returns the bit value at offset in the string value stored at key.
    ///
    /// # Return
    /// The bit value stored at offset.
    ///
    /// # See Also
    /// [<https://redis.io/commands/getbit/>](https://redis.io/commands/getbit/)
    #[must_use]
    fn getbit<K>(self, key: K, offset: u64) -> PreparedCommand<'a, Self, u64>
    where
        Self: Sized,
        K: SingleArg,
    {
        prepare_command(self, cmd("GETBIT").arg(key).arg(offset))
    }
    /// Sets or clears the bit at offset in the string value stored at key.
    ///
    /// # Return
    /// The original bit value stored at offset.
    ///
    /// # See Also
    /// [<https://redis.io/commands/setbit/>](https://redis.io/commands/setbit/)
    #[must_use]
    fn setbit<K>(self, key: K, offset: u64, value: u64) -> PreparedCommand<'a, Self, u64>
    where
        Self: Sized,
        K: SingleArg,
    {
        prepare_command(self, cmd("SETBIT").arg(key).arg(offset).arg(value))
    }
}
/// Interval options for the [`bitcount`](BitmapCommands::bitcount) command
#[derive(Default)]
pub struct BitRange {
    command_args: CommandArgs,
}
impl BitRange {
    #[must_use]
    pub fn range(start: isize, end: isize) -> Self {
        Self {
            command_args: CommandArgs::default().arg(start).arg(end).build(),
        }
    }
    /// Unit of the range, bit or byte
    #[must_use]
    pub fn unit(mut self, unit: BitUnit) -> Self {
        Self {
            command_args: self.command_args.arg(unit).build(),
        }
    }
}
impl ToArgs for BitRange {
    fn write_args(&self, args: &mut CommandArgs) {
        self.command_args.write_args(args);
    }
}
/// Unit of a [`range`](BitRange), bit or byte
pub enum BitUnit {
    Byte,
    Bit,
}
impl ToArgs for BitUnit {
    fn write_args(&self, args: &mut CommandArgs) {
        args.arg(match self {
            BitUnit::Byte => "BYTE",
            BitUnit::Bit => "BIT",
        });
    }
}
/// Sub-command for the [`bitfield`](BitmapCommands::bitfield) command
pub enum BitFieldSubCommand<E = &'static str, O = &'static str>
where
    E: SingleArg,
    O: SingleArg,
{
    Get(BitFieldGetSubCommand<E, O>),
    Set(E, O, u64),
    IncrBy(E, O, i64),
    Overflow(BitFieldOverflow),
}
impl<E, O> BitFieldSubCommand<E, O>
where
    E: SingleArg,
    O: SingleArg,
{
    /// Returns the specified bit field.
    #[must_use]
    pub fn get(encoding: E, offset: O) -> Self {
        Self::Get(BitFieldGetSubCommand::new(encoding, offset))
    }
    /// Set the specified bit field and returns its old value.
    #[must_use]
    pub fn set(encoding: E, offset: O, value: u64) -> Self {
        Self::Set(encoding, offset, value)
    }
    ///  Increments or decrements (if a negative increment is given)
    /// the specified bit field and returns the new value.
    #[must_use]
    pub fn incr_by(encoding: E, offset: O, increment: i64) -> Self {
        Self::IncrBy(encoding, offset, increment)
    }
    #[must_use]
    pub fn overflow(overflow: BitFieldOverflow) -> Self {
        Self::Overflow(overflow)
    }
}
impl<E, O> ToArgs for BitFieldSubCommand<E, O>
where
    E: SingleArg,
    O: SingleArg,
{
    fn write_args(&self, args: &mut CommandArgs) {
        match self {
            BitFieldSubCommand::Get(g) => args.arg_ref(g),
            BitFieldSubCommand::Set(encoding, offset, value) =>
                args.arg("SET").arg_ref(encoding).arg_ref(offset).arg(*value),
            BitFieldSubCommand::IncrBy(encoding, offset, increment) => args.arg("INCRBY").arg_ref(encoding).arg_ref(offset).arg(*increment),
            BitFieldSubCommand::Overflow(overflow) => args.arg("OVERFLOW").arg_ref(overflow),
        };
    }
}
/// Sub-command for the [`bitfield`](BitmapCommands::bitfield) command
pub struct BitFieldGetSubCommand<E = &'static str, O = &'static str>
where
    E: SingleArg,
    O: SingleArg,
{
    encoding: E,
    offset: O,
}
impl<E, O> BitFieldGetSubCommand<E, O>
where
    E: SingleArg,
    O: SingleArg,
{
    #[must_use]
    pub fn new(encoding: E, offset: O) -> Self {
        Self { encoding, offset }
    }
}
impl<E, O> ToArgs for BitFieldGetSubCommand<E, O>
where
    E: SingleArg,
    O: SingleArg,
{
    fn write_args(&self, args: &mut CommandArgs) {
        args.arg("GET").arg_ref(&self.encoding).arg_ref(&self.offset);
    }
}
/// Option for the [`BitFieldSubCommand`](BitFieldSubCommand) sub-command.
pub enum BitFieldOverflow {
    Wrap,
    Sat,
    Fail,
}
impl ToArgs for BitFieldOverflow {
    fn write_args(&self, args: &mut CommandArgs) {
        args.arg(match self {
            BitFieldOverflow::Wrap => "WRAP",
            BitFieldOverflow::Sat => "SAT",
            BitFieldOverflow::Fail => "FAIL",
        });
    }
}
/// Bit operation for the [`bitop`](BitmapCommands::bitop) command.
pub enum BitOperation {
    And,
    Or,
    Xor,
    Not,
}
impl ToArgs for BitOperation {
    fn write_args(&self, args: &mut CommandArgs) {
        args.arg(match self {
            BitOperation::And => "AND",
            BitOperation::Or => "OR",
            BitOperation::Xor => "XOR",
            BitOperation::Not => "NOT",
        });
    }
}