libimagmail: Move from error-chain to failure

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2018-10-30 18:40:51 +01:00
parent d3c0826188
commit f4ff2ba250
5 changed files with 28 additions and 86 deletions

View file

@ -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" }

View file

@ -1,64 +0,0 @@
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2018 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
//
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")
}
}
}

View file

@ -25,7 +25,7 @@
//!
use mail::Mail;
use error::Result;
use failure::Fallible as Result;
use libimagstore::store::FileLockEntry;

View file

@ -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;

View file

@ -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<A: AsRef<Path>>(path: A) -> RResult<String, Self::Error> {
fn unique_hash<A: AsRef<Path>>(path: A) -> Result<String> {
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<StoreId, Self::Error> {
fn postprocess_storeid(sid: StoreId) -> Result<StoreId> {
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<S: AsRef<str>>(store: &Store, hash: S) -> Result<Option<Mail>> {
debug!("Opening Mail by Hash");
store.get_ref::<UniqueMailRefGenerator, S>(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<Mail<'a>> {
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()