From 1a9fc47a805d2dc86b163d1ce524d88dee076893 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 3 Jan 2020 11:46:00 +0100 Subject: Add MailIterator for iterating over entries that are mails Signed-off-by: Matthias Beyer --- lib/domain/libimagmail/src/iter.rs | 83 ++++++++++++++++++++++++++++++++++++++ lib/domain/libimagmail/src/lib.rs | 1 + 2 files changed, 84 insertions(+) create mode 100644 lib/domain/libimagmail/src/iter.rs (limited to 'lib') 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 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>; + + fn next(&mut self) -> Option { + 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; -- cgit v1.2.3