summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-01-03 11:46:00 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-01-03 11:46:00 +0100
commit1a9fc47a805d2dc86b163d1ce524d88dee076893 (patch)
tree065be876937999e6919a15b635eb98906ae7d60a /lib
parent0122e59bb073a4bee0e74f6d1e09c32e077dc249 (diff)
Add MailIterator for iterating over entries that are mails
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/domain/libimagmail/src/iter.rs83
-rw-r--r--lib/domain/libimagmail/src/lib.rs1
2 files changed, 84 insertions, 0 deletions
diff --git a/lib/domain/libimagmail/src/iter.rs b/lib/domain/libimagmail/src/iter.rs
new file mode 100644
index 00000000..bba6b690
--- /dev/null
+++ b/lib/domain/libimagmail/src/iter.rs
@@ -0,0 +1,83 @@
+//
+// imag - the personal information management suite for the commandline
+// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; version
+// 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+//
+
+use failure::Fallible as Result;
+use failure::ResultExt;
+use failure::Error;
+use failure::err_msg;
+
+use libimagstore::iter::get::StoreGetIterator;
+use libimagstore::store::FileLockEntry;
+
+use crate::mail::Mail;
+
+pub struct MailIterator<'a> {
+ inner: StoreGetIterator<'a>,
+ ignore_ungetable: bool,
+}
+
+impl<'a> MailIterator<'a> {
+ pub fn new(sgi: StoreGetIterator<'a>) -> Self {
+ MailIterator { inner: sgi, ignore_ungetable: true }
+ }
+
+ pub fn ignore_ungetable(mut self, b: bool) -> Self {
+ self.ignore_ungetable = b;
+ self
+ }
+}
+
+pub trait IntoMailIterator<'a> {
+ fn into_mail_iterator(self) -> MailIterator<'a>;
+}
+
+impl<'a> IntoMailIterator<'a> for StoreGetIterator<'a> {
+ fn into_mail_iterator(self) -> MailIterator<'a> {
+ MailIterator::new(self)
+ }
+}
+
+impl<'a> Iterator for MailIterator<'a> {
+ type Item = Result<FileLockEntry<'a>>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ while let Some(n) = self.inner.next() {
+ match n {
+ Ok(Some(fle)) => {
+ match fle.is_mail().context("Checking whether entry is a Mail").map_err(Error::from) {
+ Err(e) => return Some(Err(e)),
+ Ok(true) => return Some(Ok(fle)),
+ Ok(false) => continue,
+ }
+ },
+
+ Ok(None) => if self.ignore_ungetable {
+ continue
+ } else {
+ return Some(Err(err_msg("Failed to get one entry")))
+ },
+
+ Err(e) => return Some(Err(e)),
+ }
+ }
+
+ None
+ }
+}
+
diff --git a/lib/domain/libimagmail/src/lib.rs b/lib/domain/libimagmail/src/lib.rs
index b3d5785b..32f1da64 100644
--- a/lib/domain/libimagmail/src/lib.rs
+++ b/lib/domain/libimagmail/src/lib.rs
@@ -55,6 +55,7 @@ module_entry_path_mod!("mail");
pub mod config;
pub mod hasher;
+pub mod iter;
pub mod mail;
pub mod mid;
pub mod store;