diff --git a/lib/domain/libimagmail/Cargo.toml b/lib/domain/libimagmail/Cargo.toml index c52f8d7d..05166d93 100644 --- a/lib/domain/libimagmail/Cargo.toml +++ b/lib/domain/libimagmail/Cargo.toml @@ -23,7 +23,7 @@ maintenance = { status = "actively-developed" } log = "0.4.0" email = "0.0.20" filters = "0.3" -error-chain = "0.12" +failure = "0.1" libimagstore = { version = "0.9.0", path = "../../../lib/core/libimagstore" } libimagerror = { version = "0.9.0", path = "../../../lib/core/libimagerror" } diff --git a/lib/domain/libimagmail/src/error.rs b/lib/domain/libimagmail/src/error.rs deleted file mode 100644 index 519eb729..00000000 --- a/lib/domain/libimagmail/src/error.rs +++ /dev/null @@ -1,64 +0,0 @@ -// -// imag - the personal information management suite for the commandline -// Copyright (C) 2015-2018 Matthias Beyer 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 -// - -error_chain! { - types { - MailError, MailErrorKind, ResultExt, Result; - } - - links { - RefError(::libimagentryref::error::RefError, ::libimagentryref::error::RefErrorKind); - } - - foreign_links { - IoError(::std::io::Error); - } - - - errors { - RefCreationError { - description("Error creating a reference to a file/directory") - display("Error creating a reference to a file/directory") - } - - RefHandlingError { - description("Error handling a reference") - display("Error handling a reference") - } - - MailParsingError { - description("Failed to parse mail") - display("Failed to parse mail") - } - - FetchByHashError { - description("Error fetching mail from Store by hash") - display("Error fetching mail from Store by hash") - } - FetchError { - description("Error fetching mail from Store") - display("Error fetching mail from Store") - } - IOError { - description("IO Error") - display("IO Error") - } - } -} - diff --git a/lib/domain/libimagmail/src/iter.rs b/lib/domain/libimagmail/src/iter.rs index 251bc8fb..22fa84bb 100644 --- a/lib/domain/libimagmail/src/iter.rs +++ b/lib/domain/libimagmail/src/iter.rs @@ -25,7 +25,7 @@ //! use mail::Mail; -use error::Result; +use failure::Fallible as Result; use libimagstore::store::FileLockEntry; diff --git a/lib/domain/libimagmail/src/lib.rs b/lib/domain/libimagmail/src/lib.rs index 1439f9b7..fc883404 100644 --- a/lib/domain/libimagmail/src/lib.rs +++ b/lib/domain/libimagmail/src/lib.rs @@ -38,13 +38,12 @@ #[macro_use] extern crate log; extern crate email; extern crate filters; -#[macro_use] extern crate error_chain; +extern crate failure; extern crate libimagerror; extern crate libimagstore; extern crate libimagentryref; -pub mod error; pub mod iter; pub mod mail; diff --git a/lib/domain/libimagmail/src/mail.rs b/lib/domain/libimagmail/src/mail.rs index bf16c4a6..a1b33a14 100644 --- a/lib/domain/libimagmail/src/mail.rs +++ b/lib/domain/libimagmail/src/mail.rs @@ -21,7 +21,6 @@ use std::path::Path; use std::fs::File; use std::io::Read; use std::fs::OpenOptions; -use std::result::Result as RResult; use libimagstore::store::Store; use libimagstore::storeid::StoreId; @@ -29,24 +28,25 @@ use libimagstore::store::FileLockEntry; use libimagentryref::reference::Ref; use libimagentryref::refstore::RefStore; use libimagentryref::refstore::UniqueRefPathGenerator; +use libimagerror::errors::ErrorMsg as EM; use email::MimeMessage; use email::results::ParsingResult as EmailParsingResult; -use error::Result; -use error::{ResultExt, MailError as ME, MailErrorKind as MEK}; +use failure::Fallible as Result; +use failure::ResultExt; +use failure::Error; +use failure::err_msg; struct UniqueMailRefGenerator; impl UniqueRefPathGenerator for UniqueMailRefGenerator { - type Error = ME; - /// The collection the `StoreId` should be created for fn collection() -> &'static str { "mail" } /// A function which should generate a unique string for a Path - fn unique_hash>(path: A) -> RResult { + fn unique_hash>(path: A) -> Result { use filters::filter::Filter; use email::Header; @@ -59,7 +59,8 @@ impl UniqueRefPathGenerator for UniqueMailRefGenerator { .read_to_string(&mut s)?; MimeMessage::parse(&s) - .chain_err(|| MEK::RefCreationError) + .context(err_msg("Error creating ref")) + .map_err(Error::from) .and_then(|mail| { let has_key = |hdr: &Header, exp: &str| hdr.name == exp; @@ -73,7 +74,7 @@ impl UniqueRefPathGenerator for UniqueMailRefGenerator { for hdr in mail.headers.iter().filter(|item| filter.filter(item)) { let s = hdr .get_value() - .chain_err(|| MEK::RefCreationError)?; + .context(err_msg("Ref creation error"))?; v.push(s); } @@ -83,7 +84,7 @@ impl UniqueRefPathGenerator for UniqueMailRefGenerator { } /// Postprocess the generated `StoreId` object - fn postprocess_storeid(sid: StoreId) -> RResult { + fn postprocess_storeid(sid: StoreId) -> Result { Ok(sid) } } @@ -113,13 +114,15 @@ impl<'a> Mail<'a> { .and_then(|reference| { debug!("Build reference file: {:?}", reference); reference.get_path() - .chain_err(|| MEK::RefHandlingError) - .and_then(|path| File::open(path).chain_err(|| MEK::IOError)) + .context(err_msg("Ref handling error")) + .map_err(Error::from) + .and_then(|path| File::open(path).context(EM::IO).map_err(Error::from)) .and_then(|mut file| { let mut s = String::new(); file.read_to_string(&mut s) .map(|_| s) - .chain_err(|| MEK::IOError) + .context(EM::IO) + .map_err(Error::from) }) .map(Buffer::from) .map(|buffer| Mail(reference, buffer)) @@ -130,8 +133,9 @@ impl<'a> Mail<'a> { pub fn open>(store: &Store, hash: S) -> Result> { debug!("Opening Mail by Hash"); store.get_ref::(hash) - .chain_err(|| MEK::FetchByHashError) - .chain_err(|| MEK::FetchError) + .context(err_msg("Fetch by hash error")) + .context(err_msg("Fetch error")) + .map_err(Error::from) .and_then(|o| match o { Some(r) => Mail::from_fle(r).map(Some), None => Ok(None), @@ -141,13 +145,15 @@ impl<'a> Mail<'a> { /// Implement me as TryFrom as soon as it is stable pub fn from_fle(fle: FileLockEntry<'a>) -> Result> { fle.get_path() - .chain_err(|| MEK::RefHandlingError) - .and_then(|path| File::open(path).chain_err(|| MEK::IOError)) + .context(err_msg("Ref handling error")) + .map_err(Error::from) + .and_then(|path| File::open(path).context(EM::IO).map_err(Error::from)) .and_then(|mut file| { let mut s = String::new(); file.read_to_string(&mut s) .map(|_| s) - .chain_err(|| MEK::IOError) + .context(EM::IO) + .map_err(Error::from) }) .map(Buffer::from) .map(|buffer| Mail(fle, buffer)) @@ -157,7 +163,8 @@ impl<'a> Mail<'a> { debug!("Getting field in mail: {:?}", field); self.1 .parsed() - .chain_err(|| MEK::MailParsingError) + .context(err_msg("Mail parsing error")) + .map_err(Error::from) .map(|parsed| { parsed.headers .iter()