-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsubcmd.rs
More file actions
119 lines (110 loc) · 3.88 KB
/
subcmd.rs
File metadata and controls
119 lines (110 loc) · 3.88 KB
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
use std::collections::BTreeMap;
use valkey_module::redisvalue::ValkeyValueKey;
use valkey_module::{
alloc::ValkeyAlloc, valkey_module, Context, NextArg, ValkeyError, ValkeyResult, ValkeyString,
ValkeyValue,
};
// top level command doesn't have logic, simply acts as a wrapper for subcommands
fn cmd1(_ctx: &Context, args: Vec<ValkeyString>) -> ValkeyResult {
if args.len() == 1 {
// display help as default subcommand
return help_subcmd();
};
let mut args = args.into_iter().skip(1);
let subcmd = args.next_string()?;
let args: Vec<ValkeyString> = args.collect();
match subcmd.to_lowercase().as_str() {
"s1" => sub1(args),
// more subcommands can be added here
"info" => info_subcmd(args),
"help" => help_subcmd(),
_ => Err(ValkeyError::Str("invalid subcommand")),
}
}
// can be called either with `cmd1 help` or just `cmd1`
fn help_subcmd() -> ValkeyResult {
let output = vec![
ValkeyValue::SimpleString("cmd1 - top level command".into()),
ValkeyValue::SimpleString("cmd1 s1 - first level subcommand".into()),
ValkeyValue::SimpleString("cmd1 s1 s1 - second level command".into()),
ValkeyValue::SimpleString("cmd1 s1 s1 s1 - third level command".into()),
ValkeyValue::SimpleString("cmd1 help - display this message".into()),
];
Ok(output.into())
}
// custom info subcommand, can be called with `cmd1 info`
fn info_subcmd(args: Vec<ValkeyString>) -> ValkeyResult {
let section = args.into_iter().next_str().unwrap_or("all");
let sections = [
("key", ValkeyValue::SimpleString("value".into())),
("integer", ValkeyValue::Integer(1)),
("float", ValkeyValue::Float(1.1)),
("bool", ValkeyValue::Bool(true)),
(
"array",
ValkeyValue::Array(vec!["a", "b", "c"].into_iter().map(|s| s.into()).collect()),
),
(
"ordered-map",
ValkeyValue::OrderedMap(BTreeMap::from([
("key1".into(), "value1".into()),
("key2".into(), "value2".into()),
])),
),
(
"ordered-set",
ValkeyValue::OrderedSet(vec!["x", "y", "z"].into_iter().map(|s| s.into()).collect()),
),
// add more sections here as needed
];
// using BTreeMap to maintain order
let mut output: BTreeMap<ValkeyValueKey, ValkeyValue> = BTreeMap::new();
for (key, value) in sections {
if section == "all" || section == key {
output.insert(key.into(), value);
}
}
Ok(ValkeyValue::OrderedMap(output))
}
fn sub1(args: Vec<ValkeyString>) -> ValkeyResult {
if args.len() == 0 {
// return if no args for subcmd are passed in, additional bizlogic can be added here
return Ok("sub1".into());
};
let mut args = args.into_iter();
let subcmd = args.next_string()?;
let args: Vec<ValkeyString> = args.collect();
match subcmd.to_lowercase().as_str() {
"s1" => sub11(args),
// more subcommands can be added here
_ => Ok("sub1".into()),
}
}
fn sub11(args: Vec<ValkeyString>) -> ValkeyResult {
if args.len() == 0 {
// return if no args for subcmd are passed in, additional bizlogic can be added here
return Ok("sub11".into());
};
let mut args = args.into_iter();
let subcmd = args.next_string()?;
let args: Vec<ValkeyString> = args.collect();
match subcmd.to_lowercase().as_str() {
"s1" => sub111(args),
// more subcommands can be added here
_ => Ok("sub11".into()),
}
}
fn sub111(_args: Vec<ValkeyString>) -> ValkeyResult {
// add bizlogic here
Ok("sub111".into())
}
//////////////////////////////////////////////////////
valkey_module! {
name: "subcmd",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
commands: [
["cmd1", cmd1, "", 0, 0, 0],
],
}