aboutsummaryrefslogtreecommitdiffstats
path: root/src/query.rs
blob: 2873b27da80cf751c024d23c62493bc71c2b7944 (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
use std;
use std::{
    ops,
    marker,
    ptr,
};

use error::Result;

use ffi;
use utils::{
    NewFromPtr,
};
use Database;
use Messages;
use Threads;
use ffi::Sort;

#[derive(Debug)]
pub struct Query<'d>(
    pub(crate) *mut ffi::notmuch_query_t,
    marker::PhantomData<&'d mut Database>,
);


impl<'d> Query<'d> {
    pub fn create(db: &'d Database, query_string: &String) -> Result<Self> {
        db.create_query(query_string)
    }

    /// Specify the sorting desired for this query.
    pub fn set_sort(self: &Self, sort: Sort)
    {
        unsafe {
            ffi::notmuch_query_set_sort(
                self.0, sort.into(),
            )
        }
    }

    /// Return the sort specified for this query. See
    /// `set_sort`.
    pub fn sort(self: &Self) -> Sort
    {
        unsafe {
            ffi::notmuch_query_get_sort(
                self.0,
            )
        }.into()
    }


    /// Filter messages according to the query and return
    pub fn search_messages(self: &Self) -> Result<Option<Messages>>
    {
        let mut msgs = ptr::null_mut();
        let ret = try!(unsafe {
            ffi::notmuch_query_search_messages(
                self.0, &mut msgs,
            )
        }.as_result());

        match msgs.is_null() {
            false => Ok(None),
            true => Ok(Some(Messages::new(msgs))),
        }
    }

    pub fn count_messages(self: &Self) -> Result<u32>
    {
        let mut cnt = 0;
        let ret = try!(unsafe {
            ffi::notmuch_query_count_messages(
                self.0, &mut cnt,
            )
        }.as_result());

        // if ret.is_err(){
        //     return ret;
        // }
        return Ok(cnt);
    }

    pub fn search_threads(self: &Self) -> Result<Option<Threads>>
    {
        let mut thrds = ptr::null_mut();
        let ret = try!(unsafe {
            ffi::notmuch_query_search_threads(
                self.0, &mut thrds,
            )
        }.as_result());

        match thrds.is_null() {
            false => Ok(None),
            true => Ok(Some(Threads::new(thrds))),
        }
    }

    pub fn count_threads(self: &Self) -> Result<u32>
    {
        let mut cnt = 0;
        let ret = try!(unsafe {
            ffi::notmuch_query_count_threads(
                self.0, &mut cnt,
            )
        }.as_result());

        // if ret.is_err(){
        //     return ret;
        // }
        return Ok(cnt);
    }
}

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


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