From 6f3b3d6b839b95efb7717a9a0b47bb3386fa819a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Tue, 12 Apr 2016 16:40:25 +0200 Subject: [PATCH] Add error module --- libimaginteraction/src/error.rs | 78 +++++++++++++++++++++++++++++++++ libimaginteraction/src/lib.rs | 1 + 2 files changed, 79 insertions(+) create mode 100644 libimaginteraction/src/error.rs diff --git a/libimaginteraction/src/error.rs b/libimaginteraction/src/error.rs new file mode 100644 index 00000000..a31e3a53 --- /dev/null +++ b/libimaginteraction/src/error.rs @@ -0,0 +1,78 @@ +use std::error::Error; +use std::fmt::Error as FmtError; +use std::clone::Clone; +use std::fmt::{Display, Formatter}; + +/** + * Kind of error + */ +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum InteractionErrorKind { + Unknown +} + +fn interaction_error_type_as_str(e: &InteractionErrorKind) -> &'static str { + match e { + &InteractionErrorKind::Unknown => "Unknown Error", + } +} + +impl Display for InteractionErrorKind { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", interaction_error_type_as_str(self))); + Ok(()) + } + +} + +#[derive(Debug)] +pub struct InteractionError { + err_type: InteractionErrorKind, + cause: Option>, +} + +impl InteractionError { + + /** + * Build a new InteractionError from an InteractionErrorKind, optionally with cause + */ + pub fn new(errtype: InteractionErrorKind, cause: Option>) + -> InteractionError + { + InteractionError { + err_type: errtype, + cause: cause, + } + } + + /** + * Get the error type of this InteractionError + */ + pub fn err_type(&self) -> InteractionErrorKind { + self.err_type.clone() + } + +} + +impl Display for InteractionError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "[{}]", interaction_error_type_as_str(&self.err_type.clone()))); + Ok(()) + } + +} + +impl Error for InteractionError { + + fn description(&self) -> &str { + interaction_error_type_as_str(&self.err_type.clone()) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} + diff --git a/libimaginteraction/src/lib.rs b/libimaginteraction/src/lib.rs index 702e2ce6..2f947a35 100644 --- a/libimaginteraction/src/lib.rs +++ b/libimaginteraction/src/lib.rs @@ -10,5 +10,6 @@ extern crate libimagstore; #[macro_use] extern crate libimagutil; pub mod ask; +pub mod error; pub mod filter;