aboutsummaryrefslogtreecommitdiffstats
path: root/src/message.rs
blob: 086670c6b40e627f85afd67cbce57246ba873af7 (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
use std::ops::Drop;
use std::marker::PhantomData;
use std::path::PathBuf;
use std::ffi::CString;

use error::{Error, Result};

use ffi;
use utils::{
    ToStr,
    NewFromPtr
};
use Query;
use Messages;
use Filenames;
use Tags;

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

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

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

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

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

    pub fn replies(self: &'q Self) -> Messages<'d, 'q>{
        Messages::new(unsafe {
            ffi::notmuch_message_get_replies(self.0)
        })
    }

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

    pub fn filenames(self: &'d Self) -> Filenames<'d>{
        Filenames::new(unsafe {
            ffi::notmuch_message_get_filenames(self.0)
        })
    }

    pub fn filename(self: &Self) -> PathBuf{
        PathBuf::from(unsafe {
            ffi::notmuch_message_get_filename(self.0)
        }.to_str().unwrap())
    }

    pub fn header(&self, name: &str) -> Result<&str> {
        let ret = unsafe {
            ffi::notmuch_message_get_header(self.0,
                CString::new(name).unwrap().as_ptr())
        };
        if ret.is_null() {
            Err(Error::UnspecifiedError)
        } else {
            Ok(ret.to_str().unwrap())
        }
    }

    pub fn tags(self: &'d Self) -> Tags<'d>{
        Tags::new(unsafe {
            ffi::notmuch_message_get_tags(self.0)
        })
    }
}


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

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