forked from RedisLabsModules/redismodule-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproc_macro_commands.rs
136 lines (127 loc) · 3.77 KB
/
proc_macro_commands.rs
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
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use redis_module::RedisError;
use redis_module::{redis_module, Context, RedisResult, RedisString, RedisValue};
use redis_module_macros::{command, RedisValue};
#[derive(RedisValue)]
struct RedisValueDeriveInner {
i1: i64,
}
#[derive(RedisValue)]
struct RedisValueDerive {
i: i64,
f: f64,
s: String,
u: usize,
v: Vec<i64>,
#[RedisValueAttr{flatten: true}]
inner: RedisValueDeriveInner,
v2: Vec<RedisValueDeriveInner>,
hash_map: HashMap<String, String>,
hash_set: HashSet<String>,
ordered_map: BTreeMap<String, RedisValueDeriveInner>,
ordered_set: BTreeSet<String>,
}
#[derive(RedisValue)]
enum RedisValueEnum {
Str(String),
RedisValue(RedisValueDerive),
}
#[command(
{
flags: [ReadOnly, NoMandatoryKeys],
arity: -1,
key_spec: [
{
notes: "test redis value derive macro",
flags: [ReadOnly, Access],
begin_search: Index({ index : 0 }),
find_keys: Range({ last_key : 0, steps : 0, limit : 0 }),
}
]
}
)]
fn redis_value_derive(
_ctx: &Context,
args: Vec<RedisString>,
) -> Result<RedisValueEnum, RedisError> {
if args.len() > 1 {
Ok(RedisValueEnum::Str("OK".to_owned()))
} else {
Ok(RedisValueEnum::RedisValue(RedisValueDerive {
i: 10,
f: 1.1,
s: "s".to_owned(),
u: 20,
v: vec![1, 2, 3],
inner: RedisValueDeriveInner { i1: 1 },
v2: vec![
RedisValueDeriveInner { i1: 1 },
RedisValueDeriveInner { i1: 2 },
],
hash_map: HashMap::from([("key".to_owned(), "val".to_owned())]),
hash_set: HashSet::from(["key".to_owned()]),
ordered_map: BTreeMap::from([("key".to_owned(), RedisValueDeriveInner { i1: 10 })]),
ordered_set: BTreeSet::from(["key".to_owned()]),
}))
}
}
#[command(
{
flags: [ReadOnly],
arity: -2,
key_spec: [
{
notes: "test command that define all the arguments at even possition as keys",
flags: [ReadOnly, Access],
begin_search: Index({ index : 1 }),
find_keys: Range({ last_key :- 1, steps : 2, limit : 0 }),
}
]
}
)]
fn classic_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::SimpleStringStatic("OK"))
}
#[command(
{
name: "keyword_keys",
flags: [ReadOnly],
arity: -2,
key_spec: [
{
notes: "test command that define all the arguments at even possition as keys",
flags: [ReadOnly, Access],
begin_search: Keyword({ keyword : "foo", startfrom : 1 }),
find_keys: Range({ last_key :- 1, steps : 2, limit : 0 }),
}
]
}
)]
fn keyword_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::SimpleStringStatic("OK"))
}
#[command(
{
name: "num_keys",
flags: [ReadOnly, NoMandatoryKeys],
arity: -2,
key_spec: [
{
notes: "test command that define all the arguments at even possition as keys",
flags: [ReadOnly, Access],
begin_search: Index({ index : 1 }),
find_keys: Keynum({ key_num_idx : 0, first_key : 1, key_step : 1 }),
}
]
}
)]
fn num_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::SimpleStringStatic("OK"))
}
redis_module! {
name: "server_events",
version: 1,
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
data_types: [],
commands: [],
}