aboutsummaryrefslogtreecommitdiffstats
path: root/src/filenames.rs
blob: 5d6a19c23b846f782edf589a4e45a8edaeed3029 (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
use std::{
    ops,
    marker,
    iter
};

use std::path::{
    PathBuf,
    Path
};

use std::ffi::{
    CString,
    CStr
};

use utils::{
    NewFromPtr,
};

use database;
use ffi;

#[derive(Debug)]
pub struct Filenames<'d>(
    *mut ffi::notmuch_filenames_t,
    marker::PhantomData<&'d mut database::Database>,
);

impl<'d> NewFromPtr<*mut ffi::notmuch_filenames_t> for Filenames<'d> {
    fn new(ptr: *mut ffi::notmuch_filenames_t) -> Filenames<'d> {
        Filenames(ptr, marker::PhantomData)
    }
}

impl<'d> ops::Drop for Filenames<'d> {
    fn drop(&mut self) {
        unsafe {
            ffi::notmuch_filenames_destroy(self.0)
        };
    }
}

impl<'d> iter::Iterator for Filenames<'d> {
    type Item = PathBuf;

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

        let valid = unsafe {
            ffi::notmuch_filenames_valid(self.0)
        };

        if valid == 0{
            return None
        }

        let ctag = unsafe {
            ffi::notmuch_filenames_move_to_next(self.0);
            CStr::from_ptr(ffi::notmuch_filenames_get(self.0))
        };

        Some(PathBuf::from(ctag.to_str().unwrap()))
    }
}