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
use crate::utils;
use bytes_utils::Str;

#[cfg(feature = "i-tracking")]
use crate::{
  error::{RedisError, RedisErrorKind},
  types::{Message, RedisKey, RedisValue, Server},
};

/// The type of clients to close.
///
/// <https://redis.io/commands/client-kill>
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ClientKillType {
  Normal,
  Master,
  Replica,
  Pubsub,
}

impl ClientKillType {
  pub(crate) fn to_str(&self) -> Str {
    utils::static_str(match *self {
      ClientKillType::Normal => "normal",
      ClientKillType::Master => "master",
      ClientKillType::Replica => "replica",
      ClientKillType::Pubsub => "pubsub",
    })
  }
}

/// Filters provided to the CLIENT KILL command.
///
/// <https://redis.io/commands/client-kill>
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ClientKillFilter {
  ID(String),
  Type(ClientKillType),
  User(String),
  Addr(String),
  LAddr(String),
  SkipMe(bool),
}

impl ClientKillFilter {
  pub(crate) fn to_str(&self) -> (Str, Str) {
    let (prefix, value) = match *self {
      ClientKillFilter::ID(ref id) => ("ID", id.into()),
      ClientKillFilter::Type(ref kind) => ("TYPE", kind.to_str()),
      ClientKillFilter::User(ref user) => ("USER", user.into()),
      ClientKillFilter::Addr(ref addr) => ("ADDR", addr.into()),
      ClientKillFilter::LAddr(ref addr) => ("LADDR", addr.into()),
      ClientKillFilter::SkipMe(ref b) => ("SKIPME", match *b {
        true => utils::static_str("yes"),
        false => utils::static_str("no"),
      }),
    };

    (utils::static_str(prefix), value)
  }
}

/// Filters for the CLIENT PAUSE command.
///
/// <https://redis.io/commands/client-pause>
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ClientPauseKind {
  Write,
  All,
}

impl ClientPauseKind {
  pub(crate) fn to_str(&self) -> Str {
    utils::static_str(match *self {
      ClientPauseKind::Write => "WRITE",
      ClientPauseKind::All => "ALL",
    })
  }
}

/// Arguments for the CLIENT REPLY command.
///
/// <https://redis.io/commands/client-reply>
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ClientReplyFlag {
  On,
  Off,
  Skip,
}

impl ClientReplyFlag {
  pub(crate) fn to_str(&self) -> Str {
    utils::static_str(match *self {
      ClientReplyFlag::On => "ON",
      ClientReplyFlag::Off => "OFF",
      ClientReplyFlag::Skip => "SKIP",
    })
  }
}

/// An `ON|OFF` flag used with client tracking commands.
#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Toggle {
  On,
  Off,
}

#[cfg(feature = "i-tracking")]
impl Toggle {
  pub(crate) fn to_str(&self) -> &'static str {
    match self {
      Toggle::On => "ON",
      Toggle::Off => "OFF",
    }
  }

  pub(crate) fn from_str(s: &str) -> Option<Self> {
    Some(match s {
      "ON" | "on" => Toggle::On,
      "OFF" | "off" => Toggle::Off,
      _ => return None,
    })
  }
}

#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
impl TryFrom<&str> for Toggle {
  type Error = RedisError;

  fn try_from(value: &str) -> Result<Self, Self::Error> {
    Toggle::from_str(value).ok_or(RedisError::new(RedisErrorKind::Parse, "Invalid toggle value."))
  }
}

#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
impl TryFrom<String> for Toggle {
  type Error = RedisError;

  fn try_from(value: String) -> Result<Self, Self::Error> {
    Toggle::from_str(&value).ok_or(RedisError::new(RedisErrorKind::Parse, "Invalid toggle value."))
  }
}

#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
impl TryFrom<&String> for Toggle {
  type Error = RedisError;

  fn try_from(value: &String) -> Result<Self, Self::Error> {
    Toggle::from_str(value).ok_or(RedisError::new(RedisErrorKind::Parse, "Invalid toggle value."))
  }
}

#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
impl From<bool> for Toggle {
  fn from(value: bool) -> Self {
    if value {
      Toggle::On
    } else {
      Toggle::Off
    }
  }
}

/// A [client tracking](https://redis.io/docs/manual/client-side-caching/) invalidation message from the provided server.
#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Invalidation {
  pub keys:   Vec<RedisKey>,
  pub server: Server,
}

#[cfg(feature = "i-tracking")]
#[cfg_attr(docsrs, doc(cfg(feature = "i-tracking")))]
impl Invalidation {
  pub(crate) fn from_message(message: Message, server: &Server) -> Option<Invalidation> {
    Some(Invalidation {
      keys:   match message.value {
        RedisValue::Array(values) => values.into_iter().filter_map(|v| v.try_into().ok()).collect(),
        RedisValue::String(s) => vec![s.into()],
        RedisValue::Bytes(b) => vec![b.into()],
        RedisValue::Double(f) => vec![f.into()],
        RedisValue::Integer(i) => vec![i.into()],
        RedisValue::Boolean(b) => vec![b.into()],
        RedisValue::Null => vec![],
        _ => {
          trace!("Dropping invalid invalidation message.");
          return None;
        },
      },
      server: server.clone(),
    })
  }
}