aboutsummaryrefslogtreecommitdiffstats
path: root/src/message_properties.rs
blob: 4802383f2bef25ba909fe560316284178d4039b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::ops::Drop;
use std::ffi::{CStr, CString};
use supercow::Supercow;

use ffi;
use Message;
use MessageOwner;
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<S>(ptr: *mut ffi::notmuch_message_properties_t, owner: S) -> MessageProperties<'m, 'o, O>
    where
        S: Into<ScopedPhantomcow<'m, Message<'o, O>>>,
    {
        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<Self::Item> {
        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 {}