Skip to main content

redis_module_common/
lib.rs

1use serde::Deserialize;
2
3#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Default)]
4pub enum AclCategory {
5    #[default]
6    None,
7    Keyspace,
8    Read,
9    Write,
10    Set,
11    SortedSet,
12    List,
13    Hash,
14    String,
15    Bitmap,
16    HyperLogLog,
17    Geo,
18    Stream,
19    PubSub,
20    Admin,
21    Fast,
22    Slow,
23    Blocking,
24    Dangerous,
25    Connection,
26    Transaction,
27    Scripting,
28    Single(String),
29    Multi(Vec<AclCategory>),
30}
31
32impl From<Vec<AclCategory>> for AclCategory {
33    fn from(value: Vec<AclCategory>) -> Self {
34        AclCategory::Multi(value)
35    }
36}
37
38impl From<&str> for AclCategory {
39    fn from(value: &str) -> Self {
40        match value {
41            "" => AclCategory::None,
42            "keyspace" => AclCategory::Keyspace,
43            "read" => AclCategory::Read,
44            "write" => AclCategory::Write,
45            "set" => AclCategory::Set,
46            "sortedset" => AclCategory::SortedSet,
47            "list" => AclCategory::List,
48            "hash" => AclCategory::Hash,
49            "string" => AclCategory::String,
50            "bitmap" => AclCategory::Bitmap,
51            "hyperloglog" => AclCategory::HyperLogLog,
52            "geo" => AclCategory::Geo,
53            "stream" => AclCategory::Stream,
54            "pubsub" => AclCategory::PubSub,
55            "admin" => AclCategory::Admin,
56            "fast" => AclCategory::Fast,
57            "slow" => AclCategory::Slow,
58            "blocking" => AclCategory::Blocking,
59            "dangerous" => AclCategory::Dangerous,
60            "connection" => AclCategory::Connection,
61            "transaction" => AclCategory::Transaction,
62            "scripting" => AclCategory::Scripting,
63            _ if !value.contains(" ") => AclCategory::Single(value.to_string()),
64            _ => AclCategory::Multi(value.split_whitespace().map(AclCategory::from).collect()),
65        }
66    }
67}
68
69impl From<AclCategory> for String {
70    fn from(value: AclCategory) -> Self {
71        match value {
72            AclCategory::None => "".to_string(),
73            AclCategory::Keyspace => "keyspace".to_string(),
74            AclCategory::Read => "read".to_string(),
75            AclCategory::Write => "write".to_string(),
76            AclCategory::Set => "set".to_string(),
77            AclCategory::SortedSet => "sortedset".to_string(),
78            AclCategory::List => "list".to_string(),
79            AclCategory::Hash => "hash".to_string(),
80            AclCategory::String => "string".to_string(),
81            AclCategory::Bitmap => "bitmap".to_string(),
82            AclCategory::HyperLogLog => "hyperloglog".to_string(),
83            AclCategory::Geo => "geo".to_string(),
84            AclCategory::Stream => "stream".to_string(),
85            AclCategory::PubSub => "pubsub".to_string(),
86            AclCategory::Admin => "admin".to_string(),
87            AclCategory::Fast => "fast".to_string(),
88            AclCategory::Slow => "slow".to_string(),
89            AclCategory::Blocking => "blocking".to_string(),
90            AclCategory::Dangerous => "dangerous".to_string(),
91            AclCategory::Connection => "connection".to_string(),
92            AclCategory::Transaction => "transaction".to_string(),
93            AclCategory::Scripting => "scripting".to_string(),
94            AclCategory::Single(s) => s,
95            AclCategory::Multi(v) => v
96                .into_iter()
97                .map(String::from)
98                .collect::<Vec<_>>()
99                .join(" "),
100        }
101    }
102}
103
104impl std::fmt::Display for AclCategory {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        write!(f, "{}", String::from(self.clone()))
107    }
108}