aboutsummaryrefslogtreecommitdiffstats
path: root/notmuch/src/message.rs
blob: 55a544fbf50c34f9633b7f8afaca09fb19a7c692 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::ffi::CString;
use std::path::PathBuf;
use std::cell::RefCell;
use std::borrow::Cow;
use std::ptr;

use supercow::{Supercow};

use error::{Error, Result};
use ffi;
use utils::{ToStr, ScopedPhantomcow, ScopedSupercow};
use Filenames;
use FilenamesOwner;
use Messages;
use MessageProperties;
use Tags;
use TagsOwner;
use IndexOpts;

pub trait MessageOwner: Send + Sync {}

#[derive(Debug)]
pub struct Message<'o, O>
where
    O: MessageOwner + 'o,
{
    pub(crate) ptr: *mut ffi::notmuch_message_t,
    marker: RefCell<ScopedPhantomcow<'o, O>>,
}

impl<'o, O> MessageOwner for Message<'o, O> where O: MessageOwner + 'o {}
impl<'o, O> FilenamesOwner for Message<'o, O> where O: MessageOwner + 'o {}
impl<'o, O> TagsOwner for Message<'o, O> where O: MessageOwner + 'o {}


// impl<'o, O> PartialEq for Message<'o, O>
// where
//     O: MessageOwner + 'o
// {
//     fn eq(self: &Self, other: &Message<'o, O>) -> bool{
//         self.id() == other.id()
//     }
// }

impl<'o, O> Message<'o, O>
where
    O: MessageOwner + 'o,
{
    pub(crate) fn from_ptr<P>(ptr: *mut ffi::notmuch_message_t, owner: P) -> Message<'o, O>
    where
        P: Into<ScopedPhantomcow<'o, O>>,
    {
        Message {
            ptr,
            marker: RefCell::new(owner.into()),
        }
    }

    pub fn id(self: &Self) -> Cow<'_, str> {
        let mid = unsafe { ffi::notmuch_message_get_message_id(self.ptr) };
        mid.to_string_lossy()
    }

    pub fn thread_id(self: &Self) -> Cow<'_, str> {
        let tid = unsafe { ffi::notmuch_message_get_thread_id(self.ptr) };
        tid.to_string_lossy()
    }

    pub fn replies(self: &Self) -> Messages<'o, O> {
        Messages::<'o, O>::from_ptr(
            unsafe { ffi::notmuch_message_get_replies(self.ptr) },
            // will never panic since the borrow is released immediately
            ScopedPhantomcow::<'o, O>::share(&mut *(self.marker.borrow_mut()))
        )
    }

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

    pub fn filenames(self: &Self) -> Filenames<Self> {
        <Self as MessageExt<'o, O>>::filenames(self)
    }

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

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

    pub fn header(&self, name: &str) -> Result<Option<Cow<'_, str>>> {
        let name = CString::new(name).unwrap();
        let ret = unsafe { ffi::notmuch_message_get_header(self.ptr, name.as_ptr()) };
        if ret.is_null() {
            Err(Error::UnspecifiedError)
        } else {
            let ret_str = ret.to_string_lossy();
            if ret_str.is_empty() {
                Ok(None)
            } else{
                Ok(Some(ret_str))
            }
        }
    }

    pub fn tags(&self) -> Tags<Self> {
        <Self as MessageExt<'o, O>>::tags(self)
    }

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

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

    pub fn remove_all_tags(self: &Self) -> Result<()> {
        unsafe { ffi::notmuch_message_remove_all_tags(self.ptr) }.as_result()
    }

    pub fn tags_to_maildir_flags(self: &Self) -> Result<()> {
        unsafe { ffi::notmuch_message_tags_to_maildir_flags(self.ptr) }.as_result()
    }

    pub fn maildir_flags_to_tags(self: &Self) -> Result<()> {
        unsafe { ffi::notmuch_message_maildir_flags_to_tags(self.ptr) }.as_result()
    }

    pub fn reindex<'d>(self: &Self, indexopts: IndexOpts<'d>) -> Result<()> {
        unsafe { ffi::notmuch_message_reindex(self.ptr, indexopts.ptr) }.as_result()
    }

    pub fn freeze(self: &Self) -> Result<()> {
        unsafe { ffi::notmuch_message_freeze(self.ptr) }.as_result()
    }

    pub fn thaw(self: &Self) -> Result<()> {
        unsafe { ffi::notmuch_message_thaw(self.ptr) }.as_result()
    }

    pub fn properties<'m>(&'m self, key: &str, exact: bool) -> MessageProperties<'m, 'o, O> {
        <Self as MessageExt<'o, O>>::properties(self, key, exact)
    }

    pub fn remove_all_properties(&self, key: Option<&str>) -> Result<()>
    {
        match key {
            Some(k) => {
                let key_str = CString::new(k).unwrap();
                unsafe {
                    ffi::notmuch_message_remove_all_properties(self.ptr, key_str.as_ptr())
                }.as_result()
            },
            None => {
                let p = ptr::null();
                unsafe {
                    ffi::notmuch_message_remove_all_properties(self.ptr, p)
                }.as_result()
            }
        }
    }

    pub fn remove_all_properties_with_prefix(&self, prefix: Option<&str>) -> Result<()>
    {
        match prefix {
            Some(k) => {
                let key_str = CString::new(k).unwrap();
                unsafe {
                    ffi::notmuch_message_remove_all_properties_with_prefix(self.ptr, key_str.as_ptr())
                }.as_result()
            },
            None => {
                let p = ptr::null();
                unsafe {
                    ffi::notmuch_message_remove_all_properties_with_prefix(self.ptr, p)
                }.as_result()
            }
        }
    }


    pub fn count_properties(&self, key: &str) -> Result<u32>
    {
        let key_str = CString::new(key).unwrap();
        let mut cnt = 0;
        unsafe {
            ffi::notmuch_message_count_properties(self.ptr, key_str.as_ptr(), &mut cnt)
        }.as_result()?;

        Ok(cnt)
    }

    pub fn property(&self, key: &str) -> Result<Cow<'_, str>>
    {
        let key_str = CString::new(key).unwrap();
        let mut prop = ptr::null();
        unsafe {
            ffi::notmuch_message_get_property(self.ptr, key_str.as_ptr(), &mut prop)
        }.as_result()?;

        if prop.is_null() {
            Err(Error::UnspecifiedError)
        } else {
            // TODO: the unwrap here is not good
            Ok(prop.to_string_lossy())
        }
    }

    pub fn add_property(&self, key: &str, value: &str) -> Result<()>
    {
        let key_str = CString::new(key).unwrap();
        let value_str = CString::new(value).unwrap();
        unsafe {
            ffi::notmuch_message_add_property(self.ptr, key_str.as_ptr(), value_str.as_ptr())
        }.as_result()
    }

    pub fn remove_property(&self, key: &str, value: &str) -> Result<()>
    {
        let key_str = CString::new(key).unwrap();
        let value_str = CString::new(value).unwrap();
        unsafe {
            ffi::notmuch_message_remove_property(self.ptr, key_str.as_ptr(), value_str.as_ptr())
        }.as_result()
    }
}

pub trait MessageExt<'o, O>
where
    O: MessageOwner + 'o,
{
    fn tags<'m, M>(message: M) -> Tags<'m, Message<'o, O>>
    where
        M: Into<ScopedSupercow<'m, Message<'o, O>>>,
    {
        let messageref = message.into();
        Tags::from_ptr(
            unsafe { ffi::notmuch_message_get_tags(messageref.ptr) },
            Supercow::phantom(messageref),
        )
    }

    // fn replies<'s, S>(message: S) -> Messages<'s, Message<'o, O>>
    // where
    //     S: Into<ScopedSupercow<'s, Message<'o, O>>>,
    // {
    //     let messageref = message.into();
    //     Messages::from_ptr(
    //         unsafe { ffi::notmuch_message_get_replies(messageref.ptr) },
    //         Supercow::phantom(messageref),
    //     )
    // }

    fn filenames<'m, M>(message: M) -> Filenames<'m, Message<'o, O>>
    where
        M: Into<ScopedSupercow<'m, Message<'o, O>>>,
    {
        let messageref = message.into();
        Filenames::from_ptr(
            unsafe { ffi::notmuch_message_get_filenames(messageref.ptr) },
            Supercow::phantom(messageref),
        )
    }

    fn properties<'m, M>(message: M, key: &str, exact: bool) -> MessageProperties<'m, 'o, O>
    where
        M: Into<ScopedSupercow<'m, Message<'o, O>>>,
    {
        let messageref = message.into();
        let key_str = CString::new(key).unwrap();

        let props = unsafe {
            ffi::notmuch_message_get_properties(messageref.ptr, key_str.as_ptr(), exact as i32)
        };

        MessageProperties::from_ptr(props, Supercow::phantom(messageref))
    }
}

impl<'o, O> MessageExt<'o, O> for Message<'o, O> where O: MessageOwner + 'o {}

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


pub struct FrozenMessage<'m ,'o, O>
where
    O: MessageOwner + 'o
{
    message: ScopedSupercow<'m, Message<'o, O>>
}


impl<'m, 'o, O> FrozenMessage<'m, 'o, O>
where
    O: MessageOwner + 'o
{
    pub fn new<M>(message: M) -> Result<Self>
    where
        M: Into<ScopedSupercow<'m, Message<'o, O>>>
    {
        let msg = message.into();
        msg.freeze()?;
        Ok(FrozenMessage{
            message: msg
        })
    }
}

impl<'m, 'o, O> Drop for FrozenMessage<'m, 'o, O>
where
    O: MessageOwner + 'o
{
    fn drop(&mut self) {
        let _ = self.message.thaw();
    }
}