put LmdbError on a diet

without.crypto
Dan Burkert 10 years ago
parent 8cdc8bef22
commit 31243bf9ab
  1. 97
      src/error.rs

@ -1,92 +1,23 @@
use libc::c_int; use libc::c_int;
use std::error::Error; use std::error::Error;
use std::io::IoError;
use std::str; use std::str;
use ffi; use ffi;
#[deriving(Show, Eq, PartialEq)] #[deriving(Show, Eq, PartialEq, Copy, Clone)]
pub enum LmdbError { pub struct LmdbError {
KeyExist, err_code: c_int,
NotFound,
PageNotFound,
Corrupted,
Panic,
VersionMismatch,
Invalid,
MapFull,
DbsFull,
ReadersFull,
TlsFull,
TxnFull,
CursorFull,
PageFull,
MapResized,
Incompatible,
BadRslot,
BadTxn,
BadValSize,
BadDbi,
Unknown(c_int),
Io(IoError),
} }
impl Error for LmdbError { impl Error for LmdbError {
fn description(&self) -> &str { fn description(&self) -> &str {
let err_code = match *self { unsafe { str::from_c_str(ffi::mdb_strerror(self.err_code) as *const _) }
LmdbError::KeyExist => ffi::MDB_KEYEXIST,
LmdbError::NotFound => ffi::MDB_NOTFOUND,
LmdbError::PageNotFound => ffi::MDB_PAGE_NOTFOUND,
LmdbError::Corrupted => ffi::MDB_CORRUPTED,
LmdbError::Panic => ffi::MDB_PANIC,
LmdbError::VersionMismatch => ffi::MDB_VERSION_MISMATCH,
LmdbError::Invalid => ffi::MDB_INVALID,
LmdbError::MapFull => ffi::MDB_MAP_FULL,
LmdbError::DbsFull => ffi::MDB_DBS_FULL,
LmdbError::ReadersFull => ffi::MDB_READERS_FULL,
LmdbError::TlsFull => ffi::MDB_TLS_FULL,
LmdbError::TxnFull => ffi::MDB_TXN_FULL,
LmdbError::CursorFull => ffi::MDB_CURSOR_FULL,
LmdbError::PageFull => ffi::MDB_PAGE_FULL,
LmdbError::MapResized => ffi::MDB_MAP_RESIZED,
LmdbError::Incompatible => ffi::MDB_INCOMPATIBLE,
LmdbError::BadRslot => ffi::MDB_BAD_RSLOT,
LmdbError::BadTxn => ffi::MDB_BAD_TXN,
LmdbError::BadValSize => ffi::MDB_BAD_VALSIZE,
LmdbError::BadDbi => ffi::MDB_BAD_DBI,
LmdbError::Unknown(i) => i,
LmdbError::Io(ref io_error) => return io_error.description(),
};
unsafe { str::from_c_str(ffi::mdb_strerror(err_code) as *const _) }
} }
} }
impl LmdbError { impl LmdbError {
pub fn from_err_code(err_code: c_int) -> LmdbError { pub fn from_err_code(err_code: c_int) -> LmdbError {
match err_code { LmdbError { err_code: err_code}
i if i > 0 => LmdbError::Io(IoError::from_errno(err_code as uint, true)),
ffi::MDB_KEYEXIST => LmdbError::KeyExist,
ffi::MDB_NOTFOUND => LmdbError::NotFound,
ffi::MDB_PAGE_NOTFOUND => LmdbError::PageNotFound,
ffi::MDB_CORRUPTED => LmdbError::Corrupted,
ffi::MDB_PANIC => LmdbError::Panic,
ffi::MDB_VERSION_MISMATCH => LmdbError::VersionMismatch,
ffi::MDB_INVALID => LmdbError::Invalid,
ffi::MDB_MAP_FULL => LmdbError::MapFull,
ffi::MDB_DBS_FULL => LmdbError::DbsFull,
ffi::MDB_READERS_FULL => LmdbError::ReadersFull,
ffi::MDB_TLS_FULL => LmdbError::TlsFull,
ffi::MDB_TXN_FULL => LmdbError::TxnFull,
ffi::MDB_CURSOR_FULL => LmdbError::CursorFull,
ffi::MDB_PAGE_FULL => LmdbError::PageFull,
ffi::MDB_MAP_RESIZED => LmdbError::MapResized,
ffi::MDB_INCOMPATIBLE => LmdbError::Incompatible,
ffi::MDB_BAD_RSLOT => LmdbError::BadRslot,
ffi::MDB_BAD_TXN => LmdbError::BadTxn,
ffi::MDB_BAD_VALSIZE => LmdbError::BadValSize,
i => LmdbError::Unknown(i),
}
} }
} }
@ -99,3 +30,21 @@ pub fn lmdb_result(err_code: c_int) -> LmdbResult<()> {
Err(LmdbError::from_err_code(err_code)) Err(LmdbError::from_err_code(err_code))
} }
} }
#[cfg(test)]
mod test {
use std::error::Error;
use super::*;
use ffi;
#[test]
fn test_description() {
assert_eq!("Permission denied",
LmdbError::from_err_code(13).description());
assert_eq!("MDB_NOTFOUND: No matching key/data pair found",
LmdbError::from_err_code(ffi::MDB_NOTFOUND).description());
}
}

Loading…
Cancel
Save