diff options
| author | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-03-23 03:51:04 +0100 |
|---|---|---|
| committer | Dirk Van Haerenborgh <vhdirk@gmail.com> | 2018-03-23 03:51:04 +0100 |
| commit | bc968e0da07e8e80e19eec1e8efbdb347746d6e2 (patch) | |
| tree | 2fa7c6f0121f76f4b8ad321bd6f3fc0d375b0f2f | |
| parent | 9c51895af08f367c112e77634cb534ecdc430bbf (diff) | |
| download | mail-bc968e0da07e8e80e19eec1e8efbdb347746d6e2.tar.gz | |
proper tags iterator
| -rw-r--r-- | src/database.rs | 14 | ||||
| -rw-r--r-- | src/directory.rs | 1 | ||||
| -rw-r--r-- | src/ffi.rs | 76 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/message.rs | 0 | ||||
| -rw-r--r-- | src/messages.rs | 12 | ||||
| -rw-r--r-- | src/query.rs | 24 | ||||
| -rw-r--r-- | src/tags.rs | 61 | ||||
| -rw-r--r-- | src/threads.rs | 32 |
9 files changed, 191 insertions, 33 deletions
diff --git a/src/database.rs b/src/database.rs index 85567f5..85ddcfb 100644 --- a/src/database.rs +++ b/src/database.rs @@ -16,6 +16,7 @@ use utils::{ use directory::Directory; use query::Query; +use tags::Tags; use ffi; @@ -197,6 +198,19 @@ impl Database { Ok(Query::new(query)) } + pub fn all_tags(&self) -> Result<Tags> { + + let mut tags = ptr::null_mut(); + unsafe { + tags = ffi::notmuch_database_get_all_tags(self.0); + } + + Ok(Tags::new(tags)) + } + + + + } impl ops::Drop for Database { diff --git a/src/directory.rs b/src/directory.rs index 3d9de6e..1d75f35 100644 --- a/src/directory.rs +++ b/src/directory.rs @@ -11,6 +11,7 @@ use database; use ffi; +#[derive(Debug)] pub struct Directory<'d>( *mut ffi::notmuch_directory_t, marker::PhantomData<&'d mut database::Database>, @@ -134,6 +134,8 @@ notmuch_enum! { #[repr(C)] pub struct notmuch_filenames_t(c_void); #[repr(C)] pub struct notmuch_message_properties_t(c_void); #[repr(C)] pub struct notmuch_config_list_t(c_void); +#[repr(C)] pub struct notmuch_indexopts_t(c_void); + pub type notmuch_compact_status_cb_t = extern "C" fn(message: *const c_char, closure: *mut c_void); pub type notmuch_database_upgrade_cb_t = extern "C" fn(closure: *mut c_void, progress: c_double); @@ -429,11 +431,13 @@ extern { directory: *mut *mut notmuch_directory_t, ) -> notmuch_status_t; - /// Add a new message to the given notmuch database or associate an - /// additional filename with an existing message. + /// Add a message file to a database, indexing it for retrieval by + /// future searches. If a message already exists with the same message + /// ID as the specified file, their indexes will be merged, and this + /// new filename will also be associated with the existing message. /// /// Here, 'filename' should be a path relative to the path of - /// 'database' (see `notmuch_database_get_path`), or else should be an + /// 'database' (see notmuch_database_get_path), or else should be an /// absolute filename with initial components that match the path of /// 'database'. /// @@ -443,40 +447,61 @@ extern { /// entire contents of the file. /// /// If another message with the same message ID already exists in the - /// database, rather than creating a new message, this adds 'filename' - /// to the list of the filenames for the existing message. + /// database, rather than creating a new message, this adds the search + /// terms from the identified file to the existing message's index, and + /// adds 'filename' to the list of filenames known for the message. + /// + /// The 'indexopts' parameter can be NULL (meaning, use the indexing + /// defaults from the database), or can be an explicit choice of + /// indexing options that should govern the indexing of this specific + /// 'filename'. /// /// If 'message' is not NULL, then, on successful return - /// (notmuch_status_t::SUCCESS or `notmuch_status_t::DUPLICATE_MESSAGE_ID`) '*message' + /// (NOTMUCH_STATUS_SUCCESS or NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) '*message' /// will be initialized to a message object that can be used for things /// such as adding tags to the just-added message. The user should call - /// `notmuch_message_destroy` when done with the message. On any failure + /// notmuch_message_destroy when done with the message. On any failure /// '*message' will be set to NULL. /// /// Return value: /// - /// * `notmuch_status_t::SUCCESS`: Message successfully added to database. + /// NOTMUCH_STATUS_SUCCESS: Message successfully added to database. /// - /// * `notmuch_status_t::XAPIAN_EXCEPTION`: A Xapian exception occurred, - /// message not added. + /// NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred, + /// message not added. /// - /// * `notmuch_status_t::DUPLICATE_MESSAGE_ID`: Message has the same message - /// ID as another message already in the database. The new - /// filename was successfully added to the message in the database - /// (if not already present) and the existing message is returned. + /// NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: Message has the same message + /// ID as another message already in the database. The new + /// filename was successfully added to the message in the database + /// (if not already present) and the existing message is returned. /// - /// * `notmuch_status_t::FILE_ERROR`: an error occurred trying to open the - /// file, (such as permission denied, or file not found, - /// etc.). Nothing added to the database. + /// NOTMUCH_STATUS_FILE_ERROR: an error occurred trying to open the + /// file, (such as permission denied, or file not found, + /// etc.). Nothing added to the database. /// - /// * `notmuch_status_t::FILE_NOT_EMAIL`: the contents of filename don't look - /// like an email message. Nothing added to the database. + /// NOTMUCH_STATUS_FILE_NOT_EMAIL: the contents of filename don't look + /// like an email message. Nothing added to the database. /// - /// * `notmuch_status_t::READ_ONLY_DATABASE`: Database was opened in read-only - /// mode so no message can be added. + /// NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only + /// mode so no message can be added. /// - /// * `notmuch_status_t::UPGRADE_REQUIRED`: The caller must upgrade the - /// database to use this function. + /// NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the + /// database to use this function. + /// + /// @since libnotmuch 5.1 (notmuch 0.26) + pub fn notmuch_database_index_file( + database: *mut notmuch_database_t, + filename: *const c_char, + indexopts: *mut notmuch_indexopts_t, + message: *mut *mut notmuch_message_t, + ) -> notmuch_status_t; + + + /// Deprecated alias for notmuch_database_index_file called with + /// NULL indexopts. + /// + /// @deprecated Deprecated as of libnotmuch 5.1 (notmuch 0.26). Please + /// use notmuch_database_index_file instead. pub fn notmuch_database_add_message( database: *mut notmuch_database_t, filename: *const c_char, @@ -750,13 +775,10 @@ extern { /// /// If a Xapian exception occurs this function will return NULL. /// - /// @since libnotmuch 4.2 (notmuch 0.20) + /// @since libnotmuch 5 (notmuch 0.25) pub fn notmuch_query_search_messages(query: *mut notmuch_query_t, out: *mut *mut notmuch_messages_t) -> notmuch_status_t; - pub fn notmuch_query_search_messages_st(query: *mut notmuch_query_t, - out: *mut *mut notmuch_messages_t) - -> notmuch_status_t; /// Destroy a `notmuch_query_t` along with any associated resources. /// @@ -11,9 +11,13 @@ pub mod database; pub mod directory; pub mod query; pub mod messages; +pub mod tags; +pub mod threads; pub use database::Database; pub use query::Query; pub use messages::Messages; +pub use tags::Tags; +pub use threads::Threads; pub use ffi::DatabaseMode; diff --git a/src/message.rs b/src/message.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/message.rs diff --git a/src/messages.rs b/src/messages.rs index 5144fb9..c21c701 100644 --- a/src/messages.rs +++ b/src/messages.rs @@ -13,20 +13,22 @@ use Database; use Query; #[derive(Debug)] -pub struct Messages<'q>( +pub struct Messages<'q, 'd:'q>( // TODO: is this lifetime specifier correct? + // query may outlive messages. pub(crate) *mut ffi::notmuch_messages_t, - marker::PhantomData<&'q Query<'q>> + marker::PhantomData<&'q Query<'d>> ); -impl<'q> NewFromPtr<*mut ffi::notmuch_messages_t> for Messages<'q> { - fn new(ptr: *mut ffi::notmuch_messages_t) -> Messages<'q> { +impl<'q, 'd:'q> NewFromPtr<*mut ffi::notmuch_messages_t> for Messages<'q, 'd> { + fn new(ptr: *mut ffi::notmuch_messages_t) -> Messages<'q, 'd> { Messages(ptr, marker::PhantomData) } } -impl<'q> ops::Drop for Messages<'q> { + +impl<'q, 'd:'q> ops::Drop for Messages<'q, 'd> { fn drop(&mut self) { unsafe { ffi::notmuch_messages_destroy(self.0) diff --git a/src/query.rs b/src/query.rs index 654d4c6..6d7da64 100644 --- a/src/query.rs +++ b/src/query.rs @@ -13,7 +13,7 @@ use utils::{ }; use Database; use Messages; - +use ffi::Sort; #[derive(Debug)] pub struct Query<'d>( @@ -27,6 +27,28 @@ impl<'d> Query<'d> { 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>> { diff --git a/src/tags.rs b/src/tags.rs new file mode 100644 index 0000000..9eb2779 --- /dev/null +++ b/src/tags.rs @@ -0,0 +1,61 @@ +use std::{ + ops, + marker, + iter +}; + +use std::ffi::{ + CString, + CStr +}; + +use utils::{ + NewFromPtr, +}; + +use database; +use ffi; + +#[derive(Debug)] +pub struct Tags<'d>( + *mut ffi::notmuch_tags_t, + marker::PhantomData<&'d mut database::Database>, +); + +impl<'d> NewFromPtr<*mut ffi::notmuch_tags_t> for Tags<'d> { + fn new(ptr: *mut ffi::notmuch_tags_t) -> Tags<'d> { + Tags(ptr, marker::PhantomData) + } +} + +impl<'d> ops::Drop for Tags<'d> { + fn drop(&mut self) { + unsafe { + ffi::notmuch_tags_destroy(self.0) + }; + } +} + +impl<'d> iter::Iterator for Tags<'d> { + type Item = String; + + fn next(&mut self) -> Option<Self::Item> { + + let valid = unsafe { + ffi::notmuch_tags_valid(self.0) + }; + + if valid == 0{ + return None + } + + let ctag = unsafe { + ffi::notmuch_tags_move_to_next(self.0); + CStr::from_ptr(ffi::notmuch_tags_get(self.0)) + }; + + Some(ctag.to_str().unwrap().to_string()) + } + + +} diff --git a/src/threads.rs b/src/threads.rs new file mode 100644 index 0000000..75f4549 --- /dev/null +++ b/src/threads.rs @@ -0,0 +1,32 @@ +use std::{ + ops, + marker, +}; + +use utils::{ + NewFromPtr, +}; + +use database; + +use ffi; + +#[derive(Debug)] +pub struct Threads<'d>( + *mut ffi::notmuch_threads_t, + marker::PhantomData<&'d mut database::Database>, +); + +impl<'d> NewFromPtr<*mut ffi::notmuch_threads_t> for Threads<'d> { + fn new(ptr: *mut ffi::notmuch_threads_t) -> Threads<'d> { + Threads(ptr, marker::PhantomData) + } +} + +impl<'d> ops::Drop for Threads<'d> { + fn drop(&mut self) { + unsafe { + ffi::notmuch_threads_destroy(self.0) + }; + } +} |
