diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-12-30 12:41:16 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-01-03 11:10:59 +0100 |
commit | d5cbf655a3edf86594bd5aac539b527c596675b2 (patch) | |
tree | d74a42382ca31dddd1bbd5b45821aae2fb4a0877 | |
parent | b44778a6c835c86a8c1ba7c373aaac08db4557a2 (diff) |
Add function to get flags
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/domain/libimagmail/src/mail.rs | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index 1d2f5a2d..6e5f2876 100644 --- a/lib/domain/libimagmail/src/mail.rs +++ b/lib/domain/libimagmail/src/mail.rs @@ -17,6 +17,8 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // +use std::str::FromStr; + use failure::Fallible as Result; use failure::ResultExt; use failure::Error; @@ -29,6 +31,8 @@ use libimagentryref::reference::Config as RefConfig; use libimagentryref::reference::{Ref, RefFassade}; use crate::mid::MessageId; +use crate::mailflags::MailFlag; +use crate::hasher::MailHasher; provide_kindflag_path!(pub IsMail, "mail.is_mail"); @@ -40,6 +44,8 @@ pub trait Mail : RefFassade { fn get_subject(&self, refconfig: &RefConfig) -> Result<Option<String>>; fn get_message_id(&self, refconfig: &RefConfig) -> Result<Option<MessageId>>; fn get_in_reply_to(&self, refconfig: &RefConfig) -> Result<Option<MessageId>>; + + fn flags(&self, refconfig: &RefConfig) -> Result<Vec<MailFlag>>; } impl Mail for Entry { @@ -51,7 +57,6 @@ impl Mail for Entry { /// Get a value of a single field of the mail file fn get_field(&self, refconfig: &RefConfig, field: &str) -> Result<Option<String>> { use std::fs::read_to_string; - use crate::hasher::MailHasher; debug!("Getting field in mail: {:?}", field); let mail_file_location = self.as_ref_with_hasher::<MailHasher>().get_path(refconfig)?; @@ -134,6 +139,31 @@ impl Mail for Entry { .map(|o| o.map(crate::util::strip_message_delimiters).map(MessageId::from)) } + /// Get the flags of the message + fn flags(&self, refconfig: &RefConfig) -> Result<Vec<MailFlag>> { + let path = self.as_ref_with_hasher::<MailHasher>().get_path(refconfig)?; + + if !path.exists() { + return Err(format_err!("Path {} does not exist", path.display())) + } + + { + // Now parse mail flags + path.to_str() + .ok_or_else(|| format_err!("Path is not UTF-8: {}", path.display()))? + .split("2,") + .map(String::from) + .collect::<Vec<String>>() + .split_last() + .ok_or_else(|| format_err!("Splitting path into prefix and flags failed: {}", path.display()))? + .0 + .chars() + .map(|c| c.to_string()) + .map(|c| MailFlag::from_str(&c)) + .collect::<Result<Vec<_>>>() + } + } + } #[derive(Debug)] |