From 26bd04166c9f0ecb70b6653af63bfc8ab2950d9d Mon Sep 17 00:00:00 2001 From: sharkdp Date: Mon, 30 Apr 2018 11:57:37 +0200 Subject: [PATCH 1/2] Implement Error for LoadingError --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f95b3a6b..e84f108b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,8 @@ pub mod html; mod escape; use std::io::Error as IoError; +use std::error::Error; +use std::fmt; #[cfg(all(feature = "yaml-load", feature = "parsing"))] use parsing::ParseSyntaxError; use highlighting::{ParseThemeError, SettingsError}; @@ -102,3 +104,45 @@ impl From for LoadingError { LoadingError::ParseSyntax(error) } } + +impl fmt::Display for LoadingError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use LoadingError::*; + + match *self { + WalkDir(ref error) => error.fmt(f), + Io(ref error) => error.fmt(f), + #[cfg(feature = "yaml-load")] + ParseSyntax(_) => write!(f, "Invalid syntax file"), + ParseTheme(_) => write!(f, "Invalid syntax theme"), + ReadSettings(_) => write!(f, "Invalid syntax theme settings"), + BadPath => write!(f, "Invalid path"), + } + } +} + +impl Error for LoadingError { + fn description(&self) -> &str { + use LoadingError::*; + + match *self { + WalkDir(ref error) => error.description(), + Io(ref error) => error.description(), + #[cfg(feature = "yaml-load")] + ParseSyntax(_) => "Invalid syntax file", + ParseTheme(_) => "Invalid syntax theme", + ReadSettings(_) => "Invalid syntax theme settings", + BadPath => "Invalid path", + } + } + + fn cause(&self) -> Option<&Error> { + use LoadingError::*; + + match *self { + WalkDir(ref error) => Some(error), + Io(ref error) => Some(error), + _ => None, + } + } +} From 2914e1de6e40cd926f9009d0cc45dd6bfb0d53fa Mon Sep 17 00:00:00 2001 From: sharkdp Date: Mon, 30 Apr 2018 17:05:48 +0200 Subject: [PATCH 2/2] Implement fmt via description --- src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e84f108b..8c8a1b20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,11 +112,7 @@ impl fmt::Display for LoadingError { match *self { WalkDir(ref error) => error.fmt(f), Io(ref error) => error.fmt(f), - #[cfg(feature = "yaml-load")] - ParseSyntax(_) => write!(f, "Invalid syntax file"), - ParseTheme(_) => write!(f, "Invalid syntax theme"), - ReadSettings(_) => write!(f, "Invalid syntax theme settings"), - BadPath => write!(f, "Invalid path"), + _ => write!(f, "{}", self.description()), } } }