2015-12-05 00:14:13 +00:00
|
|
|
use std::collections::HashMap;
|
2015-10-24 15:46:01 +00:00
|
|
|
use std::error::Error;
|
2015-12-05 00:14:13 +00:00
|
|
|
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;
|
2015-11-28 10:54:27 +00:00
|
|
|
|
2015-12-05 00:14:13 +00:00
|
|
|
use runtime::Runtime;
|
2015-12-05 00:23:06 +00:00
|
|
|
use storage::backend::StorageBackend;
|
2015-10-25 18:56:04 +00:00
|
|
|
|
|
|
|
pub mod bm;
|
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 {
|
2015-11-28 10:54:04 +00:00
|
|
|
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 {
|
2015-10-26 19:55:31 +00:00
|
|
|
write!(f, "ModuleError: {}", self.description())
|
2015-10-24 16:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-28 10:54:27 +00:00
|
|
|
pub struct CommandEnv<'a> {
|
|
|
|
pub rt: &'a Runtime<'a>,
|
|
|
|
pub bk: &'a StorageBackend,
|
|
|
|
pub matches: &'a ArgMatches<'a, 'a>,
|
|
|
|
}
|
|
|
|
|
2015-10-24 16:40:40 +00:00
|
|
|
pub type ModuleResult = Result<(), ModuleError>;
|
2015-11-28 10:54:27 +00:00
|
|
|
pub type CommandResult = ModuleResult;
|
2015-11-28 11:35:49 +00:00
|
|
|
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
|
|
|
|
2015-11-28 11:35:49 +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
|
|
|
|
2015-11-28 10:09:10 +00:00
|
|
|
fn get_commands(&self, rt: &Runtime) -> CommandMap;
|
2015-11-10 18:28:19 +00:00
|
|
|
|
2015-10-19 15:54:32 +00:00
|
|
|
}
|
|
|
|
|