From 5391c3643e77e59f2fe36039adc19d7791715654 Mon Sep 17 00:00:00 2001 From: Huo Linhe Date: Tue, 21 May 2019 15:12:31 +0800 Subject: [PATCH] style: re-fmt --- src/config.rs | 15 ++++------ src/document/doc_store.rs | 62 +++++++++++++++++++++++---------------- src/document/tests.rs | 53 +++++++++++++++++---------------- src/document/vm_value.rs | 20 ++++++++----- src/error.rs | 6 ++-- src/kv_cursor.rs | 38 +++++++++++++----------- src/kv_store.rs | 26 +++++++++------- src/lib.rs | 14 ++++----- src/transaction.rs | 2 +- src/util.rs | 13 ++++---- 10 files changed, 137 insertions(+), 112 deletions(-) diff --git a/src/config.rs b/src/config.rs index db81034f..6e81dbc4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,3 @@ -use UnQLite; use error::Wrap; use ffi::unqlite_config; use libc::strlen; @@ -6,8 +5,11 @@ 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. /// @@ -90,12 +92,7 @@ impl Config for UnQLite { fn kv_engine>>(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 } diff --git a/src/document/doc_store.rs b/src/document/doc_store.rs index bdb24ff2..35153ceb 100644 --- a/src/document/doc_store.rs +++ b/src/document/doc_store.rs @@ -1,11 +1,12 @@ 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; @@ -13,10 +14,13 @@ 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. /// @@ -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`. @@ -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 @@ -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)) diff --git a/src/document/tests.rs b/src/document/tests.rs index 0d3c6802..aeb36516 100644 --- a/src/document/tests.rs +++ b/src/document/tests.rs @@ -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 @@ -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 = 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 = 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 = 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 = 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] diff --git a/src/document/vm_value.rs b/src/document/vm_value.rs index 6dd25946..a2a541d9 100644 --- a/src/document/vm_value.rs +++ b/src/document/vm_value.rs @@ -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; @@ -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 for Option<$to_type_rs> { fn from(val: Value) -> Self { match val { @@ -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, (), ()); diff --git a/src/error.rs b/src/error.rs index 7507f1d2..f5a29644 100644 --- a/src/error.rs +++ b/src/error.rs @@ -106,7 +106,8 @@ pub enum ErrorKind { READ_ONLY, /// Locking protocol error LOCKERR, - #[doc(hidden)] __Nonexhaustive, + #[doc(hidden)] + __Nonexhaustive, } impl From for ErrorKind { @@ -173,7 +174,8 @@ impl Custom { _ => Err(Custom { kind: kind, raw: result, - }.into()), + } + .into()), } } diff --git a/src/kv_cursor.rs b/src/kv_cursor.rs index 4e670041..9357ed4f 100644 --- a/src/kv_cursor.rs +++ b/src/kv_cursor.rs @@ -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. /// @@ -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] diff --git a/src/kv_store.rs b/src/kv_store.rs index d1c84cb8..c7da173c 100644 --- a/src/kv_store.rs +++ b/src/kv_store.rs @@ -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 { @@ -129,7 +131,8 @@ impl KV for UnQLite { key.len() as _, ptr::null_mut(), &mut len - ).map(|_| len) + ) + .map(|_| len) } fn kv_fetch>(&self, key: K) -> Result> { @@ -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>( diff --git a/src/lib.rs b/src/lib.rs index 08827cf9..152a2c34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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::*; diff --git a/src/transaction.rs b/src/transaction.rs index 7b5de916..0c0fb40b 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -1,6 +1,6 @@ -use UnQLite; use error::{Result, Wrap}; use ffi::{unqlite_begin, unqlite_commit, unqlite_rollback}; +use UnQLite; /// Manual Transaction Manager /// diff --git a/src/util.rs b/src/util.rs index b70d958a..5aff5669 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,13 @@ -use UnQLite; use error::{Result, Wrap}; -use ffi::{unqlite_util_load_mmaped_file, unqlite_util_random_num, unqlite_util_random_string, - unqlite_util_release_mmaped_file}; +use ffi::{ + unqlite_util_load_mmaped_file, unqlite_util_random_num, unqlite_util_random_string, + unqlite_util_release_mmaped_file, +}; use std::ffi::CString; use std::mem; use std::os::raw::c_void; use std::path::Path; +use UnQLite; /// Utility interfaces. pub trait Util { @@ -28,8 +30,9 @@ impl Util for UnQLite { self.as_raw_mut_ptr(), vec.as_mut_ptr() as *mut i8, buf_size, - ).wrap() - .unwrap(); + ) + .wrap() + .unwrap(); vec } }