From 9d3eaa7aa7e2be84e150075fb12e97083d7c61f8 Mon Sep 17 00:00:00 2001 From: Dirk Van Haerenborgh Date: Fri, 25 Oct 2019 09:52:00 +0200 Subject: implement message_properties --- src/message_properties.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/message_properties.rs (limited to 'src/message_properties.rs') diff --git a/src/message_properties.rs b/src/message_properties.rs new file mode 100644 index 0000000..2c2ceb0 --- /dev/null +++ b/src/message_properties.rs @@ -0,0 +1,75 @@ +use std::ops::Drop; +use std::ffi::{CStr, CString}; +use supercow::Supercow; + +use ffi; +use Message; +use MessageOwner; +use MessageExt; +use utils::{ScopedSupercow, ScopedPhantomcow}; + + +#[derive(Debug)] +pub struct MessageProperties<'m, 'o, O> +where + O: MessageOwner + 'o +{ + ptr: *mut ffi::notmuch_message_properties_t, + marker: ScopedPhantomcow<'m, Message<'o, O>>, +} + +impl<'m, 'o, O> Drop for MessageProperties<'m, 'o, O> +where + O: MessageOwner + 'o +{ + fn drop(&mut self) { + unsafe { ffi::notmuch_message_properties_destroy(self.ptr) }; + } +} + +impl<'m, 'o, O> MessageProperties<'m, 'o, O> +where + O: MessageOwner + 'o +{ + pub(crate) fn from_ptr(ptr: *mut ffi::notmuch_message_properties_t, owner: S) -> MessageProperties<'m, 'o, O> + where + S: Into>>, + { + MessageProperties { + ptr, + marker: owner.into(), + } + } +} + + +impl<'m, 'o, O> Iterator for MessageProperties<'m, 'o, O> +where + O: MessageOwner + 'o +{ + type Item = (String, String); + + fn next(&mut self) -> Option { + let valid = unsafe { ffi::notmuch_message_properties_valid(self.ptr) }; + + if valid == 0 { + return None; + } + + let (k, v) = unsafe { + let key = CStr::from_ptr(ffi::notmuch_message_properties_key(self.ptr)); + let value = CStr::from_ptr(ffi::notmuch_message_properties_value(self.ptr)); + + ffi::notmuch_message_properties_move_to_next(self.ptr); + + (key, value) + }; + + Some((k.to_str().unwrap().to_string(), v.to_str().unwrap().to_string())) + } +} + +unsafe impl<'m, 'o, O> Send for MessageProperties<'m, 'o, O> where + O: MessageOwner + 'o {} +unsafe impl<'m, 'o, O> Sync for MessageProperties<'m, 'o, O> where + O: MessageOwner + 'o {} -- cgit v1.2.1