From 15b1adbc206973b1ae4e8236cc8c1f90ef370ef8 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 4 Feb 2016 14:05:30 +0100 Subject: [PATCH] Add error type implementation --- libimagtag/src/error.rs | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/libimagtag/src/error.rs b/libimagtag/src/error.rs index e69de29b..321ecf1a 100644 --- a/libimagtag/src/error.rs +++ b/libimagtag/src/error.rs @@ -0,0 +1,63 @@ +use std::error::Error; +use std::fmt::Error as FmtError; +use std::clone::Clone; +use std::fmt::{Debug, Display, Formatter}; +use std::fmt; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum TagErrorKind { +} + +fn tag_error_type_as_str(e: &TagErrorKind) -> &'static str { + match e { + _ => "", + } +} + +impl Display for TagErrorKind { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", tag_error_type_as_str(self))); + Ok(()) + } + +} + +#[derive(Debug)] +pub struct TagError { + kind: TagErrorKind, + cause: Option>, +} + +impl TagError { + + pub fn new(errtype: TagErrorKind, cause: Option>) -> TagError { + TagError { + kind: errtype, + cause: cause, + } + } + +} + +impl Display for TagError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "[{}]", tag_error_type_as_str(&self.kind.clone()))); + Ok(()) + } + +} + +impl Error for TagError { + + fn description(&self) -> &str { + tag_error_type_as_str(&self.kind.clone()) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} +