imag/src/module/mod.rs
Matthias Beyer f7a92b6e79 Resort "use"
First comes std stuff
Second is external crates
Then we have own modules

All in alphabetical order and as few lines as possible if readability is
preserved.
2015-12-05 01:14:13 +01:00

67 lines
1.5 KiB
Rust

use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::fmt::Result as FMTResult;
use std::path::Path;
use std::result::Result;
use clap::{App, ArgMatches};
use runtime::Runtime;
use storage::backend::{StorageBackend, StorageBackendError};
pub mod bm;
#[derive(Debug)]
pub struct ModuleError {
desc: String,
caused_by: Option<Box<Error>>,
}
impl ModuleError {
pub fn new(desc: &'static str) -> ModuleError {
ModuleError {
desc: desc.to_owned().to_string(),
caused_by: None,
}
}
}
impl Error for ModuleError {
fn description(&self) -> &str {
&self.desc[..]
}
fn cause(&self) -> Option<&Error> {
self.caused_by.as_ref().map(|e| &**e)
}
}
impl Display for ModuleError {
fn fmt(&self, f: &mut Formatter) -> FMTResult {
write!(f, "ModuleError: {}", self.description())
}
}
pub struct CommandEnv<'a> {
pub rt: &'a Runtime<'a>,
pub bk: &'a StorageBackend,
pub matches: &'a ArgMatches<'a, 'a>,
}
pub type ModuleResult = Result<(), ModuleError>;
pub type CommandResult = ModuleResult;
pub type CommandMap<'a> = HashMap<&'a str, fn(&Module, CommandEnv) -> CommandResult>;
pub trait Module : Debug {
fn callnames(&self) -> &'static [&'static str];
fn name(&self) -> &'static str;
fn shutdown(&self, rt : &Runtime) -> ModuleResult;
fn get_commands(&self, rt: &Runtime) -> CommandMap;
}