Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The PR introduce a new proc macro for command registrations. #326

Merged
merged 13 commits into from
May 15, 2023
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ crate-type = ["cdylib"]
name = "configuration"
crate-type = ["cdylib"]

[[example]]
name = "proc_macro_commands"
crate-type = ["cdylib"]

[[example]]
name = "acl"
crate-type = ["cdylib"]
Expand Down
66 changes: 66 additions & 0 deletions examples/proc_macro_commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use redis_module::{redis_module, Context, RedisResult, RedisString, RedisValue};
use redis_module_macros::command;

#[command(
{
flags: [ReadOnly],
arity: -2,
iddm marked this conversation as resolved.
Show resolved Hide resolved
key_spec: [
iddm marked this conversation as resolved.
Show resolved Hide resolved
{
notes: "test command that define all the arguments at even possition as keys",
flags: [ReadOnly, Access],
begin_search: Index({ index : 1 }),
find_keys: Range({ last_key :- 1, steps : 2, limit : 0 }),
}
]
}
)]
fn classic_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
iddm marked this conversation as resolved.
Show resolved Hide resolved
Ok(RedisValue::SimpleStringStatic("OK"))
}

#[command(
{
name: "keyword_keys",
flags: [ReadOnly],
arity: -2,
key_spec: [
{
notes: "test command that define all the arguments at even possition as keys",
flags: [ReadOnly, Access],
begin_search: Keyword({ keyword : "foo", startfrom : 1 }),
find_keys: Range({ last_key :- 1, steps : 2, limit : 0 }),
}
]
}
)]
fn keyword_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::SimpleStringStatic("OK"))
}

#[command(
{
name: "num_keys",
flags: [ReadOnly, NoMandatoryKeys],
arity: -2,
key_spec: [
{
notes: "test command that define all the arguments at even possition as keys",
flags: [ReadOnly, Access],
begin_search: Index({ index : 1 }),
find_keys: Keynum({ key_num_idx : 0, first_key : 1, key_step : 1 }),
}
]
}
)]
fn num_keys(_ctx: &Context, _args: Vec<RedisString>) -> RedisResult {
Ok(RedisValue::SimpleStringStatic("OK"))
}

redis_module! {
name: "server_events",
version: 1,
allocator: (redis_module::alloc::RedisAlloc, redis_module::alloc::RedisAlloc),
data_types: [],
commands: [],
}
3 changes: 3 additions & 0 deletions redismodule-rs-macros-internals/src/api_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ lazy_static::lazy_static! {
("RedisModule_BlockClientSetPrivateData".to_string(), 70200),
("RedisModule_BlockClientOnAuth".to_string(), 70200),
("RedisModule_ACLAddLogEntryByUserName".to_string(), 70200),
("RedisModule_GetCommand".to_string(), 70000),
("RedisModule_SetCommandInfo".to_string(), 70000),

]);

pub(crate) static ref API_OLDEST_VERSION: usize = 60000;
Expand Down
2 changes: 1 addition & 1 deletion redismodule-rs-macros-internals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Parse for Args {
/// );
/// ```
#[proc_macro]
pub fn redismodule_api(item: TokenStream) -> TokenStream {
pub fn api(item: TokenStream) -> TokenStream {
let args = parse_macro_input!(item as Args);
let minimum_require_version =
args.requested_apis
Expand Down
3 changes: 3 additions & 0 deletions redismodule-rs-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ categories = ["database", "api-bindings"]
[dependencies]
syn = { version="1.0", features = ["full"]}
quote = "1.0"
proc-macro2 = "1"
serde = { version = "1", features = ["derive"] }
serde_syn = "0.1.0"

[lib]
name = "redis_module_macros"
Expand Down
Loading