From 48c3cc5f5d7a8bf975013a4b4e045fed3a0acabc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 16 Jan 2016 13:36:19 +0100 Subject: [PATCH] Introduce StoreErrorType enum for simple Error type identification --- libimagstore/src/error.rs | 67 +++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/libimagstore/src/error.rs b/libimagstore/src/error.rs index cca6af95..5950769b 100644 --- a/libimagstore/src/error.rs +++ b/libimagstore/src/error.rs @@ -4,11 +4,60 @@ use std::fmt::Display; use std::fmt::Formatter; use std::fmt::Error as FmtError; use std::clone::Clone; +use std::convert::From; use std::io::Error as IOError; +#[derive(Clone)] +pub enum StoreErrorType { + Unknown, + IdNotFound, + OutOfMemory, + // maybe more +} + +impl From for String { + + fn from(e: StoreErrorType) -> String { + String::from(&e) + } + +} + +impl<'a> From<&'a StoreErrorType> for String { + + fn from(e: &'a StoreErrorType) -> String { + match e { + &StoreErrorType::Unknown => String::from(""), + &StoreErrorType::IdNotFound => String::from("ID not found"), + &StoreErrorType::OutOfMemory => String::from("Out of Memory"), + } + } + +} + +impl Debug for StoreErrorType { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + let s : String = self.into(); + try!(write!(fmt, "{:?}", s)); + Ok(()) + } + +} + +impl Display for StoreErrorType { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + let s : String = self.into(); + try!(write!(fmt, "{}", s)); + Ok(()) + } + +} + pub struct StoreError { - name: Option<&'static str>, + err_type: StoreErrorType, expl: Option<&'static str>, cause: Option>, } @@ -17,14 +66,18 @@ impl StoreError { pub fn new() -> StoreError { StoreError { - name: None, + err_type: StoreErrorType::Unknown, expl: None, cause: None, } } - pub fn with_name(mut self, n: &'static str) -> StoreError { - self.name = Some(n); + pub fn err_type(&self) -> StoreErrorType { + self.err_type.clone() + } + + pub fn with_type(mut self, t: StoreErrorType) -> StoreError { + self.err_type = t; self } @@ -43,7 +96,8 @@ impl StoreError { impl Debug for StoreError { fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { - try!(write!(fmt, "[{:?}]: {:?}, caused: {:?}", self.name, self.expl, self.cause)); + try!(write!(fmt, "[{:?}]: {:?}, caused: {:?}", + self.err_type, self.expl, self.cause)); Ok(()) } @@ -52,8 +106,9 @@ impl Debug for StoreError { impl Display for StoreError { fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + let e : String = self.err_type.clone().into(); try!(write!(fmt, "[{}]: {}", - self.name.unwrap_or("StoreError"), + e, self.expl.unwrap_or(""))); Ok(()) }