Skip to content

Commit

Permalink
Simplify the info handler example
Browse files Browse the repository at this point in the history
  • Loading branch information
iddm committed Jul 18, 2023
1 parent 87a128e commit be7fc5f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 40 deletions.
40 changes: 3 additions & 37 deletions examples/info_handler_macro.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,10 @@
use std::collections::HashMap;

use redis_module::InfoContext;
use redis_module::{redis_module, Context, RedisError, RedisResult, RedisString};
use redis_module::{redis_module, RedisResult};
use redis_module_macros::info_command_handler;
use redis_module_macros::InfoSection;

fn test_helper_version(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
let ver = ctx.get_redis_version()?;
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];

Ok(response.into())
}

fn test_helper_version_rm_call(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
let ver = ctx.get_redis_version_rm_call()?;
let response: Vec<i64> = vec![ver.major.into(), ver.minor.into(), ver.patch.into()];

Ok(response.into())
}

fn test_helper_command_name(ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(ctx.current_command_name()?.into())
}

fn test_helper_err(ctx: &Context, args: Vec<RedisString>) -> RedisResult {
if args.len() < 1 {
return Err(RedisError::WrongArity);
}

let msg = args.get(1).unwrap();

ctx.reply_error_string(msg.try_as_str().unwrap());
Ok(().into())
}

#[derive(Debug, Clone, InfoSection)]
struct InfoData {
field: String,
Expand All @@ -54,14 +25,9 @@ fn add_info(ctx: &InfoContext, _for_crash_report: bool) -> RedisResult<()> {
//////////////////////////////////////////////////////

redis_module! {
name: "test_helper",
name: "info_handler_macro",
version: 1,
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
data_types: [],
commands: [
["test_helper.version", test_helper_version, "", 0, 0, 0],
["test_helper._version_rm_call", test_helper_version_rm_call, "", 0, 0, 0],
["test_helper.name", test_helper_command_name, "", 0, 0, 0],
["test_helper.err", test_helper_err, "", 0, 0, 0],
],
commands: [],
}
6 changes: 3 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ fn test_helper_info() -> Result<()> {
get_redis_connection(port).with_context(|| "failed to connect to redis server")?;

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

Ok(())
Expand Down

0 comments on commit be7fc5f

Please sign in to comment.