diff options
| author | C. Morgan Hamill <me@cmhamill.org> | 2015-03-20 16:28:47 +0100 |
|---|---|---|
| committer | C. Morgan Hamill <me@cmhamill.org> | 2015-03-20 16:28:47 +0100 |
| commit | de63975fbe66607f8a1ad0ac8332a6bca15d3a01 (patch) | |
| tree | 05981eb2a972db51fcfe14cb2f2a3f24932ce596 /src/database.rs | |
| parent | b7ea6953d44978ae3dadbd277f001bf35ae06822 (diff) | |
| download | mail-de63975fbe66607f8a1ad0ac8332a6bca15d3a01.tar.gz | |
Add `Database::compact()` and related methods.
Not so simple wrapper around notmuch API's `notmuch_database_compact()`
function. Variants with and without a callback parameter are provided.
Diffstat (limited to 'src/database.rs')
| -rw-r--r-- | src/database.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/database.rs b/src/database.rs index 5a7cec4..7364942 100644 --- a/src/database.rs +++ b/src/database.rs @@ -9,6 +9,7 @@ use error::Result; use utils::{ NotmuchEnum, ToCString, + ToStr, }; use ffi; @@ -46,4 +47,48 @@ impl Database { Ok(Database(db)) } + pub fn compact<P: path::AsPath, F: FnMut(&str)>( + path: &P, backup_path: Option<&P>, + ) -> Result<()> { + let status: Option<F> = None; + Database::_compact(path, backup_path, status) + } + + pub fn compact_with_status<P: path::AsPath, F: FnMut(&str)>( + path: &P, backup_path: Option<&P>, status: F, + ) -> Result<()> { + Database::_compact(path, backup_path, Some(status)) + } + + fn _compact<P: path::AsPath, F: FnMut(&str)>( + path: &P, backup_path: Option<&P>, status: Option<F>, + ) -> Result<()> { + + extern fn wrapper<F: FnMut(&str)>( + message:*const libc::c_char, closure: *mut libc::c_void, + ) { + let closure = closure as *mut F; + unsafe { + (*closure)(message.to_str().unwrap()) + } + } + + let path = path.as_path().to_cstring().unwrap(); + let backup_path = backup_path.map(|p| { + p.as_path().to_cstring().unwrap() + }); + + try!(unsafe { + ffi::notmuch_database_compact( + path.as_ptr(), backup_path.map_or(ptr::null(), |p| p.as_ptr()), + if status.is_some() { Some(wrapper::<F>) } else { None }, + status.map_or(ptr::null_mut(), |f| { + &f as *const _ as *mut libc::c_void + }), + ) + }.as_result()); + + Ok(()) + } + } |
