imag/src/module/mod.rs

68 lines
1.4 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2015-10-24 15:46:01 +00:00
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
2015-10-24 16:40:40 +00:00
use std::fmt::Result as FMTResult;
use std::result::Result;
2015-10-19 15:54:32 +00:00
2015-12-05 00:23:06 +00:00
use clap::ArgMatches;
use runtime::Runtime;
2015-12-06 11:49:02 +00:00
use storage::Storage;
2015-10-25 18:56:04 +00:00
pub mod bm;
pub mod helpers;
2015-10-19 15:54:32 +00:00
2015-10-24 16:40:40 +00:00
#[derive(Debug)]
pub struct ModuleError {
desc: String,
2015-11-28 14:55:22 +00:00
caused_by: Option<Box<Error>>,
2015-10-24 16:40:40 +00:00
}
impl ModuleError {
pub fn new(desc: &'static str) -> ModuleError {
2015-10-24 16:40:40 +00:00
ModuleError {
2015-10-25 18:54:54 +00:00
desc: desc.to_owned().to_string(),
2015-11-28 14:55:22 +00:00
caused_by: None,
2015-10-24 16:40:40 +00:00
}
}
}
impl Error for ModuleError {
fn description(&self) -> &str {
&self.desc[..]
}
fn cause(&self) -> Option<&Error> {
2015-11-28 15:19:37 +00:00
self.caused_by.as_ref().map(|e| &**e)
2015-10-24 16:40:40 +00:00
}
}
impl Display for ModuleError {
fn fmt(&self, f: &mut Formatter) -> FMTResult {
write!(f, "ModuleError: {}", self.description())
2015-10-24 16:40:40 +00:00
}
}
pub struct CommandEnv<'a> {
pub rt: &'a Runtime<'a>,
2015-12-06 11:49:02 +00:00
pub bk: &'a Storage,
pub matches: &'a ArgMatches<'a, 'a>,
}
2015-10-24 16:40:40 +00:00
pub type ModuleResult = Result<(), ModuleError>;
pub type CommandResult = ModuleResult;
pub type CommandMap<'a> = HashMap<&'a str, fn(&Module, CommandEnv) -> CommandResult>;
2015-10-24 16:40:40 +00:00
2015-11-30 17:33:13 +00:00
pub trait Module : Debug {
2015-10-19 15:54:32 +00:00
fn callnames(&self) -> &'static [&'static str];
2015-10-19 16:09:39 +00:00
fn name(&self) -> &'static str;
2015-10-24 16:40:40 +00:00
fn shutdown(&self, rt : &Runtime) -> ModuleResult;
2015-10-19 15:54:32 +00:00
fn get_commands(&self, rt: &Runtime) -> CommandMap;
2015-10-19 15:54:32 +00:00
}