aboutsummaryrefslogtreecommitdiffstats
path: root/src/thread.rs
blob: 4f98c260f102de8f9ce88fde187ffc49bb901154 (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
108
109
110
use std::ops::Drop;
use std::marker::PhantomData;
use ffi;
use utils::{
    NewFromPtr,
    ToStr
};
use Query;
use Messages;
use Tags;

#[derive(Debug)]
pub struct Thread<'d:'q, 'q>(
    pub(crate) *mut ffi::notmuch_thread_t,
    PhantomData<&'q Query<'d>>,
);

impl<'d, 'q> NewFromPtr<*mut ffi::notmuch_thread_t> for Thread<'d, 'q> {
    fn new(ptr: *mut ffi::notmuch_thread_t) -> Thread<'d, 'q> {
        Thread(ptr, PhantomData)
    }
}

impl<'d, 'q> Thread<'d, 'q>{

    pub fn id(self: &Self) -> String{
        let tid = unsafe {
            ffi::notmuch_thread_get_thread_id(self.0)
        };
        tid.to_str().unwrap().to_string()
    }


    pub fn total_messages(self: &Self) -> i32{
        unsafe {
            ffi::notmuch_thread_get_total_messages(self.0)
        }
    }

    #[cfg(feature = "0.26")]
    pub fn total_files(self: &Self) -> i32{
        unsafe {
            ffi::notmuch_thread_get_total_files(self.0)
        }
    }


    pub fn toplevel_messages(self: &Self) -> Messages{
        Messages::new(unsafe {
            ffi::notmuch_thread_get_toplevel_messages(self.0)
        })
    }

    /// Get a `Messages` iterator for all messages in 'thread' in
    /// oldest-first order.
    pub fn messages(self: &Self) -> Messages{
        Messages::new(unsafe {
            ffi::notmuch_thread_get_messages(self.0)
        })
    }


    pub fn tags(self: &Self) -> Tags{
        Tags::new(unsafe {
            ffi::notmuch_thread_get_tags(self.0)
        })
    }

    pub fn subject(self: &Self) -> String{
        let sub = unsafe {
            ffi::notmuch_thread_get_subject(self.0)
        };

        sub.to_str().unwrap().to_string()
    }

    pub fn authors(self: &Self) -> Vec<String>{
        let athrs = unsafe {
            ffi::notmuch_thread_get_authors(self.0)
        };

        athrs.to_str().unwrap().split(',').map(|s| s.to_string()).collect()
    }

    /// Get the date of the oldest message in 'thread' as a time_t value.
    pub fn oldest_date(self: &Self) -> i64 {
        unsafe {
            ffi::notmuch_thread_get_oldest_date(self.0)
        }
    }

    /// Get the date of the newest message in 'thread' as a time_t value.
    pub fn newest_date(self: &Self) -> i64 {
       unsafe {
           ffi::notmuch_thread_get_newest_date(self.0)
       }
    }
}


impl<'d, 'q> Drop for Thread<'d, 'q> {
    fn drop(&mut self) {
        unsafe {
            ffi::notmuch_thread_destroy(self.0)
        };
    }
}

unsafe impl<'d, 'q> Send for Thread<'d, 'q> {}
unsafe impl<'d, 'q> Sync for Thread<'d, 'q> {}