Skip to content

Commit 307b7cb

Browse files
authored
add call (#24)
1 parent 13d3bb9 commit 307b7cb

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/context.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::raw;
77
use crate::LogLevel;
88
use crate::{RedisError, RedisResult, RedisString, RedisValue};
99

10+
const FMT: *const i8 = b"v\0".as_ptr() as *const i8;
11+
1012
/// Redis is a structure that's designed to give us a high-level interface to
1113
/// the Redis module API by abstracting away the raw C FFI calls.
1214
pub struct Context {
@@ -46,11 +48,25 @@ impl Context {
4648
.map(|s| RedisString::create(self.ctx, s))
4749
.collect();
4850

49-
let _ = terminated_args;
50-
let _ = command;
51-
let _ = args;
51+
let inner_args :Vec<*mut raw::RedisModuleString> = terminated_args.iter().map(|s| s.inner).collect();
52+
53+
let cmd = CString::new(command).unwrap();
54+
let reply: *mut raw::RedisModuleCallReply = unsafe {
55+
let p_call = raw::RedisModule_Call.unwrap();
56+
p_call(self.ctx, cmd.as_ptr(), FMT, inner_args.as_ptr() as *mut i8, terminated_args.len())
57+
};
58+
59+
let result = match raw::call_reply_type(reply) {
60+
raw::ReplyType::Unknown | raw::ReplyType::Error => {
61+
Err(RedisError::String(raw::call_reply_string(reply)))
62+
}
63+
_ => {
64+
Ok(RedisValue::SimpleStringStatic("OK"))
65+
}
66+
};
67+
raw::free_call_reply(reply);
68+
result
5269

53-
return Err(RedisError::Str("not implemented"));
5470
}
5571

5672
pub fn reply(&self, r: RedisResult) -> raw::Status {

src/raw.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern crate libc;
99
extern crate num_traits;
1010

1111
use libc::size_t;
12+
use std::slice;
13+
use std::ptr::null_mut;
1214
use num_traits::FromPrimitive;
1315

1416
pub use crate::redisraw::bindings::*;
@@ -159,6 +161,14 @@ pub fn call_reply_string_ptr(str: *mut RedisModuleCallReply, len: *mut size_t) -
159161
unsafe { RedisModule_CallReplyStringPtr.unwrap()(str, len) }
160162
}
161163

164+
pub fn call_reply_string(reply: *mut RedisModuleCallReply) -> String {
165+
unsafe {
166+
let len: *mut size_t = null_mut();
167+
let str: *mut u8 = RedisModule_CallReplyStringPtr.unwrap()(reply, len) as *mut u8;
168+
String::from_utf8(slice::from_raw_parts(str, *len).into_iter().map(|v| *v).collect()).unwrap()
169+
}
170+
}
171+
162172
pub fn close_key(kp: *mut RedisModuleKey) {
163173
unsafe { RedisModule_CloseKey.unwrap()(kp) }
164174
}

0 commit comments

Comments
 (0)