diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-06-06 12:27:02 +0300 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2020-06-06 12:27:02 +0300 |
commit | db4c40182880582761937a0b8e0dfdc915628b21 (patch) | |
tree | 147422e3e1af48c6c57511bfba4ff5303773dc4f /melib/src | |
parent | e4d4cd55d3ba2c80e7efa420ea94193f4701e8dc (diff) |
melib/error: add chain_err_summary() method
Diffstat (limited to 'melib/src')
-rw-r--r-- | melib/src/error.rs | 35 | ||||
-rw-r--r-- | melib/src/lib.rs | 2 |
2 files changed, 36 insertions, 1 deletions
diff --git a/melib/src/error.rs b/melib/src/error.rs index 31c4b9d9..b1bcc232 100644 --- a/melib/src/error.rs +++ b/melib/src/error.rs @@ -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 diff --git a/melib/src/lib.rs b/melib/src/lib.rs index 3ffc3ac7..0ebd7337 100644 --- a/melib/src/lib.rs +++ b/melib/src/lib.rs @@ -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::*; |