Skip to content

Commit be7fc5f

Browse files
committed
Simplify the info handler example
1 parent 87a128e commit be7fc5f

File tree

2 files changed

+6
-40
lines changed

2 files changed

+6
-40
lines changed

examples/info_handler_macro.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
use std::collections::HashMap;
22

33
use redis_module::InfoContext;
4-
use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString};
4+
use redis_module::{redis_module, RedisResult};
55
use redis_module_macros::info_command_handler;
66
use redis_module_macros::InfoSection;
77

8-
fn test_helper_version(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
9-
let ver = ctx.get_redis_version()?;
10-
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];
11-
12-
Ok(response.into())
13-
}
14-
15-
fn test_helper_version_rm_call(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
16-
let ver = ctx.get_redis_version_rm_call()?;
17-
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];
18-
19-
Ok(response.into())
20-
}
21-
22-
fn test_helper_command_name(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
23-
Ok(ctx.current_command_name()?.into())
24-
}
25-
26-
fn test_helper_err(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
27-
if args.len() < 1 {
28-
return Err(RedisError::WrongArity);
29-
}
30-
31-
let msg = args.get(1).unwrap();
32-
33-
ctx.reply_error_string(msg.try_as_str().unwrap());
34-
Ok(().into())
35-
}
36-
378
#[derive(Debug, Clone, InfoSection)]
389
struct InfoData {
3910
field: String,
@@ -54,14 +25,9 @@ fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
5425
//////////////////////////////////////////////////////
5526

5627
redis_module! {
57-
name: "test_helper",
28+
name: "info_handler_macro",
5829
version: 1,
5930
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
6031
data_types: [],
61-
commands: [
62-
["test_helper.version", test_helper_version, "", 0, 0, 0],
63-
["test_helper._version_rm_call", test_helper_version_rm_call, "", 0, 0, 0],
64-
["test_helper.name", test_helper_command_name, "", 0, 0, 0],
65-
["test_helper.err", test_helper_err, "", 0, 0, 0],
66-
],
32+
commands: [],
6733
}

tests/integration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ fn test_helper_info() -> Result<()> {
123123
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;
124124

125125
let res: String = redis::cmd("INFO")
126-
.arg("TEST_HELPER")
126+
.arg(module)
127127
.query(&mut con)
128-
.with_context(|| "failed to run INFO TEST_HELPER")?;
129-
assert!(res.contains("test_helper_field:test_helper_value"));
128+
.with_context(|| format!("failed to run INFO {module}"))?;
129+
assert!(res.contains(&format!("{module}_field:test_helper_value")));
130130
assert!(res.contains("dictionary:key=value"));
131131

132132
Ok(())

0 commit comments

Comments
 (0)