From 58eee2f329859c33008a5b8b5af53189b2b4ab4a Mon Sep 17 00:00:00 2001 From: khai96_ Date: Sat, 27 Apr 2019 13:56:45 +0700 Subject: Combine ContextualError and ContextualErrorKind into one --- src/errors.rs | 58 ++++------------------------------------------------------ 1 file changed, 4 insertions(+), 54 deletions(-) (limited to 'src/errors.rs') diff --git a/src/errors.rs b/src/errors.rs index 833e9c4..b8af25d 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,8 +1,7 @@ -use failure::{Backtrace, Context, Fail}; -use std::fmt::{self, Debug, Display}; +use failure::Fail; #[derive(Debug, Fail)] -pub enum ContextualErrorKind { +pub enum ContextualError { /// Fully customized errors, not inheriting from any error #[fail(display = "{}", _0)] CustomError(String), @@ -73,58 +72,9 @@ pub fn log_error_chain(description: String) { } } -/// Based on https://boats.gitlab.io/failure/error-errorkind.html -pub struct ContextualError { - inner: Context, -} - -impl ContextualError { - pub fn new(kind: ContextualErrorKind) -> ContextualError { - ContextualError { - inner: Context::new(kind), - } - } -} - -impl Fail for ContextualError { - fn cause(&self) -> Option<&Fail> { - self.inner.cause() - } - - fn backtrace(&self) -> Option<&Backtrace> { - self.inner.backtrace() - } -} - -impl Display for ContextualError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - Display::fmt(&self.inner, f) - } -} - -impl Debug for ContextualError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - Debug::fmt(&self.inner, f) - } -} - -impl From> for ContextualError { - fn from(inner: Context) -> ContextualError { - ContextualError { inner } - } -} - -impl From for ContextualError { - fn from(kind: ContextualErrorKind) -> ContextualError { - ContextualError { - inner: Context::new(kind), - } - } -} - -/// This allows to create CustomErrors more simply +/// This makes creating CustomErrors easier impl From for ContextualError { fn from(msg: String) -> ContextualError { - ContextualError::new(ContextualErrorKind::CustomError(msg)) + ContextualError::CustomError(msg) } } -- cgit v1.2.3