From af54e621a83cfadb63569f63f722301c60661168 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 4 Oct 2016 14:15:57 +0200 Subject: [PATCH] Add Mail::from_ref() --- libimagmail/src/mail.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/libimagmail/src/mail.rs b/libimagmail/src/mail.rs index 2187b1fe..b49e0ed5 100644 --- a/libimagmail/src/mail.rs +++ b/libimagmail/src/mail.rs @@ -57,15 +57,18 @@ impl<'a> Mail<'a> { /// Opens a mail by the passed hash pub fn open>(store: &Store, hash: S) -> Result> { - let r = try!(Ref::get_by_hash(store, String::from(hash.as_ref())) + Ref::get_by_hash(store, String::from(hash.as_ref())) .map_err_into(MEK::FetchByHashError) - .map_err_into(MEK::FetchError)); + .map_err_into(MEK::FetchError) + .and_then(|o| match o { + Some(r) => Mail::from_ref(r).map(Some), + None => Ok(None), + }) - if r.is_none() { - return Ok(None); - } - let r = r.unwrap(); + } + /// Implement me as TryFrom as soon as it is stable + pub fn from_ref(r: Ref<'a>) -> Result { r.fs_file() .map_err_into(MEK::RefHandlingError) .and_then(|path| File::open(path).map_err_into(MEK::IOError)) @@ -76,7 +79,7 @@ impl<'a> Mail<'a> { .map_err_into(MEK::IOError) }) .map(Buffer::from) - .map(|buffer| Some(Mail(r, buffer))) + .map(|buffer| Mail(r, buffer)) } pub fn get_field(&self, field: &str) -> Result> {