aboutsummaryrefslogtreecommitdiffstats
path: root/src/message.rs
blob: ed729977583b67c136b319d4a2cff739ba327ef7 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::ffi::CString;
use std::ops::Drop;
use std::path::PathBuf;
use supercow::Phantomcow;

use error::{Error, Result};
use ffi;
use ffi::Status;
use utils::ToStr;
use Filenames;
use FilenamesOwner;
use Messages;
use MessagesOwner;
use Tags;
use TagsOwner;

pub trait MessageOwner {}

#[derive(Debug)]
pub(crate) struct MessagePtr {
    pub ptr: *mut ffi::notmuch_message_t,
}

impl Drop for MessagePtr {
    fn drop(&mut self) {
        unsafe { ffi::notmuch_message_destroy(self.ptr) };
    }
}

#[derive(Debug)]
pub struct Message<'o, Owner: MessageOwner + 'o> {
    pub(crate) handle: MessagePtr,
    marker: Phantomcow<'o, Owner>,
}

impl<'o, Owner: MessageOwner + 'o> MessagesOwner for Message<'o, Owner> {}
impl<'o, Owner: MessageOwner + 'o> FilenamesOwner for Message<'o, Owner> {}
impl<'o, Owner: MessageOwner + 'o> TagsOwner for Message<'o, Owner> {}

impl<'o, Owner: MessageOwner + 'o> Message<'o, Owner> {
    pub fn from_ptr<O: Into<Phantomcow<'o, Owner>>>(
        ptr: *mut ffi::notmuch_message_t,
        owner: O,
    ) -> Message<'o, Owner> {
        Message {
            handle: MessagePtr { ptr },
            marker: owner.into(),
        }
    }

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

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

    pub fn replies(self: &Self) -> Messages<Self> {
        Messages::from_ptr(
            unsafe { ffi::notmuch_message_get_replies(self.handle.ptr) },
            self,
        )
    }

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

    pub fn filenames(self: &Self) -> Filenames<Self> {
        Filenames::from_ptr(
            unsafe { ffi::notmuch_message_get_filenames(self.handle.ptr) },
            self,
        )
    }

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

    pub fn date(&self) -> i64 {
        unsafe {
            ffi::notmuch_message_get_date(self.handle.ptr)
        }
    }

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

    pub fn tags(&self) -> Tags<Self> {
        Tags::from_ptr(
            unsafe { ffi::notmuch_message_get_tags(self.handle.ptr) },
            self,
        )
    }

    pub fn add_tag(self: &Self, tag: &str) -> Status {
        let tag = CString::new(tag).unwrap();
        Status::from(unsafe {
            ffi::notmuch_message_add_tag(self.handle.ptr, tag.as_ptr())
        })
    }

    pub fn remove_tag(self: &Self, tag: &str) -> Status {
        let tag = CString::new(tag).unwrap();
        Status::from(unsafe {
            ffi::notmuch_message_remove_tag(self.handle.ptr, tag.as_ptr())
        })
    }

    pub fn remove_all_tags(self: &Self) -> Status {
        Status::from(unsafe {
            ffi::notmuch_message_remove_all_tags(self.handle.ptr)
        })
    }
}

unsafe impl<'o, Owner: MessageOwner + 'o> Send for Message<'o, Owner> {}
unsafe impl<'o, Owner: MessageOwner + 'o> Sync for Message<'o, Owner> {}