From 911384331bdaddca774945c0272a4c1c2f969f16 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 24 Oct 2015 18:40:40 +0200 Subject: [PATCH] Add ModuleError --- src/module/mod.rs | 50 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/module/mod.rs b/src/module/mod.rs index 5b463570..12c6e699 100644 --- a/src/module/mod.rs +++ b/src/module/mod.rs @@ -1,19 +1,57 @@ use runtime::Runtime; use std::error::Error; +use std::fmt::Formatter; +use std::fmt::Result as FMTResult; +use std::fmt::Display; use std::path::Path; +use std::result::Result; use module::todo::TodoModule; mod todo; -pub trait Module { +#[derive(Debug)] +pub struct ModuleError { + desc: String, +} - fn new(&rt : Runtime) -> Self; - fn callnames() -> &'static [str]; - fn name(&self) -> &'static str; +impl ModuleError { + fn mk(desc: String) -> ModuleError { + ModuleError { + desc: desc, + } + } +} - fn execute(&self, &rt : Runtime) -> Option; - fn shutdown(&self, &rt : Runtime) -> Option; +impl Error for ModuleError { + + fn description(&self) -> &str { + &self.desc[..] + } + + fn cause(&self) -> Option<&Error> { + None + } + +} + +impl Display for ModuleError { + fn fmt(&self, f: &mut Formatter) -> FMTResult { + write!(f, "ModuleError: {}", + self.description()) + } +} + +pub type ModuleResult = Result<(), ModuleError>; + +pub trait Module { + + fn new(rt : &Runtime) -> Self; + fn callnames() -> &'static [&'static str]; + fn name(&self) -> &'static str; + + fn execute(&self, rt : &Runtime) -> ModuleResult; + fn shutdown(&self, rt : &Runtime) -> ModuleResult; }