aboutsummaryrefslogtreecommitdiffstats
path: root/src/messages.rs
blob: ef196e55f22c251bbc470273dff547fa5e37c300 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::ops::Drop;
use std::iter::Iterator;
use std::marker::PhantomData;

use ffi;
use utils::{
    FromPtr,
};
use Query;
use Message;
use Tags;
use message::MessageOwner;
use tags::TagsOwner;

pub trait MessagesOwner{
}

#[derive(Debug)]
pub(crate) struct MessagesPtr {
    pub ptr: *mut ffi::notmuch_messages_t
}

impl Drop for MessagesPtr {
    fn drop(self: &mut Self) {
        let valid = unsafe {
            ffi::notmuch_messages_valid(self.ptr)
        };

        if valid == 0{
            return;
        }

        unsafe {
            ffi::notmuch_messages_destroy(self.ptr)
        };
    }
}


#[derive(Debug)]
pub struct Messages<'o, Owner: MessagesOwner>{
    pub(crate) handle: MessagesPtr,
    phantom: PhantomData<&'o Owner>,
}

impl<'o, Owner: MessagesOwner> FromPtr<*mut ffi::notmuch_messages_t> for Messages<'o, Owner> {
    fn from_ptr(ptr: *mut ffi::notmuch_messages_t) -> Messages<'o, Owner> {
        Messages{
            handle: MessagesPtr{ptr},
            phantom: PhantomData
        }
    }
}

impl<'o, Owner: MessagesOwner> MessageOwner for Messages<'o, Owner>{}
impl<'o, Owner: MessagesOwner> TagsOwner for Messages<'o, Owner>{}


impl<'o, Owner: MessagesOwner> Messages<'o, Owner>{

    /**
     * Return a list of tags from all messages.
     *
     * The resulting list is guaranteed not to contain duplicated tags.
     *
     * WARNING: You can no longer iterate over messages after calling this
     * function, because the iterator will point at the end of the list.
     * We do not have a function to reset the iterator yet and the only
     * way how you can iterate over the list again is to recreate the
     * message list.
     *
     * The function returns NULL on error.
     */
    pub fn collect_tags<'m>(self: &'o Self) -> Tags<'m, Self>{
        Tags::from_ptr(unsafe {
            ffi::notmuch_messages_collect_tags(self.handle.ptr)
        })
    }
}



impl<'o, Owner: MessagesOwner> Iterator for Messages<'o, Owner> {
    type Item = Message<'o, Self>;

    fn next(&mut self) -> Option<Self::Item> {

        let valid = unsafe {
            ffi::notmuch_messages_valid(self.handle.ptr)
        };

        if valid == 0{
            return None
        }

        let cmsg = unsafe {
            let msg = ffi::notmuch_messages_get(self.handle.ptr);
            ffi::notmuch_messages_move_to_next(self.handle.ptr);
            msg
        };

        Some(Self::Item::from_ptr(cmsg))
    }
}

unsafe impl<'o, Owner: MessagesOwner> Send for Messages<'o, Owner>{}
unsafe impl<'o, Owner: MessagesOwner> Sync for Messages<'o, Owner>{}