imag/src/module/mod.rs

70 lines
1.5 KiB
Rust
Raw Normal View History

2015-10-24 15:46:01 +00:00
use runtime::Runtime;
use std::error::Error;
2015-10-24 16:40:40 +00:00
use std::fmt::Formatter;
use std::fmt::Result as FMTResult;
use std::fmt::Display;
2015-11-30 17:33:13 +00:00
use std::fmt::Debug;
use std::path::Path;
2015-10-24 16:40:40 +00:00
use std::result::Result;
use std::collections::HashMap;
2015-10-19 15:54:32 +00:00
use clap::{App, ArgMatches};
use storage::backend::{StorageBackend, StorageBackendError};
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 {
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>,
pub bk: &'a StorageBackend,
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
}