summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-02-22 11:31:49 +0100
committerMatthias Beyer <mail@beyermatthias.de>2019-02-22 11:31:58 +0100
commit211d6fa60bb2c306ea674b9fb37c11ca9b19b8cc (patch)
tree535819ada76c6ad78d466398e7ddeb30662e938a
parentb2fbf1e4629fd05b37ac5636379efb709b060a1e (diff)
downloadimag-211d6fa60bb2c306ea674b9fb37c11ca9b19b8cc.zip
imag-211d6fa60bb2c306ea674b9fb37c11ca9b19b8cc.tar.gz
Add helper function to be more flexible
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/domain/libimagmail/src/util.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/domain/libimagmail/src/util.rs b/lib/domain/libimagmail/src/util.rs
index 8b51a9f..c58cf49 100644
--- a/lib/domain/libimagmail/src/util.rs
+++ b/lib/domain/libimagmail/src/util.rs
@@ -23,23 +23,37 @@ use failure::Error;
use failure::Fallible as Result;
use failure::ResultExt;
-pub(crate) fn get_message_id_for_mailfile<P: AsRef<Path>>(p: P) -> Result<String> {
+/// Get the message header at a specific key
+///
+/// # WARNING
+///
+/// Key must be all-lowercase
+///
+/// # WARNING
+///
+/// Expensive, as mailfile gets read from disk internally.
+/// TODO: Optimize
+///
+pub(crate) fn get_message_header_at_key<P: AsRef<Path>, K: AsRef<str>>(p: P, k: K) -> Result<String> {
::mailparse::parse_mail(::std::fs::read_to_string(p.as_ref())?.as_bytes())
.context(format_err!("Cannot parse Email {}", p.as_ref().display()))?
.headers
.into_iter()
.filter_map(|hdr| match hdr.get_key() {
Err(e) => Some(Err(e).map_err(Error::from)),
- Ok(k) => if k.to_lowercase() == "message-id" {
+ Ok(k) => if k.to_lowercase() == k.as_ref() {
Some(Ok(hdr))
} else {
None
}
})
.next()
- .ok_or_else(|| format_err!("Message Id not found in {}", p.as_ref().display()))?
+ .ok_or_else(|| format_err!("'{}' not found in {}", k.as_ref(), p.as_ref().display()))?
.and_then(|hdr| hdr.get_value().map_err(Error::from))
- .map(strip_message_delimiters)
+}
+
+pub(crate) fn get_message_id_for_mailfile<P: AsRef<Path>>(p: P) -> Result<String> {
+ get_message_header_at_key(p, "message-id").map(strip_message_delimiters)
}
/// Strips message delimiters ('<' and '>') from a Message-ID field.