diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2016-10-04 14:15:57 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2016-10-12 19:17:41 +0200 |
commit | af54e621a83cfadb63569f63f722301c60661168 (patch) | |
tree | 1fb91c939a447a65f6f8b797e2779b4adef9f13c /libimagmail | |
parent | 80010f6043a6e4e2051f22a5826f368d1ed2d27b (diff) |
Add Mail::from_ref()
Diffstat (limited to 'libimagmail')
-rw-r--r-- | libimagmail/src/mail.rs | 17 |
1 files 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<S: AsRef<str>>(store: &Store, hash: S) -> Result<Option<Mail>> { - 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<Mail> { 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<Option<String>> { |