Skip to content

Commit

Permalink
style: re-fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
zitsen committed May 21, 2019
1 parent 3cd20d4 commit 5391c36
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 112 deletions.
15 changes: 6 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use UnQLite;
use error::Wrap;
use ffi::unqlite_config;
use libc::strlen;
use std::ffi::CString;
use std::mem;
use std::os::raw::c_char;
use std::ptr;
use vars::{UNQLITE_CONFIG_JX9_ERR_LOG, UNQLITE_CONFIG_DISABLE_AUTO_COMMIT, UNQLITE_CONFIG_ERR_LOG,
UNQLITE_CONFIG_GET_KV_NAME, UNQLITE_CONFIG_KV_ENGINE, UNQLITE_CONFIG_MAX_PAGE_CACHE};
use vars::{
UNQLITE_CONFIG_DISABLE_AUTO_COMMIT, UNQLITE_CONFIG_ERR_LOG, UNQLITE_CONFIG_GET_KV_NAME,
UNQLITE_CONFIG_JX9_ERR_LOG, UNQLITE_CONFIG_KV_ENGINE, UNQLITE_CONFIG_MAX_PAGE_CACHE,
};
use UnQLite;

/// A `Trait` for configuration.
///
Expand Down Expand Up @@ -90,12 +92,7 @@ impl Config for UnQLite {

fn kv_engine<S: Into<Vec<u8>>>(self, name: S) -> Self {
let name = CString::new(name).expect("KV engine error").into_raw();
wrap_raw!(
self,
config,
UNQLITE_CONFIG_KV_ENGINE,
name
).expect("config KV engine");
wrap_raw!(self, config, UNQLITE_CONFIG_KV_ENGINE, name).expect("config KV engine");
self
}

Expand Down
62 changes: 36 additions & 26 deletions src/document/doc_store.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
use super::vm_value::{to_value, Value};
use UnQLite;
use error::{Result, Wrap};
use ffi::{unqlite_array_add_strkey_elem, unqlite_compile, unqlite_compile_file, unqlite_value,
unqlite_value_bool, unqlite_value_double, unqlite_value_null, unqlite_value_string,
unqlite_vm, unqlite_vm_config, unqlite_vm_dump, unqlite_vm_exec,
unqlite_vm_extract_variable, unqlite_vm_new_array, unqlite_vm_new_scalar,
unqlite_vm_release, unqlite_vm_release_value, unqlite_vm_reset, unqlite_value_int64};
use ffi::{
unqlite_array_add_strkey_elem, unqlite_compile, unqlite_compile_file, unqlite_value,
unqlite_value_bool, unqlite_value_double, unqlite_value_int64, unqlite_value_null,
unqlite_value_string, unqlite_vm, unqlite_vm_config, unqlite_vm_dump, unqlite_vm_exec,
unqlite_vm_extract_variable, unqlite_vm_new_array, unqlite_vm_new_scalar, unqlite_vm_release,
unqlite_vm_release_value, unqlite_vm_reset,
};
use std::cell::RefCell;
use std::ffi::CString;
use std::os::raw::c_void;
use std::ptr::{null, null_mut, NonNull};
use std::rc::Rc;
use std::slice;
use std::sync::mpsc;
use vars::{UNQLITE_OK, UNQLITE_VM_CONFIG_ARGV_ENTRY, UNQLITE_VM_CONFIG_CREATE_VAR,
UNQLITE_VM_CONFIG_ENV_ATTR, UNQLITE_VM_CONFIG_ERR_REPORT, UNQLITE_VM_CONFIG_EXEC_VALUE,
UNQLITE_VM_CONFIG_EXTRACT_OUTPUT, UNQLITE_VM_CONFIG_IMPORT_PATH,
UNQLITE_VM_CONFIG_OUTPUT, UNQLITE_VM_CONFIG_RECURSION_DEPTH, UNQLITE_VM_OUTPUT_LENGTH};
use vars::{
UNQLITE_OK, UNQLITE_VM_CONFIG_ARGV_ENTRY, UNQLITE_VM_CONFIG_CREATE_VAR,
UNQLITE_VM_CONFIG_ENV_ATTR, UNQLITE_VM_CONFIG_ERR_REPORT, UNQLITE_VM_CONFIG_EXEC_VALUE,
UNQLITE_VM_CONFIG_EXTRACT_OUTPUT, UNQLITE_VM_CONFIG_IMPORT_PATH, UNQLITE_VM_CONFIG_OUTPUT,
UNQLITE_VM_CONFIG_RECURSION_DEPTH, UNQLITE_VM_OUTPUT_LENGTH,
};
use UnQLite;

/// Jx9 script compiler Interface.
///
Expand Down Expand Up @@ -120,7 +124,8 @@ impl UnQLiteVm {
UNQLITE_VM_CONFIG_OUTPUT,
callback_to_channel as OutputCallback,
sender_ptr
).map(|_| receiver)
)
.map(|_| receiver)
}

/// Redirect VM output to `stdout`.
Expand Down Expand Up @@ -148,7 +153,8 @@ impl UnQLiteVm {
UNQLITE_VM_CONFIG_EXTRACT_OUTPUT,
&ptr,
&mut len
).map(|_| unsafe { slice::from_raw_parts(ptr as *const u8, len as usize) })
)
.map(|_| unsafe { slice::from_raw_parts(ptr as *const u8, len as usize) })
}

/// Return the total number of bytes that have been outputted by the Virtual Machine
Expand Down Expand Up @@ -242,20 +248,24 @@ impl UnQLiteVm {
CString::new(x.as_bytes()).unwrap().as_ptr(),
x.len() as i32
)?,
Value::Array(array) => for x in array {
let raw_elem = self.new_variable(x)?;
wrap!(array_add_strkey_elem, raw, null_mut(), raw_elem.as_ptr())?;
},
Value::Object(map) => for (key, value) in map {
let raw_key = CString::new(key.as_bytes()).unwrap();
let raw_value = self.new_variable(value)?;
wrap!(
array_add_strkey_elem,
raw,
raw_key.as_ptr(),
raw_value.as_ptr()
)?;
},
Value::Array(array) => {
for x in array {
let raw_elem = self.new_variable(x)?;
wrap!(array_add_strkey_elem, raw, null_mut(), raw_elem.as_ptr())?;
}
}
Value::Object(map) => {
for (key, value) in map {
let raw_key = CString::new(key.as_bytes()).unwrap();
let raw_value = self.new_variable(value)?;
wrap!(
array_add_strkey_elem,
raw,
raw_key.as_ptr(),
raw_value.as_ptr()
)?;
}
}
}

Ok(RawValue::new(unsafe { self.as_raw_mut_ptr() }, raw))
Expand Down
53 changes: 27 additions & 26 deletions src/document/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use UnQLite;
use document::{Map, Value};
use document::Jx9;
use document::{Map, Value};
use std::thread;
use UnQLite;

// For view stdout of tests run:
// cargo test document -- --nocapture
Expand Down Expand Up @@ -157,31 +157,32 @@ fn exec_result() {
db.compile(
"return {'key_one': 123, 'key_2': 'string_value', 'key_3': [1,2,3],
'key_4': {'sub_key': 'sub_value', 'sub_key2': 42}, 'key_5': 1.2};",
).map(|mut vm| {
vm.exec()
.map(|opt| opt.unwrap())
.map(|result| {
let map: Option<Map> = result.into();
assert!(map.is_some());
if let Some(map) = map {
assert_eq!(5, map.len());
assert_eq!(Value::Int(123), map["key_one"]);
let s: Option<String> = map["key_2"].clone().into();
assert_eq!("string_value", s.unwrap());
assert_eq!(
Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
map["key_3"]
);
if let &Value::Object(ref sub_map) = &map["key_4"] {
assert_eq!(Value::Int(42), sub_map["sub_key2"]);
} else {
assert!(false);
}
)
.map(|mut vm| {
vm.exec()
.map(|opt| opt.unwrap())
.map(|result| {
let map: Option<Map> = result.into();
assert!(map.is_some());
if let Some(map) = map {
assert_eq!(5, map.len());
assert_eq!(Value::Int(123), map["key_one"]);
let s: Option<String> = map["key_2"].clone().into();
assert_eq!("string_value", s.unwrap());
assert_eq!(
Value::Array(vec![Value::Int(1), Value::Int(2), Value::Int(3)]),
map["key_3"]
);
if let &Value::Object(ref sub_map) = &map["key_4"] {
assert_eq!(Value::Int(42), sub_map["sub_key2"]);
} else {
assert!(false);
}
})
.unwrap()
})
.unwrap();
}
})
.unwrap()
})
.unwrap();
}

#[test]
Expand Down
20 changes: 12 additions & 8 deletions src/document/vm_value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use ffi::{unqlite_array_count, unqlite_array_walk, unqlite_value, unqlite_value_is_bool,
unqlite_value_is_float, unqlite_value_is_int, unqlite_value_is_json_array,
unqlite_value_is_json_object, unqlite_value_is_null, unqlite_value_is_string,
unqlite_value_to_bool, unqlite_value_to_double, unqlite_value_to_string,
unqlite_value_to_int64};
use ffi::{
unqlite_array_count, unqlite_array_walk, unqlite_value, unqlite_value_is_bool,
unqlite_value_is_float, unqlite_value_is_int, unqlite_value_is_json_array,
unqlite_value_is_json_object, unqlite_value_is_null, unqlite_value_is_string,
unqlite_value_to_bool, unqlite_value_to_double, unqlite_value_to_int64,
unqlite_value_to_string,
};
use std::collections::HashMap;
use std::os::raw::{c_int, c_void};
use std::slice;
Expand Down Expand Up @@ -37,7 +39,7 @@ impl Value {
}

macro_rules! declare_converter_to_ex {
($march_type: pat, $some_x: expr, $to_type_rs: ty) => (
($march_type: pat, $some_x: expr, $to_type_rs: ty) => {
impl From<Value> for Option<$to_type_rs> {
fn from(val: Value) -> Self {
match val {
Expand All @@ -46,11 +48,13 @@ macro_rules! declare_converter_to_ex {
}
}
}
)
};
}

macro_rules! declare_converter_to {
($type: path, $to: ty) => (declare_converter_to_ex!($type(x), x, $to);)
($type: path, $to: ty) => {
declare_converter_to_ex!($type(x), x, $to);
};
}

declare_converter_to_ex!(Value::Null, (), ());
Expand Down
6 changes: 4 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ pub enum ErrorKind {
READ_ONLY,
/// Locking protocol error
LOCKERR,
#[doc(hidden)] __Nonexhaustive,
#[doc(hidden)]
__Nonexhaustive,
}

impl From<i32> for ErrorKind {
Expand Down Expand Up @@ -173,7 +174,8 @@ impl Custom {
_ => Err(Custom {
kind: kind,
raw: result,
}.into()),
}
.into()),
}
}

Expand Down
38 changes: 21 additions & 17 deletions src/kv_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use UnQLite;
use error::{Result, Wrap};
use ffi::{unqlite, unqlite_kv_cursor, unqlite_kv_cursor_data, unqlite_kv_cursor_data_callback,
unqlite_kv_cursor_delete_entry, unqlite_kv_cursor_first_entry, unqlite_kv_cursor_init,
unqlite_kv_cursor_key, unqlite_kv_cursor_key_callback, unqlite_kv_cursor_last_entry,
unqlite_kv_cursor_next_entry, unqlite_kv_cursor_prev_entry, unqlite_kv_cursor_release,
unqlite_kv_cursor_reset, unqlite_kv_cursor_seek, unqlite_kv_cursor_valid_entry};
use ffi::{
unqlite, unqlite_kv_cursor, unqlite_kv_cursor_data, unqlite_kv_cursor_data_callback,
unqlite_kv_cursor_delete_entry, unqlite_kv_cursor_first_entry, unqlite_kv_cursor_init,
unqlite_kv_cursor_key, unqlite_kv_cursor_key_callback, unqlite_kv_cursor_last_entry,
unqlite_kv_cursor_next_entry, unqlite_kv_cursor_prev_entry, unqlite_kv_cursor_release,
unqlite_kv_cursor_reset, unqlite_kv_cursor_seek, unqlite_kv_cursor_valid_entry,
};
use std::mem;
use std::os::raw::c_void;
use std::ptr::{self, NonNull};
use vars::{UNQLITE_CURSOR_MATCH_EXACT, UNQLITE_CURSOR_MATCH_GE, UNQLITE_CURSOR_MATCH_LE};
use UnQLite;

/// Cursor iterator interfaces.
///
Expand Down Expand Up @@ -345,22 +347,24 @@ impl Drop for RawCursor {
#[cfg(feature = "enable-threads")]
mod tests {
use super::*;
use {UnQLite, KV};
use std::os::raw::c_void;
use std::ptr;
use {UnQLite, KV};

macro_rules! _test_assert_eq {
($lhs:expr, ($rhs_0:expr, $rhs_1:expr)) => {
{
let kv = $lhs;
assert_eq!(
(String::from_utf8(kv.0).unwrap(), String::from_utf8(kv.1).unwrap()),
($rhs_0.to_string(), $rhs_1.to_string()))
}
};
($lhs:expr, $rhs:expr) => (
($lhs:expr, ($rhs_0:expr, $rhs_1:expr)) => {{
let kv = $lhs;
assert_eq!(
(
String::from_utf8(kv.0).unwrap(),
String::from_utf8(kv.1).unwrap()
),
($rhs_0.to_string(), $rhs_1.to_string())
)
}};
($lhs:expr, $rhs:expr) => {
assert_eq!(String::from_utf8($lhs).unwrap(), $rhs.to_string())
);
};
}

#[no_mangle]
Expand Down
26 changes: 15 additions & 11 deletions src/kv_store.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
use UnQLite;
use error::{Result, Wrap};
use ffi::{unqlite_kv_append,
unqlite_kv_config,
unqlite_kv_delete,
// unqlite_kv_store_fmt,
// unqlite_kv_append_fmt,
unqlite_kv_fetch,
unqlite_kv_fetch_callback,
unqlite_kv_store};
use ffi::{
unqlite_kv_append,
unqlite_kv_config,
unqlite_kv_delete,
// unqlite_kv_store_fmt,
// unqlite_kv_append_fmt,
unqlite_kv_fetch,
unqlite_kv_fetch_callback,
unqlite_kv_store,
};
use std::mem;
use std::os::raw::c_void;
use std::ptr;
use vars::{UNQLITE_KV_CONFIG_CMP_FUNC, UNQLITE_KV_CONFIG_HASH_FUNC};
use UnQLite;

/// Key-Value Store Interface
pub trait KV {
Expand Down Expand Up @@ -129,7 +131,8 @@ impl KV for UnQLite {
key.len() as _,
ptr::null_mut(),
&mut len
).map(|_| len)
)
.map(|_| len)
}

fn kv_fetch<K: AsRef<[u8]>>(&self, key: K) -> Result<Vec<u8>> {
Expand All @@ -146,7 +149,8 @@ impl KV for UnQLite {
key.len() as _,
ptr as _,
&mut len
).map(|_| unsafe { Vec::from_raw_parts(ptr, len as usize, cap) })
)
.map(|_| unsafe { Vec::from_raw_parts(ptr, len as usize, cap) })
}

fn kv_fetch_callback<K: AsRef<[u8]>>(
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@

extern crate libc;

extern crate paste;
#[cfg(test)]
extern crate tempfile;
extern crate paste;

pub use error::{Error, Result};
use error::Wrap;
pub use error::{Error, Result};

use ffi::{unqlite_close, unqlite_open};
use std::ffi::CString;
Expand Down Expand Up @@ -276,14 +276,14 @@ pub mod ffi;
#[allow(dead_code)]
pub mod vars;

mod config;
pub mod document;
mod error;
mod kv_cursor;
mod kv_store;
mod openmode;
mod config;
mod util;
mod transaction;
mod kv_store;
mod kv_cursor;
pub mod document;
mod util;

pub use self::config::Config;
pub use self::kv_cursor::*;
Expand Down
Loading

0 comments on commit 5391c36

Please sign in to comment.