summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bin/domain/imag-mail/Cargo.toml5
-rw-r--r--bin/domain/imag-mail/src/lib.rs190
-rw-r--r--bin/domain/imag-mail/src/ui.rs50
-rw-r--r--bin/domain/imag-mail/src/util.rs87
-rw-r--r--imagrc.toml1
-rw-r--r--lib/core/libimagstore/src/store.rs12
-rw-r--r--lib/domain/libimagmail/src/lib.rs1
-rw-r--r--lib/domain/libimagmail/src/mail.rs68
-rw-r--r--lib/domain/libimagmail/src/mailflags.rs93
9 files changed, 424 insertions, 83 deletions
diff --git a/bin/domain/imag-mail/Cargo.toml b/bin/domain/imag-mail/Cargo.toml
index ed88d064..fd871089 100644
--- a/bin/domain/imag-mail/Cargo.toml
+++ b/bin/domain/imag-mail/Cargo.toml
@@ -22,8 +22,10 @@ maintenance = { status = "actively-developed" }
[dependencies]
log = "0.4.6"
failure = "0.1.5"
-indoc = "0.3.3"
resiter = "0.4"
+handlebars = "2"
+walkdir = "2"
+rayon = "1"
libimagrt = { version = "0.10.0", path = "../../../lib/core/libimagrt" }
libimagstore = { version = "0.10.0", path = "../../../lib/core/libimagstore" }
@@ -31,6 +33,7 @@ libimagerror = { version = "0.10.0", path = "../../../lib/core/libimagerror"
libimagmail = { version = "0.10.0", path = "../../../lib/domain/libimagmail" }
libimagutil = { version = "0.10.0", path = "../../../lib/etc/libimagutil" }
libimagentryref = { version = "0.10.0", path = "../../../lib/entry/libimagentryref" }
+libimaginteraction = { version = "0.10.0", path = "../../../lib/etc/libimaginteraction" }
[dependencies.clap]
version = "2.33.0"
diff --git a/bin/domain/imag-mail/src/lib.rs b/bin/domain/imag-mail/src/lib.rs
index 0dd7f2bb..d069ee0e 100644
--- a/bin/domain/imag-mail/src/lib.rs
+++ b/bin/domain/imag-mail/src/lib.rs
@@ -38,8 +38,10 @@ extern crate clap;
#[macro_use] extern crate log;
#[macro_use] extern crate failure;
extern crate toml_query;
-#[macro_use] extern crate indoc;
extern crate resiter;
+extern crate handlebars;
+extern crate walkdir;
+extern crate rayon;
extern crate libimagrt;
extern crate libimagmail;
@@ -47,6 +49,7 @@ extern crate libimagerror;
extern crate libimagstore;
extern crate libimagutil;
extern crate libimagentryref;
+extern crate libimaginteraction;
use std::io::Write;
use std::path::PathBuf;
@@ -57,21 +60,23 @@ use failure::Error;
use toml_query::read::TomlValueReadTypeExt;
use clap::App;
use resiter::AndThen;
+use resiter::Filter;
use resiter::IterInnerOkOrElse;
+use resiter::Map;
+use rayon::prelude::*;
-use libimagmail::mail::Mail;
use libimagmail::store::MailStore;
-use libimagmail::util;
-use libimagentryref::reference::{Ref, RefFassade};
+use libimagmail::mail::Mail;
use libimagentryref::util::get_ref_config;
+use libimagentryref::reference::Config as RefConfig;
use libimagrt::runtime::Runtime;
use libimagrt::application::ImagApplication;
use libimagutil::info_result::*;
-use libimagstore::store::FileLockEntry;
use libimagstore::storeid::StoreIdIterator;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
mod ui;
+mod util;
/// Marker enum for implementing ImagApplication on
///
@@ -81,8 +86,10 @@ pub enum ImagMail {}
impl ImagApplication for ImagMail {
fn run(rt: Runtime) -> Result<()> {
match rt.cli().subcommand_name().ok_or_else(|| err_msg("No subcommand called"))? {
+ "scan" => scan(&rt),
"import-mail" => import_mail(&rt),
"list" => list(&rt),
+ "unread" => unread(&rt),
"mail-store" => mail_store(&rt),
other => {
debug!("Unknown command");
@@ -112,6 +119,64 @@ impl ImagApplication for ImagMail {
}
}
+fn scan(rt: &Runtime) -> Result<()> {
+ let collection_name = get_ref_collection_name(rt)?;
+ let refconfig = get_ref_config(rt, "imag-mail")?;
+ let scmd = rt.cli().subcommand_matches("scan").unwrap();
+
+ /// Helper function to get an Iterator for all files from one PathBuf
+ fn walk_pathes(path: PathBuf) -> impl Iterator<Item = Result<PathBuf>> {
+ walkdir::WalkDir::new(path)
+ .follow_links(false)
+ .into_iter()
+ .filter_ok(|entry| entry.file_type().is_file())
+ .map_err(Error::from)
+ .map_ok(|entry| entry.into_path())
+ .inspect(|e| trace!("Processing = {:?}", e))
+ }
+
+ /// Helper function to process an iterator over Result<PathBuf> and create store entries for
+ /// each path in the iterator
+ fn process_iter(i: &mut dyn Iterator<Item = Result<PathBuf>>, rt: &Runtime, collection_name: &str, refconfig: &RefConfig) -> Result<()> {
+ let scmd = rt.cli().subcommand_matches("scan").unwrap();
+ i.and_then_ok(|path| {
+ if scmd.is_present("ignore-existing-ids") {
+ rt.store().retrieve_mail_from_path(path, collection_name, refconfig, true)
+ } else {
+ rt.store().create_mail_from_path(path, collection_name, refconfig)
+ }
+ })
+ .and_then_ok(|e| rt.report_touched(e.get_location()).map_err(Error::from))
+ .collect::<Result<Vec<_>>>()
+ .map(|_| ())
+ }
+
+ let pathes = scmd.values_of("path")
+ .unwrap() // enforced by clap
+ .map(PathBuf::from)
+ .collect::<Vec<_>>();
+
+ if scmd.is_present("scan-parallel") {
+ debug!("Fetching pathes in parallel");
+ let mut i = pathes.into_par_iter()
+ .map(|path| walk_pathes(path).collect::<Result<_>>())
+ .collect::<Result<Vec<PathBuf>>>()?
+ .into_iter()
+ .map(Ok);
+
+ debug!("Processing pathes");
+ process_iter(&mut i, rt, &collection_name, &refconfig)
+ } else {
+ debug!("Fetching pathes not in parallel");
+ let mut i = pathes
+ .into_iter()
+ .map(walk_pathes)
+ .flatten();
+
+ debug!("Processing pathes");
+ process_iter(&mut i, rt, &collection_name, &refconfig)
+ }
+}
fn import_mail(rt: &Runtime) -> Result<()> {
let collection_name = get_ref_collection_name(rt)?;
@@ -141,77 +206,44 @@ fn import_mail(rt: &Runtime) -> Result<()> {
}
fn list(rt: &Runtime) -> Result<()> {
- let refconfig = get_ref_config(rt, "imag-mail")?;
- let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe via clap
- let print_content = scmd.is_present("list-read");
+ let refconfig = get_ref_config(rt, "imag-mail")?;
+ let scmd = rt.cli().subcommand_matches("list").unwrap(); // safe via clap
+ let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?;
+ let mut i = 0;
- if print_content {
- // TODO: Check whether workaround with "{}" is still necessary when updating "indoc"
- warn!("{}", indoc!(r#"You requested to print the content of the mail as well.
- We use the 'mailparse' crate underneath, but its implementation is nonoptimal.
- Thus, the content might be printed as empty (no text in the email)
- This is not reliable and might be wrong."#));
+ if rt.ids_from_stdin() {
+ let iter = rt
+ .ids::<crate::ui::PathProvider>()?
+ .ok_or_else(|| err_msg("No ids supplied"))?
+ .into_iter()
+ .map(Ok);
- // TODO: Fix above.
+ StoreIdIterator::new(Box::new(iter))
+ } else {
+ rt.store()
+ .all_mails()?
+ .into_storeid_iter()
}
-
- // TODO: Implement lister type in libimagmail for this
- //
- // Optimization: Pass refconfig here instead of call get_ref_config() in lister function. This
- // way we do not call get_ref_config() multiple times.
- fn list_mail<'a>(rt: &Runtime,
- refconfig: &::libimagentryref::reference::Config,
- m: &FileLockEntry<'a>,
- print_content: bool) -> Result<()> {
-
- let id = match m.get_message_id(&refconfig)? {
- Some(f) => f.into(),
- None => "<no id>".to_owned(),
- };
-
- let from = match m.get_from(&refconfig)? {
- Some(f) => f,
- None => "<no from>".to_owned(),
- };
-
- let to = match m.get_to(&refconfig)? {
- Some(f) => f,
- None => "<no to>".to_owned(),
- };
-
- let subject = match m.get_subject(&refconfig)? {
- Some(f) => f,
- None => "<no subject>".to_owned(),
- };
-
- if print_content {
- use libimagmail::hasher::MailHasher;
-
- let content = m.as_ref_with_hasher::<MailHasher>()
- .get_path(&refconfig)
- .and_then(util::get_mail_text_content)?;
-
- writeln!(rt.stdout(),
- "Mail: {id}\nFrom: {from}\nTo: {to}\n{subj}\n---\n{content}\n---\n",
- from = from,
- id = id,
- subj = subject,
- to = to,
- content = content
- )?;
- } else {
- writeln!(rt.stdout(),
- "Mail: {id}\nFrom: {from}\nTo: {to}\n{subj}\n",
- from = from,
- id = id,
- subj = subject,
- to = to
- )?;
- }
-
+ .inspect(|id| debug!("Found: {:?}", id))
+ .into_get_iter(rt.store())
+ .map_inner_ok_or_else(|| err_msg("Did not find one entry"))
+ .and_then_ok(|m| {
+ let data = util::build_data_object_for_handlebars(i, &m, &refconfig)?;
+ let s = list_format.render("format", &data)?;
+ writeln!(rt.stdout(), "{}", s)?;
rt.report_touched(m.get_location())?;
+ i += 1;
Ok(())
- }
+ })
+ .collect::<Result<Vec<_>>>()
+ .map(|_| ())
+}
+
+fn unread(rt: &Runtime) -> Result<()> {
+ let refconfig = get_ref_config(rt, "imag-mail")?;
+ let scmd = rt.cli().subcommand_matches("unread").unwrap(); // safe via clap
+ let list_format = util::get_mail_print_format("mail.list_format", rt, &scmd)?;
+ let mut i = 0;
if rt.ids_from_stdin() {
let iter = rt
@@ -222,14 +254,22 @@ fn list(rt: &Runtime) -> Result<()> {
StoreIdIterator::new(Box::new(iter))
} else {
- rt.store()
- .all_mails()?
- .into_storeid_iter()
+ rt.store().all_mails()?.into_storeid_iter()
}
.inspect(|id| debug!("Found: {:?}", id))
.into_get_iter(rt.store())
.map_inner_ok_or_else(|| err_msg("Did not find one entry"))
- .and_then_ok(|m| list_mail(&rt, &refconfig, &m, print_content))
+ .and_then_ok(|m| {
+ if !m.is_seen(&refconfig)? {
+ let data = util::build_data_object_for_handlebars(i, &m, &refconfig)?;
+ let s = list_format.render("format", &data)?;
+ writeln!(rt.stdout(), "{}", s)?;
+ rt.report_touched(m.get_location())?;
+ i += 1;
+ }
+
+ Ok(())
+ })
.collect::<Result<Vec<_>>>()
.map(|_| ())
}
diff --git a/bin/domain/imag-mail/src/ui.rs b/bin/domain/imag-mail/src/ui.rs
index e1fa88e6..324fabb4 100644
--- a/bin/domain/imag-mail/src/ui.rs
+++ b/bin/domain/imag-mail/src/ui.rs
@@ -47,17 +47,36 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.value_name("PATH"))
)
- .subcommand(SubCommand::with_name("list")
- .about("List all stored references to mails")
+ .subcommand(SubCommand::with_name("scan")
+ .about("Scan a directory for mails")
.version("0.1")
+ .arg(Arg::with_name("ignore-existing-ids")
+ .long("ignore-existing")
+ .short("I")
+ .takes_value(false)
+ .required(false)
+ .help("Ignore errors that might occur when store entries exist already"))
- .arg(Arg::with_name("list-read")
- .long("read")
- .short("r")
+ .arg(Arg::with_name("scan-parallel")
+ .long("parallel")
.takes_value(false)
.required(false)
.multiple(false)
- .help("Print the textual content of the mail itself as well"))
+ .help("Scan with multiple threads. Might be faster, but might slow down other tasks"))
+
+ .arg(Arg::with_name("path")
+ .index(1)
+ .takes_value(true)
+ .multiple(true)
+ .required(true)
+ .validator(libimagutil::cli_validators::is_directory)
+ .value_name("DIR")
+ .help("Path to the directory containing mails"))
+ )
+
+ .subcommand(SubCommand::with_name("list")
+ .about("List all stored references to mails")
+ .version("0.1")
.arg(Arg::with_name("list-id")
.index(1)
@@ -66,6 +85,25 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.multiple(true)
.help("The ids of the mails to list information for"))
+ .arg(Arg::with_name("format")
+ .long("format")
+ .takes_value(true)
+ .required(false)
+ .multiple(false)
+ .help("The format to list the mails with"))
+ )
+
+ .subcommand(SubCommand::with_name("unread")
+ .about("Show unread mail")
+ .version("0.1")
+
+ .arg(Arg::with_name("unread-read")
+ .long("read")
+ .short("r")
+ .takes_value(false)
+ .required(false)
+ .multiple(false)
+ .help("Print the textual content of the mail itself as well"))
)
.subcommand(SubCommand::with_name("mail-store")
diff --git a/bin/domain/imag-mail/src/util.rs b/bin/domain/imag-mail/src/util.rs
new file mode 100644
index 00000000..77d6b5a5
--- /dev/null
+++ b/bin/domain/imag-mail/src/util.rs
@@ -0,0 +1,87 @@
+//
+// 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 std::collections::BTreeMap;
+
+use clap::ArgMatches;
+use failure::Error;
+use failure::Fallible as Result;
+use failure::err_msg;
+use handlebars::Handlebars;
+use toml_query::read::TomlValueReadTypeExt;
+
+use libimagmail::mail::Mail;
+use libimagrt::runtime::Runtime;
+use libimagstore::store::FileLockEntry;
+use libimagentryref::reference::Config as RefConfig;
+
+pub fn get_mail_print_format(config_value_path: &'static str, rt: &Runtime, scmd: &ArgMatches) -> Result<Handlebars> {
+ let fmt = match scmd.value_of("format").map(String::from) {
+ Some(s) => Ok(s),
+ None => {
+ rt.config()
+ .ok_or_else(|| err_msg("No configuration file"))?
+ .read_string(config_value_path)
+ .map_err(Error::from)?
+ .ok_or_else(|| format_err!("Configuration '{}' does not exist", config_value_path))
+ }
+ }?;
+
+ let mut hb = Handlebars::new();
+ hb.register_template_string("format", fmt)?;
+
+ hb.register_escape_fn(::handlebars::no_escape);
+ ::libimaginteraction::format::register_all_color_helpers(&mut hb);
+ ::libimaginteraction::format::register_all_format_helpers(&mut hb);
+ Ok(hb)
+}
+
+pub fn build_data_object_for_handlebars(i: usize, m: &FileLockEntry, refconfig: &RefConfig) -> Result<BTreeMap<&'static str, String>> {
+ let mut data = BTreeMap::new();
+
+ data.insert("i", format!("{}", i));
+
+ let id = match m.get_message_id(refconfig)? {
+ Some(f) => f.into(),
+ None => "<no id>".to_owned(),
+ };
+
+ let from = match m.get_from(refconfig)? {
+ Some(f) => f,
+ None => "<no from>".to_owned(),
+ };
+
+ let to = match m.get_to(refconfig)? {
+ Some(f) => f,
+ None => "<no to>".to_owned(),
+ };
+
+ let subject = match m.get_subject(refconfig)? {
+ Some(f) => f,
+ None => "<no subject>".to_owned(),
+ };
+
+ data.insert("id" , id);
+ data.insert("from" , from);
+ data.insert("to" , to);
+ data.insert("subject" , subject);
+
+ Ok(data)
+}
+
diff --git a/imagrc.toml b/imagrc.toml
index 40a3ac49..33b7195b 100644
--- a/imagrc.toml
+++ b/imagrc.toml
@@ -374,6 +374,7 @@ calendars = "/home/user/calendars"
[mail]
# The name of the mail reference collection
ref_collection_name = "mail"
+list_format = "{{i}} - {{id}} - {{from}} -> {{to}}\t: {{subject}}"
[todo]
show_format = """
diff --git a/lib/core/libimagstore/src/store.rs b/lib/core/libimagstore/src/store.rs
index 26612965..5137b517 100644
--- a/lib/core/libimagstore/src/store.rs
+++ b/lib/core/libimagstore/src/store.rs
@@ -216,6 +216,18 @@ impl Store {
Ok(store)
}
+ pub fn is_borrowed<S: IntoStoreId>(&self, id: S) -> Result<bool> {
+ let id = id.into_storeid()?;
+ debug!("Checking whether id is borrowed: '{}'", id);
+
+ self.entries
+ .read()
+ .map_err(|_| Error::from(EM::LockError))
+ .context(format_err!("Error while checking whether {} is borrowed", id))
+ .map(|cache| cache.get(&id).map(|e| e.is_borrowed()).unwrap_or(false))
+ .map_err(Error::from)
+ }
+
/// Creates the Entry at the given location (inside the entry)
///
/// # Return value
diff --git a/lib/domain/libimagmail/src/lib.rs b/lib/domain/libimagmail/src/lib.rs
index 3f6ddd3d..913acfc9 100644
--- a/lib/domain/libimagmail/src/lib.rs
+++ b/lib/domain/libimagmail/src/lib.rs
@@ -59,6 +59,7 @@ pub mod config;
pub mod hasher;
pub mod iter;
pub mod mail;
+pub mod mailflags;
pub mod mid;
pub mod store;
pub mod util;
diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs
index d8e009f0..f24f5506 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;
@@ -35,6 +37,8 @@ use libimagstore::storeid::StoreIdIterator;
use libimagstore::iter::get::StoreIdGetIteratorExtension;
use crate::mid::MessageId;
+use crate::mailflags::MailFlag;
+use crate::hasher::MailHasher;
use crate::iter::MailIterator;
use crate::iter::IntoMailIterator;
@@ -49,6 +53,14 @@ pub trait Mail : RefFassade + Linkable {
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>>;
+ fn is_passed(&self, refconfig: &RefConfig) -> Result<bool>;
+ fn is_replied(&self, refconfig: &RefConfig) -> Result<bool>;
+ fn is_seen(&self, refconfig: &RefConfig) -> Result<bool>;
+ fn is_trashed(&self, refconfig: &RefConfig) -> Result<bool>;
+ fn is_draft(&self, refconfig: &RefConfig) -> Result<bool>;
+ fn is_flagged(&self, refconfig: &RefConfig) -> Result<bool>;
+
fn neighbors(&self) -> Result<StoreIdIterator>;
fn get_neighbors<'a>(&self, store: &'a Store) -> Result<MailIterator<'a>>;
fn get_thread<'a>(&self, store: &'a Store) -> Result<MailIterator<'a>>;
@@ -64,7 +76,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)?;
@@ -147,6 +158,61 @@ 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<_>>>()
+ }
+ }
+
+ /// Check whether the mail is passed
+ fn is_passed(&self, refconfig: &RefConfig) -> Result<bool> {
+ self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Passed == f))
+ }
+
+ /// Check whether the mail is replied
+ fn is_replied(&self, refconfig: &RefConfig) -> Result<bool> {
+ self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Replied == f))
+ }
+
+ /// Check whether the mail is seen
+ fn is_seen(&self, refconfig: &RefConfig) -> Result<bool> {
+ self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Seen == f))
+ }
+
+ /// Check whether the mail is trashed
+ fn is_trashed(&self, refconfig: &RefConfig) -> Result<bool> {
+ self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Trashed == f))
+ }
+
+ /// Check whether the mail is draft
+ fn is_draft(&self, refconfig: &RefConfig) -> Result<bool> {
+ self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Draft == f))
+ }
+
+ /// Check whether the mail is flagged
+ fn is_flagged(&self, refconfig: &RefConfig) -> Result<bool> {
+ self.flags(refconfig).map(|fs| fs.into_iter().any(|f| MailFlag::Flagged == f))
+ }
+
/// Get all direct neighbors for the Mail
///
/// # Note
diff --git a/lib/domain/libimagmail/src/mailflags.rs b/lib/domain/libimagmail/src/mailflags.rs
new file mode 100644
index 00000000..5fd52761
--- /dev/null
+++ b/lib/domain/libimagmail/src/mailflags.rs
@@ -0,0 +1,93 @@
+//
+// 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 std::fmt::{Display, Result as FmtResult, Formatter};
+use std::str::FromStr;
+
+use failure::Fallible as Result;
+use failure::Error;
+
+/// Message flags
+///
+/// As defined by https://cr.yp.to/proto/maildir.html with strong typing
+#[derive(Copy, Clone, Debug, Eq, PartialEq)]
+pub enum MailFlag {
+ /// Flag "P" (passed): the user has resent/forwarded/bounced this message to someone else.
+ Passed,
+
+ /// Flag "R" (replied): the user has replied to this message.
+ Replied,
+
+ /// Flag "S" (seen): the user has viewed this message, though perhaps he didn't read all the way through it.
+ Seen,
+
+ /// Flag "T" (trashed): the user has moved this message to the trash; the trash will be emptied by a later user action.
+ Trashed,
+
+ /// Flag "D" (draft): the user considers this message a draft; toggled at user discretion.
+ Draft,
+
+ /// Flag "F" (flagged): user-defined flag; toggled at user discretion.
+ Flagged,
+}
+
+impl MailFlag {
+ pub fn as_char(self) -> char {
+ match self {
+ MailFlag::Passed => 'P',
+ MailFlag::Replied => 'R',
+ MailFlag::Seen => 'S',
+ MailFlag::Trashed => 'T',
+ MailFlag::Draft => 'D',
+ MailFlag::Flagged => 'F',
+ }
+ }
+}
+
+impl FromStr for MailFlag {
+ type Err = Error;
+
+ fn from_str(s: &str) -> Result<MailFlag> {
+ match s {
+ "P" => Ok(MailFlag::Passed),
+ "R" => Ok(MailFlag::Replied),
+ "S" => Ok(MailFlag::Seen),
+ "T" => Ok(MailFlag::Trashed),
+ "D" => Ok(MailFlag::Draft),
+ "F" => Ok(MailFlag::Flagged),
+ _ => Err(format_err!("Unknown message flag: '{}'", s)),
+ }
+ }
+}
+
+impl Display for MailFlag {
+ fn fmt(&self, f: &mut Formatter) -> FmtResult {
+ let s = match self {
+ MailFlag::Passed => "Passed",
+ MailFlag::Replied => "Replied",
+ MailFlag::Seen => "Seen",
+ MailFlag::Trashed => "Trashed",
+ MailFlag::Draft => "Draft",
+ MailFlag::Flagged => "Flagged",
+ };
+
+ write!(f, "{}", s)
+ }
+}
+