From 6b2502c4d705bc6c43de0a02ca1af69811f92999 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 21 Jan 2016 18:42:43 +0100 Subject: [PATCH] rt: Add error for instantiation --- libimagrt/src/error.rs | 57 ++++++++++++++++++++++++++++++++++++++++++ libimagrt/src/lib.rs | 1 + 2 files changed, 58 insertions(+) create mode 100644 libimagrt/src/error.rs diff --git a/libimagrt/src/error.rs b/libimagrt/src/error.rs new file mode 100644 index 00000000..6d72cbe0 --- /dev/null +++ b/libimagrt/src/error.rs @@ -0,0 +1,57 @@ +use std::error::Error; +use std::fmt::Display; +use std::fmt::Formatter; +use std::fmt::Error as FmtError; +use std::clone::Clone; + +#[derive(Debug, PartialEq, Clone, Copy)] +pub enum RuntimeErrorKind { + Instantiate, + + // more? +} + +#[derive(Debug)] +pub struct RuntimeError { + kind: RuntimeErrorKind, + cause: Option>, +} + +impl RuntimeError { + + pub fn new(kind: RuntimeErrorKind, cause: Option>) -> RuntimeError { + RuntimeError { + kind: kind, + cause: cause, + } + } + +} + +fn runtime_error_kind_as_str(e: &RuntimeErrorKind) -> &'static str { + match e { + &RuntimeErrorKind::Instantiate => "Could not instantiate", + } +} + +impl Display for RuntimeError { + + fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> { + try!(write!(fmt, "{}", runtime_error_kind_as_str(&self.kind))); + Ok(()) + } + +} + +impl Error for RuntimeError { + + fn description(&self) -> &str { + runtime_error_kind_as_str(&self.kind) + } + + fn cause(&self) -> Option<&Error> { + self.cause.as_ref().map(|e| &**e) + } + +} + diff --git a/libimagrt/src/lib.rs b/libimagrt/src/lib.rs index 5d6c194c..4ef07144 100644 --- a/libimagrt/src/lib.rs +++ b/libimagrt/src/lib.rs @@ -9,6 +9,7 @@ extern crate libimagstore; extern crate libimagutil; mod configuration; +mod error; mod logger; mod runtime;