diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-11-04 19:42:46 +0100 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-11-04 19:42:46 +0100 |
| commit | 08aa3be11a25c8ccbdf4806a4a08c9267cae9140 (patch) | |
| tree | 45a553300dc3cdf77f5e94091a7038139f651610 | |
| parent | f2e74aad70b3dceab9c9d78db333f6d71066cbff (diff) | |
| download | mail-08aa3be11a25c8ccbdf4806a4a08c9267cae9140.tar.gz | |
start adding 'Ext' threads to enable more flexible supercow api
| -rw-r--r-- | src/database.rs | 22 | ||||
| -rw-r--r-- | src/directory.rs | 30 | ||||
| -rw-r--r-- | src/lib.rs | 2 | ||||
| -rw-r--r-- | src/messages.rs | 12 | ||||
| -rw-r--r-- | src/query.rs | 66 | ||||
| -rw-r--r-- | src/threads.rs | 9 | ||||
| -rw-r--r-- | tests/main.rs | 15 |
7 files changed, 128 insertions, 28 deletions
diff --git a/src/database.rs b/src/database.rs index 6390c15..67ed3da 100644 --- a/src/database.rs +++ b/src/database.rs @@ -10,6 +10,7 @@ use ffi; use utils::ToStr; use Directory; use Query; +use query::QueryPtr; use Tags; use TagsOwner; @@ -36,6 +37,17 @@ impl Drop for DatabasePtr { } } +impl DatabasePtr { + + pub(crate) fn create_query(&self, query_string: &str) -> Result<QueryPtr> { + let query_str = CString::new(query_string).unwrap(); + + let query = unsafe { ffi::notmuch_query_create(self.ptr, query_str.as_ptr()) }; + + Ok(QueryPtr { ptr: query }) + } +} + #[derive(Debug)] pub struct Database { pub(crate) handle: DatabasePtr, @@ -74,7 +86,7 @@ impl Database { Ok(()) } - + pub fn compact<P: AsRef<Path>, F: FnMut(&str)>( path: &P, backup_path: Option<&P>, @@ -214,11 +226,9 @@ impl Database { } pub fn create_query<'d>(&'d self, query_string: &str) -> Result<Query<'d>> { - let query_str = CString::new(query_string).unwrap(); - - let query = unsafe { ffi::notmuch_query_create(self.handle.ptr, query_str.as_ptr()) }; - - Ok(Query::from_ptr(query, self)) + self.handle.create_query(query_string).map(move |handle|{ + Query::from_handle(handle, self) + }) } pub fn all_tags<'d>(&'d self) -> Result<Tags<'d, Self>> { diff --git a/src/directory.rs b/src/directory.rs index 9bdae2d..c179eb8 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -1,6 +1,10 @@ +use std::ffi::{CStr, CString}; use std::ops::Drop; -use supercow::Phantomcow; +use std::path::Path; +use std::ptr; +use supercow::{Supercow, Phantomcow}; +use error::Result; use ffi; use Database; use Filenames; @@ -36,6 +40,30 @@ impl<'d> Directory<'d> { } } + 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) }, @@ -27,7 +27,7 @@ pub use error::Error; pub use filenames::{Filenames, FilenamesOwner}; pub use message::{Message, MessageOwner}; pub use messages::{Messages, MessagesOwner}; -pub use query::Query; +pub use query::{Query, QueryExt}; pub use tags::{Tags, TagsOwner}; pub use thread::{Thread, ThreadOwner}; pub use threads::{Threads, ThreadsOwner}; diff --git a/src/messages.rs b/src/messages.rs index 08a9cf3..8863bef 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -35,7 +35,7 @@ pub struct Messages<'o, Owner: MessagesOwner + 'o> { } impl<'o, Owner: MessagesOwner + 'o> Messages<'o, Owner> { - pub fn from_ptr<O: Into<Phantomcow<'o, Owner>>>( + pub(crate) fn from_ptr<O: Into<Phantomcow<'o, Owner>>>( ptr: *mut ffi::notmuch_messages_t, owner: O, ) -> Messages<'o, Owner> { @@ -44,6 +44,16 @@ impl<'o, Owner: MessagesOwner + 'o> Messages<'o, Owner> { marker: owner.into(), } } + + pub(crate) fn from_handle<O: Into<Phantomcow<'o, Owner>>>( + handle: MessagesPtr, + owner: O, + ) -> Messages<'o, Owner> { + Messages { + handle, + marker: owner.into(), + } + } } impl<'o, Owner: MessagesOwner + 'o> MessageOwner for Messages<'o, Owner> {} 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> {} diff --git a/src/threads.rs b/src/threads.rs index 359569e..6dfa047 100644 --- a/src/threads.rs +++ b/src/threads.rs @@ -38,6 +38,15 @@ impl<'o, Owner: ThreadsOwner + 'o> Threads<'o, Owner> { marker: owner.into(), } } + pub(crate) fn from_handle<O: Into<Phantomcow<'o, Owner>>>( + handle: ThreadsPtr, + owner: O, + ) -> Threads<'o, Owner> { + Threads { + handle, + marker: owner.into(), + } + } } impl<'s, 'o: 's, Owner: ThreadsOwner + 'o> StreamingIterator<'s, Thread<'s, Self>> diff --git a/tests/main.rs b/tests/main.rs index d790e21..fd7e511 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -1,6 +1,8 @@ extern crate notmuch; extern crate dirs; +use std::sync::Arc; + use notmuch::StreamingIterator; fn main() { @@ -16,10 +18,19 @@ fn main() { let rev = db.revision(); println!("db revision: {:?}", rev); } - - let query = db.create_query(&"".to_string()).unwrap(); + let query = { + let dbr = Arc::new(db); + + notmuch::Query::create(dbr.clone(), &"".to_string()).unwrap() + }; + + let mut threads = query.search_threads().unwrap(); + + + //let mut threads = db.create_query(&"".to_string()).unwrap().search_threads().unwrap(); + while let Some(thread) = threads.next() { println!("thread {:?} {:?}", thread.subject(), thread.authors()); } |
