From e3ca30d4b1da83f8ab8c90a935f6ff15cc58b982 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 18 Oct 2015 21:15:17 +0200 Subject: [PATCH] Add module trait --- src/main.rs | 2 ++ src/module.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/module.rs diff --git a/src/main.rs b/src/main.rs index 1d7fabad..40ea2ba5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,11 @@ use cli::Config; use runtime::Runtime; +use module::Module; mod cli; mod runtime; +mod module; fn main() { let mut config = Config::new(); diff --git a/src/module.rs b/src/module.rs new file mode 100644 index 00000000..1c686a51 --- /dev/null +++ b/src/module.rs @@ -0,0 +1,49 @@ +use runtime::Runtime; + +pub struct ModuleError { + shortdesc : String, + longdesc : String, +} + +impl ModuleError { + + pub fn short(short : String) -> ModuleError { + ModuleError::new(short, "".to_string()) + } + + pub fn new(short : String, long : String) -> ModuleError { + ModuleError { + shortdesc: short, + longdesc: long, + } + } + + pub fn print(&self, rt : &Runtime) { + if self.longdesc.is_empty() { + let s = format!("Error: {}\n\n{}\n\n", + self.shortdesc, self.longdesc); + rt.print(&s) + } else { + let s = format!("Error: {}\n", self.shortdesc); + rt.print(&s) + } + } + + pub fn print_short(&self, rt : &Runtime) { + rt.print(&self.shortdesc) + } + + pub fn print_long(&self, rt : &Runtime) { + rt.print(&self.longdesc) + } + +} + +pub trait Module { + + fn load(self, &rt : Runtime) -> Self; + fn name(self) -> String; + + fn execute(self, &rt : Runtime) -> Option; + +}