ndb.rs (1270B)
1 use std::ffi::CString; 2 use std::ptr; 3 4 use crate::bindings; 5 use crate::config::NdbConfig; 6 use crate::error::Error; 7 use crate::result::Result; 8 9 pub struct Ndb { 10 ndb: *mut bindings::ndb, 11 } 12 13 impl Ndb { 14 // Constructor 15 pub fn new(db_dir: &str, config: &NdbConfig) -> Result<Self> { 16 let db_dir_cstr = match CString::new(db_dir) { 17 Ok(cstr) => cstr, 18 Err(_) => return Err(Error::DbOpenFailed), 19 }; 20 let mut ndb: *mut bindings::ndb = ptr::null_mut(); 21 let result = unsafe { bindings::ndb_init(&mut ndb, db_dir_cstr.as_ptr(), config.as_ptr()) }; 22 23 if result != 0 { 24 return Err(Error::DbOpenFailed); 25 } 26 27 Ok(Ndb { ndb }) 28 } 29 30 // Add other methods to interact with the library here 31 } 32 33 impl Drop for Ndb { 34 fn drop(&mut self) { 35 unsafe { 36 bindings::ndb_destroy(self.ndb); 37 } 38 } 39 } 40 41 #[cfg(test)] 42 mod tests { 43 use super::*; 44 use std::fs; 45 46 fn cleanup() { 47 let _ = fs::remove_file("data.mdb"); 48 let _ = fs::remove_file("lock.mdb"); 49 } 50 51 #[test] 52 fn ndb_init_works() { 53 // Initialize ndb 54 { 55 let cfg = NdbConfig::new(); 56 let _ = Ndb::new(".", &cfg); 57 } 58 59 cleanup(); 60 } 61 }