melib/error: add chain_err_summary() method

async
Manos Pitsidianakis 2020-06-06 12:27:02 +03:00
parent e4d4cd55d3
commit db4c401828
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 36 additions and 1 deletions

View File

@ -43,6 +43,41 @@ pub struct MeliError {
pub source: Option<std::sync::Arc<dyn Error + Send + Sync + 'static>>,
}
pub trait IntoMeliError {
fn set_err_summary<M>(self, msg: M) -> MeliError
where
M: Into<Cow<'static, str>>;
}
pub trait ResultIntoMeliError<T> {
fn chain_err_summary<M, F>(self, msg_fn: F) -> Result<T>
where
F: Fn() -> M,
M: Into<Cow<'static, str>>;
}
impl<I: Into<MeliError>> IntoMeliError for I {
#[inline]
fn set_err_summary<M>(self, msg: M) -> MeliError
where
M: Into<Cow<'static, str>>,
{
let err: MeliError = self.into();
err.set_summary(msg)
}
}
impl<T, I: Into<MeliError>> ResultIntoMeliError<T> for std::result::Result<T, I> {
#[inline]
fn chain_err_summary<M, F>(self, msg_fn: F) -> Result<T>
where
F: Fn() -> M,
M: Into<Cow<'static, str>>,
{
self.map_err(|err| err.set_err_summary(msg_fn()))
}
}
impl MeliError {
pub fn new<M>(msg: M) -> MeliError
where

View File

@ -136,7 +136,7 @@ pub use crate::backends::{Backends, RefreshEvent, RefreshEventConsumer, SpecialU
pub use crate::collection::*;
pub use crate::conf::*;
pub use crate::email::{Envelope, EnvelopeHash, Flag};
pub use crate::error::{MeliError, Result};
pub use crate::error::{IntoMeliError, MeliError, Result, ResultIntoMeliError};
pub use crate::addressbook::*;