Safe Rust bindings for LMDB with encryption at rest
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lmdb-rs/src/error.rs

51 lines
1.0 KiB

10 years ago
use libc::c_int;
use std::error::Error;
use std::str;
use ffi;
#[deriving(Show, Eq, PartialEq, Copy, Clone)]
pub struct LmdbError {
err_code: c_int,
10 years ago
}
impl Error for LmdbError {
fn description(&self) -> &str {
unsafe { str::from_c_str(ffi::mdb_strerror(self.err_code) as *const _) }
10 years ago
}
}
impl LmdbError {
pub fn from_err_code(err_code: c_int) -> LmdbError {
LmdbError { err_code: err_code}
10 years ago
}
}
pub type LmdbResult<T> = Result<T, LmdbError>;
pub fn lmdb_result(err_code: c_int) -> LmdbResult<()> {
if err_code == ffi::MDB_SUCCESS {
Ok(())
} else {
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());
}
}