1- macro_rules! error {
2- ( $message: expr) => {
3- Error :: generic( $message)
4- } ;
5- ( $message: expr, $( $arg: tt) * ) => {
6- Error :: generic( format!( $message, $( $arg) +) . as_str( ) )
7- }
8- }
1+ #[ macro_export]
2+ macro_rules! redis_command {
3+ ( $ctx: expr, $command_name: expr, $command_handler: expr, $command_flags: expr) => {
4+ {
5+ let name = CString :: new( $command_name) . unwrap( ) ;
6+ let flags = CString :: new( $command_flags) . unwrap( ) ;
7+ let ( firstkey, lastkey, keystep) = ( 1 , 1 , 1 ) ;
98
10- #[ allow( unused_macros) ]
11- macro_rules! log_debug {
12- ( $logger: expr, $target: expr) => {
13- if cfg!( debug_assertions) {
14- $logger. log_debug( $target)
15- }
16- } ;
17- ( $logger: expr, $target: expr, $( $arg: tt) * ) => {
18- if cfg!( debug_assertions) {
19- $logger. log_debug( format!( $target, $( $arg) +) . as_str( ) )
9+ /////////////////////
10+ extern fn do_command(
11+ ctx: * mut raw:: RedisModuleCtx ,
12+ argv: * mut * mut raw:: RedisModuleString ,
13+ argc: c_int,
14+ ) -> c_int {
15+ let context = Context :: new( ctx) ;
16+
17+ let args: Vec <String > = unsafe { slice:: from_raw_parts( argv, argc as usize ) }
18+ . into_iter( )
19+ . map( |a| RedisString :: from_ptr( * a) . expect( "UTF8 encoding error in handler args" ) . to_string( ) )
20+ . collect( ) ;
21+
22+ let response = $command_handler( & context, args) ;
23+ context. reply( response) as c_int
24+ }
25+ /////////////////////
26+
27+ if raw:: RedisModule_CreateCommand . unwrap( ) (
28+ $ctx,
29+ name. as_ptr( ) ,
30+ Some ( do_command) ,
31+ flags. as_ptr( ) ,
32+ firstkey, lastkey, keystep,
33+ ) == raw:: Status :: Err as _ { return raw:: Status :: Err as _; }
2034 }
2135 }
2236}
2337
2438#[ macro_export]
2539macro_rules! redis_module {
26- ( $module_name: expr, $module_version: expr, $data_types: expr, $commands: expr) => {
40+ (
41+ name: $module_name: expr,
42+ version: $module_version: expr,
43+ data_types: [
44+ $( $data_type: ident) ,* $( , ) *
45+ ] ,
46+ commands: [
47+ $( [
48+ $name: expr,
49+ $command: ident,
50+ $flags: expr
51+ ] ) ,* $( , ) *
52+ ] $( , ) *
53+ ) => {
2754 use std:: os:: raw:: c_int;
2855 use std:: ffi:: CString ;
56+ use std:: slice;
2957
3058 use redismodule:: raw;
59+ use redismodule:: RedisString ;
3160
3261 #[ no_mangle]
3362 #[ allow( non_snake_case) ]
@@ -47,31 +76,21 @@ macro_rules! redis_module {
4776 raw:: REDISMODULE_APIVER_1 as c_int,
4877 ) == raw:: Status :: Err as _ { return raw:: Status :: Err as _; }
4978
50- for data_type in & $data_types {
51- if data_type. create_data_type( ctx) . is_err( ) {
79+ $ (
80+ if ( & $ data_type) . create_data_type( ctx) . is_err( ) {
5281 return raw:: Status :: Err as _;
5382 }
54- }
83+ ) *
5584
5685 if true {
5786 redismodule:: alloc:: use_redis_alloc( ) ;
5887 } else {
5988 eprintln!( "*** NOT USING Redis allocator ***" ) ;
6089 }
6190
62- for command in & $commands {
63- let name = CString :: new( command. name) . unwrap( ) ;
64- let flags = CString :: new( command. flags) . unwrap( ) ;
65- let ( firstkey, lastkey, keystep) = ( 1 , 1 , 1 ) ;
66-
67- if raw:: RedisModule_CreateCommand . unwrap( ) (
68- ctx,
69- name. as_ptr( ) ,
70- command. wrap_handler( ) ,
71- flags. as_ptr( ) ,
72- firstkey, lastkey, keystep,
73- ) == raw:: Status :: Err as _ { return raw:: Status :: Err as _; }
74- }
91+ $(
92+ redis_command!( ctx, $name, $command, $flags) ;
93+ ) *
7594
7695 raw:: Status :: Ok as _
7796 }
0 commit comments