From 04a92f9968e2ba91ee9b283f4080cbaa82959cdf Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Sat, 6 Oct 2018 15:12:49 +0100 Subject: [PATCH] Implement the Error trait on the Error enum --- src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 85c2092..89a3e8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod function; mod version; use std::collections::{HashMap, HashSet}; +use std::error; use std::ffi::OsStr; use std::fmt; use std::io; @@ -46,6 +47,31 @@ impl From for Error { } } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Error::ParsingIncomplete => write!(f, "More input was expected by the parser"), + Error::ParsingError => { + write!(f, "An error was encountered while parsing the expression") + } + Error::PeParsingError => write!( + f, + "An error was encountered while reading the version of an executable" + ), + Error::IoError(e) => e.fmt(f), + } + } +} + +impl error::Error for Error { + fn cause(&self) -> Option<&error::Error> { + match self { + Error::IoError(e) => Some(e), + _ => None, + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum GameType { Tes4,