Rename success! to ok!

This commit is contained in:
Ivan Ukhov 2015-06-19 20:30:28 -04:00
parent e9f6fc0f1a
commit fa4ceac00a
3 changed files with 29 additions and 38 deletions

View File

@ -17,9 +17,9 @@ impl<'l> Database<'l> {
pub fn open<T: AsRef<Path>>(path: T) -> Result<Database<'l>> { pub fn open<T: AsRef<Path>>(path: T) -> Result<Database<'l>> {
let mut raw = 0 as *mut _; let mut raw = 0 as *mut _;
unsafe { unsafe {
success!(ffi::sqlite3_open_v2(path_to_c_str!(path.as_ref()), &mut raw, ok!(ffi::sqlite3_open_v2(path_to_c_str!(path.as_ref()), &mut raw,
ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE, ffi::SQLITE_OPEN_CREATE | ffi::SQLITE_OPEN_READWRITE,
0 as *const _)); 0 as *const _));
} }
Ok(Database { Ok(Database {
raw: raw, raw: raw,
@ -32,8 +32,8 @@ impl<'l> Database<'l> {
#[inline] #[inline]
pub fn execute(&self, sql: &str) -> Result<()> { pub fn execute(&self, sql: &str) -> Result<()> {
unsafe { unsafe {
success!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _, ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), None, 0 as *mut _,
0 as *mut _)); 0 as *mut _));
} }
Ok(()) Ok(())
} }
@ -48,10 +48,10 @@ impl<'l> Database<'l> {
{ {
unsafe { unsafe {
let callback = Box::new(callback); let callback = Box::new(callback);
success!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql), ok!(self.raw, ffi::sqlite3_exec(self.raw, str_to_c_str!(sql),
Some(process_callback::<F>), Some(process_callback::<F>),
&*callback as *const F as *mut F as *mut _, &*callback as *const F as *mut F as *mut _,
0 as *mut _)); 0 as *mut _));
} }
Ok(()) Ok(())
} }
@ -76,7 +76,7 @@ impl<'l> Database<'l> {
let result = ffi::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>), let result = ffi::sqlite3_busy_handler(self.raw, Some(busy_callback::<F>),
&*callback as *const F as *mut F as *mut _); &*callback as *const F as *mut F as *mut _);
self.busy_callback = Some(callback); self.busy_callback = Some(callback);
success!(self.raw, result); ok!(self.raw, result);
} }
Ok(()) Ok(())
} }
@ -85,7 +85,7 @@ impl<'l> Database<'l> {
/// rejected operations until a timeout expires. /// rejected operations until a timeout expires.
#[inline] #[inline]
pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> { pub fn set_busy_timeout(&mut self, milliseconds: usize) -> Result<()> {
unsafe { success!(self.raw, ffi::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) }; unsafe { ok!(self.raw, ffi::sqlite3_busy_timeout(self.raw, milliseconds as c_int)) };
Ok(()) Ok(())
} }
@ -93,7 +93,7 @@ impl<'l> Database<'l> {
#[inline] #[inline]
pub fn remove_busy_handler(&mut self) -> Result<()> { pub fn remove_busy_handler(&mut self) -> Result<()> {
::std::mem::replace(&mut self.busy_callback, None); ::std::mem::replace(&mut self.busy_callback, None);
unsafe { success!(self.raw, ffi::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) }; unsafe { ok!(self.raw, ffi::sqlite3_busy_handler(self.raw, None, 0 as *mut _)) };
Ok(()) Ok(())
} }
} }
@ -155,25 +155,21 @@ mod tests {
use super::Database; use super::Database;
use tests::setup; use tests::setup;
macro_rules! ok(
($result:expr) => ($result.unwrap());
);
#[test] #[test]
fn execute() { fn execute() {
let (path, _directory) = setup(); let (path, _directory) = setup();
let database = ok!(Database::open(&path)); let database = Database::open(&path).unwrap();
match database.execute(":)") { match database.execute(":)") {
Err(error) => assert_eq!(error.message, Err(error) => assert_eq!(error.message,
Some(String::from(r#"unrecognized token: ":""#))), Some(String::from(r#"unrecognized token: ":""#))),
_ => assert!(false), _ => unreachable!(),
} }
} }
#[test] #[test]
fn set_busy_handler() { fn set_busy_handler() {
let (path, _directory) = setup(); let (path, _directory) = setup();
let mut database = ok!(Database::open(&path)); let mut database = Database::open(&path).unwrap();
ok!(database.set_busy_handler(|_| true)); database.set_busy_handler(|_| true).unwrap();
} }
} }

View File

@ -30,7 +30,7 @@ macro_rules! raise(
($message:expr) => (return Err(::Error::from($message))); ($message:expr) => (return Err(::Error::from($message)));
); );
macro_rules! failure( macro_rules! error(
($database:expr, $code:expr) => ( ($database:expr, $code:expr) => (
match ::error::last($database) { match ::error::last($database) {
Some(error) => return Err(error), Some(error) => return Err(error),
@ -39,11 +39,11 @@ macro_rules! failure(
); );
); );
macro_rules! success( macro_rules! ok(
($database:expr, $result:expr) => ( ($database:expr, $result:expr) => (
match $result { match $result {
::ffi::SQLITE_OK => {}, ::ffi::SQLITE_OK => {},
code => failure!($database, code), code => error!($database, code),
} }
); );
($result:expr) => ( ($result:expr) => (
@ -108,12 +108,8 @@ mod tests {
use std::path::PathBuf; use std::path::PathBuf;
use temporary::Directory; use temporary::Directory;
macro_rules! ok(
($result:expr) => ($result.unwrap());
);
pub fn setup() -> (PathBuf, Directory) { pub fn setup() -> (PathBuf, Directory) {
let directory = ok!(Directory::new("sqlite")); let directory = Directory::new("sqlite").unwrap();
(directory.path().join("database.sqlite3"), directory) (directory.path().join("database.sqlite3"), directory)
} }
} }

View File

@ -57,14 +57,14 @@ impl<'l> Statement<'l> {
match unsafe { ffi::sqlite3_step(self.raw.0) } { match unsafe { ffi::sqlite3_step(self.raw.0) } {
ffi::SQLITE_DONE => Ok(State::Done), ffi::SQLITE_DONE => Ok(State::Done),
ffi::SQLITE_ROW => Ok(State::Row), ffi::SQLITE_ROW => Ok(State::Row),
code => failure!(self.raw.1, code), code => error!(self.raw.1, code),
} }
} }
/// Reset the statement. /// Reset the statement.
#[inline] #[inline]
pub fn reset(&mut self) -> Result<()> { pub fn reset(&mut self) -> Result<()> {
unsafe { success!(self.raw.1, ffi::sqlite3_reset(self.raw.0)) }; unsafe { ok!(self.raw.1, ffi::sqlite3_reset(self.raw.0)) };
Ok(()) Ok(())
} }
} }
@ -81,8 +81,8 @@ impl Parameter for f64 {
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> { fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
debug_assert!(i > 0, "the indexing starts from 1"); debug_assert!(i > 0, "the indexing starts from 1");
unsafe { unsafe {
success!(statement.raw.1, ffi::sqlite3_bind_double(statement.raw.0, i as c_int, ok!(statement.raw.1, ffi::sqlite3_bind_double(statement.raw.0, i as c_int,
*self as c_double)); *self as c_double));
} }
Ok(()) Ok(())
} }
@ -93,8 +93,8 @@ impl Parameter for i64 {
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> { fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
debug_assert!(i > 0, "the indexing starts from 1"); debug_assert!(i > 0, "the indexing starts from 1");
unsafe { unsafe {
success!(statement.raw.1, ffi::sqlite3_bind_int64(statement.raw.0, i as c_int, ok!(statement.raw.1, ffi::sqlite3_bind_int64(statement.raw.0, i as c_int,
*self as ffi::sqlite3_int64)); *self as ffi::sqlite3_int64));
} }
Ok(()) Ok(())
} }
@ -105,8 +105,8 @@ impl<'l> Parameter for &'l str {
fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> { fn bind(&self, statement: &mut Statement, i: usize) -> Result<()> {
debug_assert!(i > 0, "the indexing starts from 1"); debug_assert!(i > 0, "the indexing starts from 1");
unsafe { unsafe {
success!(statement.raw.1, ffi::sqlite3_bind_text(statement.raw.0, i as c_int, ok!(statement.raw.1, ffi::sqlite3_bind_text(statement.raw.0, i as c_int,
str_to_c_str!(*self), -1, None)); str_to_c_str!(*self), -1, None));
} }
Ok(()) Ok(())
} }
@ -143,8 +143,7 @@ impl Value for String {
pub fn new<'l>(raw1: *mut ffi::sqlite3, sql: &str) -> Result<Statement<'l>> { pub fn new<'l>(raw1: *mut ffi::sqlite3, sql: &str) -> Result<Statement<'l>> {
let mut raw0 = 0 as *mut _; let mut raw0 = 0 as *mut _;
unsafe { unsafe {
success!(raw1, ffi::sqlite3_prepare_v2(raw1, str_to_c_str!(sql), -1, &mut raw0, ok!(raw1, ffi::sqlite3_prepare_v2(raw1, str_to_c_str!(sql), -1, &mut raw0, 0 as *mut _));
0 as *mut _));
} }
Ok(Statement { raw: (raw0, raw1), phantom: PhantomData }) Ok(Statement { raw: (raw0, raw1), phantom: PhantomData })
} }