Add module trait
This commit is contained in:
parent
98c782212c
commit
e3ca30d4b1
2 changed files with 51 additions and 0 deletions
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
use cli::Config;
|
use cli::Config;
|
||||||
use runtime::Runtime;
|
use runtime::Runtime;
|
||||||
|
use module::Module;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
mod runtime;
|
mod runtime;
|
||||||
|
mod module;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut config = Config::new();
|
let mut config = Config::new();
|
||||||
|
|
49
src/module.rs
Normal file
49
src/module.rs
Normal file
|
@ -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<ModuleError>;
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue