leveldb-rs-binding 2.2.0

An interface for the LevelDB
Documentation
//! Management functions, e.g. for destroying and repairing a LevelDB.
use crate::error::Error;
use crate::options::{Options, c_options};
use libc::c_char;
use std::ffi::CString;
use std::path::Path;
use std::ptr;

use crate::binding::{leveldb_destroy_db, leveldb_repair_db};

/// Destroy a LevelDB instance. At this moment, the LevelDB instance shouldn't be referenced anywhere else.
pub fn destroy(name: &Path, options: Options) -> Result<(), Error> {
    let mut error = ptr::null_mut();
    unsafe {
        let c_string = CString::new(name.to_str().unwrap()).unwrap();
        let c_options = c_options(&options);
        leveldb_destroy_db(
            c_options,
            c_string.as_bytes_with_nul().as_ptr() as *const c_char,
            &mut error,
        );

        if error.is_null() {
            Ok(())
        } else {
            Err(Error::new_from_char(error))
        }
    }
}

/// Repair the LevelDB instance. At this moment, the LevelDB instance should be closed.
pub fn repair(name: &Path, options: Options) -> Result<(), Error> {
    let mut error = ptr::null_mut();
    unsafe {
        let c_string = CString::new(name.to_str().unwrap()).unwrap();
        let c_options = c_options(&options);
        leveldb_repair_db(
            c_options,
            c_string.as_bytes_with_nul().as_ptr() as *const c_char,
            &mut error,
        );

        if error.is_null() {
            Ok(())
        } else {
            Err(Error::new_from_char(error))
        }
    }
}