From c6e3e5eb2a19d18ca3a3d112cba44816831c17e5 Mon Sep 17 00:00:00 2001 From: Ephraim Feldblum Date: Thu, 31 Oct 2024 17:48:33 +0200 Subject: [PATCH 1/4] move `AddACLCategory` to `redismodule-rs` --- src/macros.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index bfbd3ace..33f37b74 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -49,12 +49,13 @@ macro_rules! redis_command { return $crate::raw::Status::Err as c_int; } - if unsafe { - $crate::raw::RedisModule_SetCommandACLCategories.unwrap()( - command, - acl_categories.as_ptr(), - ) - } == $crate::raw::Status::Err as c_int + if $crate::raw::RedisModule_SetCommandACLCategories.is_some() + && unsafe { + $crate::raw::RedisModule_SetCommandACLCategories.unwrap()( + command, + acl_categories.as_ptr(), + ) + } == $crate::raw::Status::Err as c_int { return $crate::raw::Status::Err as c_int; } @@ -127,6 +128,7 @@ macro_rules! redis_module { data_types: [ $($data_type:ident),* $(,)* ], + $(acl_category: $acl_category:expr,)* $(,)* $(init: $init_func:ident,)* $(,)* $(deinit: $deinit_func:ident,)* $(,)* $(info: $info_func:ident,)? @@ -262,6 +264,17 @@ macro_rules! redis_module { } )* + $( + let category = CString::new($acl_category).unwrap(); + if $crate::raw::RedisModule_SetCommandACLCategories.is_some() && unsafe { + raw::RedisModule_AddACLCategory.unwrap()(ctx, category.as_ptr()) + } == Status::Err as c_int + { + redis_log(ctx, "Error: failed to add ACL category"); + return Err("Error: failed to add ACL category"); + } + )* + $( $crate::redis_command!(ctx, $name, $command, $flags, $firstkey, $lastkey, $keystep, $acl_categories); )* From 49d9c878960122500ffc9b5c692a9627f42eb524 Mon Sep 17 00:00:00 2001 From: Ephraim Feldblum Date: Thu, 31 Oct 2024 17:52:26 +0200 Subject: [PATCH 2/4] typo --- src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 33f37b74..0c7f77a2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -266,7 +266,7 @@ macro_rules! redis_module { $( let category = CString::new($acl_category).unwrap(); - if $crate::raw::RedisModule_SetCommandACLCategories.is_some() && unsafe { + if $crate::raw::RedisModule_AddACLCategory.is_some() && unsafe { raw::RedisModule_AddACLCategory.unwrap()(ctx, category.as_ptr()) } == Status::Err as c_int { From 36491ceb9c1cc33c6915ed031b7ac1788bfe436d Mon Sep 17 00:00:00 2001 From: Ephraim Feldblum Date: Thu, 31 Oct 2024 19:26:23 +0200 Subject: [PATCH 3/4] deal with 5 year old todo --- src/macros.rs | 33 +++++++++++++++++++-------------- src/native_types.rs | 16 +++------------- src/raw.rs | 9 +++++++++ 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 0c7f77a2..939ce4ad 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -49,15 +49,21 @@ macro_rules! redis_command { return $crate::raw::Status::Err as c_int; } - if $crate::raw::RedisModule_SetCommandACLCategories.is_some() - && unsafe { - $crate::raw::RedisModule_SetCommandACLCategories.unwrap()( - command, - acl_categories.as_ptr(), - ) - } == $crate::raw::Status::Err as c_int + if let Some(RM_SetCommandACLCategories) = + $crate::raw::RedisModule_SetCommandACLCategories { - return $crate::raw::Status::Err as c_int; + if RM_SetCommandACLCategories(command, acl_categories.as_ptr()) + == $crate::raw::Status::Err as c_int + { + $crate::raw::redis_log( + $ctx, + &format!( + "Error: failed to set command {} ACL categories {}", + $command_name, $acl_categories + ), + ); + return $crate::raw::Status::Err as c_int; + } } } }}; @@ -266,12 +272,11 @@ macro_rules! redis_module { $( let category = CString::new($acl_category).unwrap(); - if $crate::raw::RedisModule_AddACLCategory.is_some() && unsafe { - raw::RedisModule_AddACLCategory.unwrap()(ctx, category.as_ptr()) - } == Status::Err as c_int - { - redis_log(ctx, "Error: failed to add ACL category"); - return Err("Error: failed to add ACL category"); + if let Some(RM_AddACLCategory) = raw::RedisModule_AddACLCategory { + if RM_AddACLCategory(ctx, category.as_ptr()) == raw::Status::Err as c_int { + raw::redis_log(ctx, "Error: failed to add ACL category"); + return raw::Status::Err as c_int; + } } )* diff --git a/src/native_types.rs b/src/native_types.rs index d1ce4953..d4ca9de0 100644 --- a/src/native_types.rs +++ b/src/native_types.rs @@ -34,7 +34,7 @@ impl RedisType { pub fn create_data_type(&self, ctx: *mut raw::RedisModuleCtx) -> Result<(), &str> { if self.name.len() != 9 { let msg = "Redis requires the length of native type names to be exactly 9 characters"; - redis_log(ctx, format!("{msg}, name is: '{}'", self.name).as_str()); + raw::redis_log(ctx, format!("{msg}, name is: '{}'", self.name).as_str()); return Err(msg); } @@ -50,13 +50,13 @@ impl RedisType { }; if redis_type.is_null() { - redis_log(ctx, "Error: created data type is null"); + raw::redis_log(ctx, "Error: created data type is null"); return Err("Error: created data type is null"); } *self.raw_type.borrow_mut() = redis_type; - redis_log( + raw::redis_log( ctx, format!("Created new data type '{}'", self.name).as_str(), ); @@ -64,13 +64,3 @@ impl RedisType { Ok(()) } } - -// TODO: Move to raw -#[allow(clippy::not_unsafe_ptr_arg_deref)] -pub fn redis_log(ctx: *mut raw::RedisModuleCtx, msg: &str) { - let level = CString::new("notice").unwrap(); // FIXME reuse this - let msg = CString::new(msg).unwrap(); - unsafe { - raw::RedisModule_Log.unwrap()(ctx, level.as_ptr(), msg.as_ptr()); - } -} diff --git a/src/raw.rs b/src/raw.rs index 7d779a01..10f992a9 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -952,3 +952,12 @@ impl From for Version { pub fn is_io_error(rdb: *mut RedisModuleIO) -> bool { unsafe { RedisModule_IsIOError.unwrap()(rdb) != 0 } } + +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub fn redis_log(ctx: *mut RedisModuleCtx, msg: &str) { + let level = CString::new("notice").unwrap(); // FIXME reuse this + let msg = CString::new(msg).unwrap(); + unsafe { + RedisModule_Log.unwrap()(ctx, level.as_ptr(), msg.as_ptr()); + } +} From 06bed66d85e4b80696881f87ec0701a67c6a8c49 Mon Sep 17 00:00:00 2001 From: Ephraim Feldblum Date: Sun, 3 Nov 2024 11:04:23 +0200 Subject: [PATCH 4/4] better error message --- src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 939ce4ad..36f553f3 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -274,7 +274,7 @@ macro_rules! redis_module { let category = CString::new($acl_category).unwrap(); if let Some(RM_AddACLCategory) = raw::RedisModule_AddACLCategory { if RM_AddACLCategory(ctx, category.as_ptr()) == raw::Status::Err as c_int { - raw::redis_log(ctx, "Error: failed to add ACL category"); + raw::redis_log(ctx, &format!("Error: failed to add ACL category {}", $acl_category)); return raw::Status::Err as c_int; } }