aboutsummaryrefslogtreecommitdiffstats
path: root/src/directory.rs
blob: 8ddc044b86061bcc36a167ed920f4554edfcfa4c (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
use std::ffi::{CStr, CString};
use std::ops::Drop;
use std::path::Path;
use std::ptr;
use supercow::{Supercow, Phantomcow};

use error::Result;
use ffi;
use Database;
use Filenames;
use FilenamesOwner;

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

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

#[derive(Debug)]
pub struct Directory<'d> {
    handle: DirectoryPtr,
    marker: Phantomcow<'d, Database>,
}

impl<'d> FilenamesOwner for Directory<'d> {}

impl<'d> Directory<'d> {
    pub fn from_ptr<O: Into<Phantomcow<'d, Database>>>(
        ptr: *mut ffi::notmuch_directory_t,
        owner: O,
    ) -> Directory<'d> {
        Directory {
            handle: DirectoryPtr { ptr },
            marker: owner.into(),
        }
    }

    pub fn new<O: Into<Supercow<'d, Database>>, 
               P: AsRef<Path>>(owner: O, path: &P) -> Result<Option<Directory<'d>>> {
        let db = owner.into();
        let path_str = CString::new(path.as_ref().to_str().unwrap()).unwrap();

        let mut dir = ptr::null_mut();
        try!(
            unsafe {
                ffi::notmuch_database_get_directory(db.handle.ptr, path_str.as_ptr(), &mut dir)
            }
            .as_result()
        );

        if dir.is_null() {
            Ok(None)
        } else {
            Ok(Some(Directory {
                handle: DirectoryPtr { ptr: dir },
                marker: Supercow::phantom(db),
            }))
        }
    }


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

pub trait DirectoryExt<'d>{

}

unsafe impl<'d> Send for Directory<'d> {}
unsafe impl<'d> Sync for Directory<'d> {}