diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2019-10-25 09:12:59 +0200 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2019-10-25 09:31:20 +0200 |
| commit | 51f063aaf2cab590a6cfe42e1da3f7bbd6423ba5 (patch) | |
| tree | e0fa5ad5c52084bbae12bd65e5f5a424699702a5 | |
| parent | 6813fbb5226acfa53e30f41564bb41c93d808656 (diff) | |
| download | mail-51f063aaf2cab590a6cfe42e1da3f7bbd6423ba5.tar.gz | |
add scopable atomic operation and configlist
| -rw-r--r-- | src/config_list.rs | 62 | ||||
| -rw-r--r-- | src/database.rs | 48 | ||||
| -rw-r--r-- | src/lib.rs | 6 |
3 files changed, 114 insertions, 2 deletions
diff --git a/src/config_list.rs b/src/config_list.rs new file mode 100644 index 0000000..632603c --- /dev/null +++ b/src/config_list.rs @@ -0,0 +1,62 @@ +use std::ops::Drop; +use std::ffi::{CStr, CString}; +use supercow::Supercow; + +use ffi; +use Database; +use Filenames; +use FilenamesOwner; +use utils::{ScopedSupercow, ScopedPhantomcow}; + + +#[derive(Debug)] +pub struct ConfigList<'d> { + ptr: *mut ffi::notmuch_config_list_t, + marker: ScopedPhantomcow<'d, Database>, +} + +impl<'d> Drop for ConfigList<'d> { + fn drop(&mut self) { + unsafe { ffi::notmuch_config_list_destroy(self.ptr) }; + } +} + +impl<'d> ConfigList<'d> { + pub(crate) fn from_ptr<O>(ptr: *mut ffi::notmuch_config_list_t, owner: O) -> ConfigList<'d> + where + O: Into<ScopedPhantomcow<'d, Database>>, + { + ConfigList { + ptr, + marker: owner.into(), + } + } +} + + +impl<'d> Iterator for ConfigList<'d> +{ + type Item = (String, String); + + fn next(&mut self) -> Option<Self::Item> { + let valid = unsafe { ffi::notmuch_config_list_valid(self.ptr) }; + + if valid == 0 { + return None; + } + + let (k, v) = unsafe { + let key = CStr::from_ptr(ffi::notmuch_config_list_key(self.ptr)); + let value = CStr::from_ptr(ffi::notmuch_config_list_value(self.ptr)); + + ffi::notmuch_config_list_move_to_next(self.ptr); + + (key, value) + }; + + Some((k.to_str().unwrap().to_string(), v.to_str().unwrap().to_string())) + } +} + +unsafe impl<'d> Send for ConfigList<'d> {} +unsafe impl<'d> Sync for ConfigList<'d> {} diff --git a/src/database.rs b/src/database.rs index c828bb2..089976d 100644 --- a/src/database.rs +++ b/src/database.rs @@ -18,6 +18,7 @@ use TagsOwner; use Message; use MessageOwner; use IndexOpts; +use ConfigList; use utils::ScopedSupercow; @@ -218,6 +219,11 @@ impl Database { <Self as DatabaseExt>::directory(self, path) } + pub fn get_config_list<'d>(&'d self, prefix: &str) -> Result<ConfigList<'d>> + { + <Self as DatabaseExt>::get_config_list(self, prefix) + } + pub fn create_query<'d>(&'d self, query_string: &str) -> Result<Query<'d>> { <Self as DatabaseExt>::create_query(self, query_string) } @@ -310,6 +316,22 @@ pub trait DatabaseExt { } } + fn get_config_list<'d, D>(database: D, prefix: &str) -> Result<ConfigList<'d>> + where + D: Into<ScopedSupercow<'d, Database>> + { + let dbref = database.into(); + + let prefix_str = CString::new(prefix).unwrap(); + + let mut cfgs = ptr::null_mut(); + unsafe { + ffi::notmuch_database_get_config_list(dbref.ptr, prefix_str.as_ptr(), &mut cfgs) + }.as_result()?; + + Ok(ConfigList::from_ptr(cfgs, Supercow::phantom(dbref))) + } + fn find_message<'d, D>(database: D, message_id: &str) -> Result<Option<Message<'d, Database>>> where D: Into<ScopedSupercow<'d, Database>> @@ -406,3 +428,29 @@ impl DatabaseExt for Database {} unsafe impl Send for Database {} unsafe impl Sync for Database {} + + +#[derive(Debug)] +pub struct AtomicOperation<'d> { + database: ScopedSupercow<'d, Database>, +} + +impl<'d> AtomicOperation<'d> { + pub fn new<D>(db: D) -> Result<Self> + where + D: Into<ScopedSupercow<'d, Database>>, + { + let database = db.into(); + database.begin_atomic()?; + Ok(AtomicOperation{ + database + }) + } +} + +impl<'d> Drop for AtomicOperation<'d> { + fn drop(&mut self) { + let _ = self.database.end_atomic(); + } +} + @@ -21,8 +21,9 @@ mod tags; mod thread; mod threads; mod index; +mod config_list; -pub use database::{Database, DatabaseExt}; +pub use database::{Database, DatabaseExt, AtomicOperation}; pub use directory::{Directory, DirectoryExt}; pub use error::Error; pub use filenames::{Filenames, FilenamesOwner}; @@ -33,7 +34,8 @@ pub use tags::{Tags, TagsExt, TagsOwner}; pub use thread::{Thread, ThreadExt}; pub use threads::{Threads, ThreadsExt}; pub use index::IndexOpts; +pub use config_list::ConfigList; -pub use ffi::{DatabaseMode, Sort, DecryptionPolicy}; +pub use ffi::{Status, DatabaseMode, Sort, DecryptionPolicy}; pub use utils::{ScopedSupercow, ScopedPhantomcow};
\ No newline at end of file |
