Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.

Commit fb73d5a

Browse files
author
Simone Mosciatti
committed
fix linter
1 parent 4189f97 commit fb73d5a

5 files changed

Lines changed: 185 additions & 181 deletions

File tree

redisql_lib/src/community_statement.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use sqlite::ffi;
1111

1212
use sqlite::StatementTrait;
1313
use sqlite::{SQLite3Error, Cursor, RawConnection, SQLiteOK};
14-
use sqlite::generate_sqlite3_error;
1514
use sqlite::SQLiteConnection;
1615

1716

@@ -108,7 +107,7 @@ pub fn generate_statements
108107
});
109108
}
110109
}
111-
_ => return Err(generate_sqlite3_error(conn.get_db())),
110+
_ => return Err(conn.get_last_error()),
112111
}
113112
}
114113
}
@@ -144,20 +143,19 @@ impl Statement {
144143
modified_rows: 0,
145144
})
146145
}
147-
_ => {
148-
Err(generate_sqlite3_error(
149-
unsafe { ffi::sqlite3_db_handle(self.stmt) },
150-
))
151-
}
146+
_ => Err(self.get_last_error()),
152147
}
153148
}
149+
fn get_last_error(&self) -> SQLite3Error {
150+
let db = unsafe { ffi::sqlite3_db_handle(self.stmt) };
151+
let rc = RawConnection::from_db_handler(db);
152+
rc.get_last_error()
153+
}
154154
}
155155

156156
impl<'a> StatementTrait<'a> for Statement {
157157
fn execute(&self) -> Result<Cursor, SQLite3Error> {
158-
Err(generate_sqlite3_error(
159-
unsafe { ffi::sqlite3_db_handle(self.stmt) },
160-
))
158+
Err(self.get_last_error())
161159
}
162160

163161
fn new(conn: Arc<Mutex<RawConnection>>,
@@ -178,7 +176,7 @@ impl<'a> StatementTrait<'a> for Statement {
178176
};
179177
match r {
180178
ffi::SQLITE_OK => Ok(Statement { stmt }),
181-
_ => Err(generate_sqlite3_error(conn.get_db())),
179+
_ => Err(conn.get_last_error()),
182180
}
183181
}
184182

@@ -220,10 +218,7 @@ impl<'a> StatementTrait<'a> for Statement {
220218
SQLITE_TRANSIENT())
221219
} {
222220
ffi::SQLITE_OK => Ok(SQLiteOK::OK),
223-
_ => {
224-
let db = unsafe { ffi::sqlite3_db_handle(self.stmt) };
225-
Err(generate_sqlite3_error(db))
226-
}
221+
_ => Err(self.get_last_error()),
227222
}
228223
}
229224

redisql_lib/src/redis.rs

Lines changed: 58 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
extern crate uuid;
22
extern crate fnv;
33

4-
use std::mem;
54
use std::ffi::{CString, CStr};
65
use std::string;
76
use std::fs::File;
87
use std::io::BufReader;
98

10-
use std::os::raw::c_char;
119
use std::os::raw::c_long;
1210

1311
use std::io::{Read, Write};
@@ -249,39 +247,37 @@ pub fn create_rm_string(ctx: *mut rm::ffi::RedisModuleCtx,
249247
*/
250248

251249
pub trait RedisReply {
252-
fn reply(&self, ctx: *mut rm::ffi::RedisModuleCtx) -> i32;
250+
fn reply(&self, ctx: rm::Context) -> i32;
253251
}
254252

255253
impl RedisReply for sql::Entity {
256-
fn reply(&self, ctx: *mut rm::ffi::RedisModuleCtx) -> i32 {
257-
unsafe {
258-
match *self {
259-
sql::Entity::Integer { int } => {
260-
rm::ReplyWithLongLong(ctx, int as i64)
261-
}
262-
sql::Entity::Float { float } => {
263-
rm::ReplyWithDouble(ctx, float)
264-
}
265-
sql::Entity::Text { ref text } => {
266-
rm::ReplyWithStringBuffer(ctx, text.as_bytes())
267-
}
268-
sql::Entity::Blob { ref blob } => {
269-
rm::ReplyWithStringBuffer(ctx, blob.as_bytes())
270-
}
271-
sql::Entity::Null => rm::ReplyWithNull(ctx),
272-
sql::Entity::OK { to_replicate } => {
273-
QueryResult::OK { to_replicate }.reply(ctx)
274-
}
275-
sql::Entity::DONE {
276-
modified_rows,
277-
to_replicate,
278-
} => {
279-
QueryResult::DONE {
280-
modified_rows,
281-
to_replicate,
282-
}
283-
.reply(ctx)
284-
}
254+
fn reply(&self, ctx: rm::Context) -> i32 {
255+
match *self {
256+
sql::Entity::Integer { int } => {
257+
rm::ReplyWithLongLong(ctx, i64::from(int))
258+
}
259+
sql::Entity::Float { float } => {
260+
rm::ReplyWithDouble(ctx, float)
261+
}
262+
sql::Entity::Text { ref text } => {
263+
rm::ReplyWithStringBuffer(ctx, text.as_bytes())
264+
}
265+
sql::Entity::Blob { ref blob } => {
266+
rm::ReplyWithStringBuffer(ctx, blob.as_bytes())
267+
}
268+
sql::Entity::Null => rm::ReplyWithNull(ctx),
269+
sql::Entity::OK { to_replicate } => {
270+
QueryResult::OK { to_replicate }.reply(ctx)
271+
}
272+
sql::Entity::DONE {
273+
modified_rows,
274+
to_replicate,
275+
} => {
276+
QueryResult::DONE {
277+
modified_rows,
278+
to_replicate,
279+
}
280+
.reply(ctx)
285281
}
286282
}
287283
}
@@ -318,23 +314,21 @@ fn reply_with_done(ctx: *mut rm::ffi::RedisModuleCtx,
318314
reply_with_simple_string(ctx, "DONE\0");
319315
unsafe {
320316
rm::ffi::RedisModule_ReplyWithLongLong
321-
.unwrap()(ctx, modified_rows as i64);
317+
.unwrap()(ctx, i64::from(modified_rows));
322318
}
323319
rm::ffi::REDISMODULE_OK
324320
}
325321

326-
fn reply_with_array(ctx: *mut rm::ffi::RedisModuleCtx,
327-
array: Vec<sql::Row>)
328-
-> i32 {
322+
fn reply_with_array(ctx: rm::Context, array: Vec<sql::Row>) -> i32 {
329323
let len = array.len() as c_long;
330324
unsafe {
331-
rm::ffi::RedisModule_ReplyWithArray.unwrap()(ctx, len);
325+
rm::ffi::RedisModule_ReplyWithArray.unwrap()(ctx.as_ptr(),
326+
len);
332327
}
333328
for row in array {
334329
unsafe {
335-
rm::ffi::RedisModule_ReplyWithArray.unwrap()(ctx,
336-
row.len() as
337-
c_long);
330+
rm::ffi::RedisModule_ReplyWithArray
331+
.unwrap()(ctx.as_ptr(), row.len() as c_long);
338332
}
339333
for entity in row {
340334
entity.reply(ctx);
@@ -345,16 +339,16 @@ fn reply_with_array(ctx: *mut rm::ffi::RedisModuleCtx,
345339

346340

347341
impl RedisReply for sql::SQLite3Error {
348-
fn reply(&self, ctx: *mut rm::ffi::RedisModuleCtx) -> i32 {
342+
fn reply(&self, ctx: Context) -> i32 {
349343
let error = format!("{}", self);
350-
reply_with_error(ctx, error)
344+
reply_with_error(ctx.as_ptr(), error)
351345
}
352346
}
353347

354348
impl RedisReply for RediSQLError {
355-
fn reply(&self, ctx: *mut rm::ffi::RedisModuleCtx) -> i32 {
349+
fn reply(&self, ctx: Context) -> i32 {
356350
let error = format!("{}", self);
357-
reply_with_error(ctx, error)
351+
reply_with_error(ctx.as_ptr(), error)
358352
}
359353
}
360354

@@ -466,11 +460,11 @@ pub enum QueryResult {
466460
}
467461

468462
impl QueryResult {
469-
pub fn reply(self, ctx: *mut rm::ffi::RedisModuleCtx) -> i32 {
463+
pub fn reply(self, ctx: rm::Context) -> i32 {
470464
match self {
471-
QueryResult::OK { .. } => reply_with_ok(ctx),
465+
QueryResult::OK { .. } => reply_with_ok(ctx.as_ptr()),
472466
QueryResult::DONE { modified_rows, .. } => {
473-
reply_with_done(ctx, modified_rows)
467+
reply_with_done(ctx.as_ptr(), modified_rows)
474468
}
475469
QueryResult::Array { array, .. } => {
476470
reply_with_array(ctx, array)
@@ -837,11 +831,11 @@ pub fn make_backup(conn1: &sql::RawConnection,
837831
match sql::create_backup(conn1, conn2) {
838832
Err(e) => Err(e),
839833
Ok(bk) => {
840-
let mut result = sql::backup_step(bk, 1);
834+
let mut result = unsafe { sql::BackupStep(&bk, 1) };
841835
while sql::backup_should_step_again(result) {
842-
result = sql::backup_step(bk, 1);
836+
result = unsafe { sql::BackupStep(&bk, 1) };
843837
}
844-
sql::backup_finish(bk);
838+
unsafe { sql::BackupFinish(&bk) };
845839
Ok(result)
846840
}
847841
}
@@ -850,15 +844,15 @@ pub fn make_backup(conn1: &sql::RawConnection,
850844
pub fn create_backup(conn: &sql::RawConnection,
851845
path: &str)
852846
-> Result<i32, sql::SQLite3Error> {
853-
match sql::open_connection(path) {
847+
match sql::RawConnection::open_connection(path) {
854848
Err(e) => Err(e),
855849
Ok(new_db) => make_backup(conn, &new_db),
856850
}
857851
}
858852

859-
pub fn write_file_to_rdb(f: File,
860-
rdb: *mut rm::ffi::RedisModuleIO)
861-
-> Result<(), std::io::Error> {
853+
pub unsafe fn write_file_to_rdb(f: File,
854+
rdb: *mut rm::ffi::RedisModuleIO)
855+
-> Result<(), std::io::Error> {
862856

863857
let block_size = 1024 * 4 as i64;
864858
let lenght = f.metadata().unwrap().len();
@@ -871,12 +865,8 @@ pub fn write_file_to_rdb(f: File,
871865
loop {
872866
let mut tw = to_write.clone();
873867
match buffer.read(tw.as_mut_slice()) {
874-
Ok(0) => {
875-
return Ok(());
876-
}
877-
Ok(_n) => {
878-
rm::SaveStringBuffer(rdb, tw.as_slice());
879-
}
868+
Ok(0) => return Ok(()),
869+
Ok(_n) => rm::SaveStringBuffer(rdb, tw.as_slice()),
880870
Err(e) => return Err(e),
881871
}
882872
}
@@ -897,25 +887,22 @@ impl Drop for SafeRedisModuleString {
897887
}
898888
}
899889

900-
pub fn write_rdb_to_file(f: &mut File,
901-
rdb: *mut rm::ffi::RedisModuleIO)
902-
-> Result<(), std::io::Error> {
890+
pub unsafe fn write_rdb_to_file(f: &mut File,
891+
rdb: *mut rm::ffi::RedisModuleIO)
892+
-> Result<(), std::io::Error> {
903893

904894
let blocks = rm::LoadSigned(rdb);
905895
for _ in 0..blocks {
906896
let mut dimension: usize = 0;
907897
let c_str_ptr = SafeRedisModuleString {
908-
ptr: unsafe {
909-
rm::ffi::RedisModule_LoadStringBuffer
910-
.unwrap()(rdb, &mut dimension)
911-
},
898+
ptr: rm::ffi::RedisModule_LoadStringBuffer
899+
.unwrap()(rdb, &mut dimension),
912900
};
913901
if dimension == 0 {
914902
break;
915903
}
916-
let slice = unsafe {
917-
slice::from_raw_parts(c_str_ptr.ptr as *mut u8, dimension)
918-
};
904+
let slice = slice::from_raw_parts(c_str_ptr.ptr as *mut u8,
905+
dimension);
919906
let y = f.write_all(slice);
920907
// mem::forget(slice);
921908
if let Err(e) = y {
@@ -930,7 +917,7 @@ pub fn get_dbkey_from_name(ctx: *mut rm::ffi::RedisModuleCtx,
930917
-> Result<Box<DBKey>, i32> {
931918
let context = Context::new(ctx);
932919
let key_name = rm::RMString::new(context, name);
933-
let key = OpenKey(context, key_name, rm::ffi::REDISMODULE_WRITE);
920+
let key = OpenKey(context, &key_name, rm::ffi::REDISMODULE_WRITE);
934921
let safe_key = RedisKey { key };
935922
let key_type = unsafe {
936923
rm::ffi::RedisModule_KeyType.unwrap()(safe_key.key)

redisql_lib/src/redis_type.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::ffi::CString;
88
#[allow(non_upper_case_globals)]
99
#[allow(improper_ctypes)]
1010
pub mod ffi {
11+
#![allow(clippy)]
1112
include!(concat!(env!("OUT_DIR"), "/bindings_redis.rs"));
1213
}
1314

@@ -101,7 +102,7 @@ pub fn ReplyWithError(ctx: Context, error: &str) -> i32 {
101102

102103
#[allow(non_snake_case)]
103104
pub fn OpenKey(ctx: Context,
104-
name: RMString,
105+
name: &RMString,
105106
mode: i32)
106107
-> *mut ffi::RedisModuleKey {
107108
unsafe {
@@ -119,51 +120,52 @@ pub fn LoadStringBuffer(rdb: *mut rm::ffi::RedisModuleIO,
119120
*/
120121

121122
#[allow(non_snake_case)]
122-
pub fn LoadSigned(rdb: *mut ffi::RedisModuleIO) -> i64 {
123-
unsafe { ffi::RedisModule_LoadSigned.unwrap()(rdb) as i64 }
123+
pub unsafe fn LoadSigned(rdb: *mut ffi::RedisModuleIO) -> i64 {
124+
ffi::RedisModule_LoadSigned.unwrap()(rdb) as i64
124125
}
125126

127+
126128
#[allow(non_snake_case)]
127-
pub fn SaveSigned(rdb: *mut ffi::RedisModuleIO, to_save: i64) {
128-
unsafe { ffi::RedisModule_SaveSigned.unwrap()(rdb, to_save) }
129+
pub unsafe fn SaveSigned(rdb: *mut ffi::RedisModuleIO,
130+
to_save: i64) {
131+
ffi::RedisModule_SaveSigned.unwrap()(rdb, to_save)
129132
}
130133

134+
131135
#[allow(non_snake_case)]
132-
pub fn SaveStringBuffer(rdb: *mut ffi::RedisModuleIO,
133-
buffer: &[u8]) {
136+
pub unsafe fn SaveStringBuffer(rdb: *mut ffi::RedisModuleIO,
137+
buffer: &[u8]) {
134138
let ptr = buffer.as_ptr() as *const c_char;
135139
let len = buffer.len();
136-
unsafe {
137-
ffi::RedisModule_SaveStringBuffer.unwrap()(rdb, ptr, len)
138-
}
140+
ffi::RedisModule_SaveStringBuffer.unwrap()(rdb, ptr, len)
139141
}
140142

141143
#[allow(non_snake_case)]
142-
pub fn ReplyWithNull(ctx: *mut ffi::RedisModuleCtx) -> i32 {
143-
unsafe { ffi::RedisModule_ReplyWithNull.unwrap()(ctx) }
144+
pub fn ReplyWithNull(ctx: Context) -> i32 {
145+
unsafe { ffi::RedisModule_ReplyWithNull.unwrap()(ctx.as_ptr()) }
144146
}
145147

146148
#[allow(non_snake_case)]
147-
pub fn ReplyWithLongLong(ctx: *mut ffi::RedisModuleCtx,
148-
ll: i64)
149-
-> i32 {
150-
unsafe { ffi::RedisModule_ReplyWithLongLong.unwrap()(ctx, ll) }
149+
pub fn ReplyWithLongLong(ctx: Context, ll: i64) -> i32 {
150+
unsafe {
151+
ffi::RedisModule_ReplyWithLongLong.unwrap()(ctx.as_ptr(), ll)
152+
}
151153
}
152154

153155
#[allow(non_snake_case)]
154-
pub fn ReplyWithDouble(ctx: *mut ffi::RedisModuleCtx,
155-
dd: f64)
156-
-> i32 {
157-
unsafe { ffi::RedisModule_ReplyWithDouble.unwrap()(ctx, dd) }
156+
pub fn ReplyWithDouble(ctx: Context, dd: f64) -> i32 {
157+
unsafe {
158+
ffi::RedisModule_ReplyWithDouble.unwrap()(ctx.as_ptr(), dd)
159+
}
158160
}
159161

160162
#[allow(non_snake_case)]
161-
pub fn ReplyWithStringBuffer(ctx: *mut ffi::RedisModuleCtx,
162-
buffer: &[u8])
163-
-> i32 {
163+
pub fn ReplyWithStringBuffer(ctx: Context, buffer: &[u8]) -> i32 {
164164
let ptr = buffer.as_ptr() as *const c_char;
165165
let len = buffer.len();
166166
unsafe {
167-
ffi::RedisModule_ReplyWithStringBuffer.unwrap()(ctx, ptr, len)
167+
ffi::RedisModule_ReplyWithStringBuffer.unwrap()(ctx.as_ptr(),
168+
ptr,
169+
len)
168170
}
169171
}

0 commit comments

Comments
 (0)