diff options
Diffstat (limited to 'src/query.rs')
| -rw-r--r-- | src/query.rs | 66 |
1 files changed, 49 insertions, 17 deletions
diff --git a/src/query.rs b/src/query.rs index a90a0fe..7cf940c 100644 --- a/src/query.rs +++ b/src/query.rs @@ -1,6 +1,8 @@ use std::ops::Drop; use std::ptr; -use supercow::Phantomcow; +use std::ffi::{CStr, CString}; + +use supercow::{Supercow, Phantomcow}; use error::Result; use ffi; @@ -10,6 +12,8 @@ use Messages; use MessagesOwner; use Threads; use ThreadsOwner; +use threads::ThreadsPtr; +use messages::MessagesPtr; #[derive(Debug)] pub(crate) struct QueryPtr { @@ -32,18 +36,23 @@ impl<'d> ThreadsOwner for Query<'d> {} impl<'d> MessagesOwner for Query<'d> {} impl<'d> Query<'d> { - pub fn from_ptr<O: Into<Phantomcow<'d, Database>>>( - ptr: *mut ffi::notmuch_query_t, + pub(crate) fn from_handle<O: Into<Phantomcow<'d, Database>>>( + handle: QueryPtr, owner: O, ) -> Query<'d> { Query { - handle: QueryPtr { ptr }, + handle, marker: owner.into(), } } - pub fn create(db: &'d Database, query_string: &str) -> Result<Self> { - db.create_query(query_string) + pub fn create<D: Into<Supercow<'d, Database>>>(db: D, + query_string: &str) -> Result<Self> { + + let dbref = db.into(); + dbref.handle.create_query(query_string).map(move |handle|{ + Query::from_handle(handle, Supercow::phantom(dbref)) + }) } /// Specify the sorting desired for this query. @@ -59,12 +68,7 @@ impl<'d> Query<'d> { /// Filter messages according to the query and return pub fn search_messages<'q>(self: &'d Self) -> Result<Messages<'q, Self>> { - let mut msgs = ptr::null_mut(); - try!( - unsafe { ffi::notmuch_query_search_messages(self.handle.ptr, &mut msgs,) }.as_result() - ); - - Ok(Messages::from_ptr(msgs, self)) + <Query as QueryExt>::search_messages(self) } pub fn count_messages(self: &Self) -> Result<u32> { @@ -75,12 +79,8 @@ impl<'d> Query<'d> { } pub fn search_threads<'q>(self: &'d Self) -> Result<Threads<'q, Self>> { - let mut thrds = ptr::null_mut(); - try!( - unsafe { ffi::notmuch_query_search_threads(self.handle.ptr, &mut thrds,) }.as_result() - ); + <Query as QueryExt>::search_threads(self) - Ok(Threads::from_ptr(thrds, self)) } pub fn count_threads(self: &Self) -> Result<u32> { @@ -91,5 +91,37 @@ impl<'d> Query<'d> { } } +pub trait QueryExt<'d>{ + fn search_threads<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Threads<'q, Query<'d>>>; + fn search_messages<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Messages<'q, Query<'d>>>; + +} + +impl<'d> QueryExt<'d> for Query<'d>{ + fn search_threads<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Threads<'q, Query<'d>>>{ + let queryref = query.into(); + + let mut thrds = ptr::null_mut(); + try!( + unsafe { ffi::notmuch_query_search_threads(queryref.handle.ptr, &mut thrds) }.as_result() + ); + + Ok(Threads::from_ptr(thrds, Supercow::phantom(queryref))) + } + + fn search_messages<'q, Q: Into<Supercow<'q, Query<'d>>>>(query: Q) -> Result<Messages<'q, Query<'d>>>{ + let queryref = query.into(); + + let mut msgs = ptr::null_mut(); + try!( + unsafe { ffi::notmuch_query_search_messages(queryref.handle.ptr, &mut msgs) }.as_result() + ); + + Ok(Messages::from_ptr(msgs, Supercow::phantom(queryref))) + } + +} + + unsafe impl<'d> Send for Query<'d> {} unsafe impl<'d> Sync for Query<'d> {} |
