Skip to content

Commit

Permalink
MOD-8036 move AddACLCategory to redismodule-rs (#394)
Browse files Browse the repository at this point in the history
* move `AddACLCategory` to `redismodule-rs`

* typo

* deal with 5 year old todo

* better error message
  • Loading branch information
ephraimfeldblum authored Nov 3, 2024
1 parent 34d9ec8 commit ef0ad59
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
32 changes: 25 additions & 7 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ 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 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;
}
}
}
}};
Expand Down Expand Up @@ -127,6 +134,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,)?
Expand Down Expand Up @@ -262,6 +270,16 @@ 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, &format!("Error: failed to add ACL category {}", $acl_category));
return raw::Status::Err as c_int;
}
}
)*

$(
$crate::redis_command!(ctx, $name, $command, $flags, $firstkey, $lastkey, $keystep, $acl_categories);
)*
Expand Down
16 changes: 3 additions & 13 deletions src/native_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -50,27 +50,17 @@ 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(),
);

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());
}
}
9 changes: 9 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,12 @@ impl From<c_int> 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());
}
}

0 comments on commit ef0ad59

Please sign in to comment.