From adf6009472c2244b6668a1d0ca6fab98f12529c0 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 4 Sep 2023 03:08:52 +0200 Subject: [PATCH] feat: added Ignorable Error with generic default --- src/errors.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/errors.rs b/src/errors.rs index 48b6f17..58d7806 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -23,6 +23,25 @@ where E : std::fmt::Display { } } + +/// an error which can be ignored with just a warning entry and returning the default value +pub trait IgnorableDefaultableError { + fn unwrap_or_warn_default(self, msg: &str) -> T; +} + +impl IgnorableDefaultableError for StdResult +where E : std::fmt::Display, T: Default { + fn unwrap_or_warn_default(self, msg: &str) -> T { + match self { + Ok(x) => x, + Err(e) => { + warn!("{}: {}", msg, e); + T::default() + }, + } + } +} + /// result type for codemp errors pub type Result = StdResult;