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

View File

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

View File

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