Pass module to subcommand.

We need the module in the subcommand for building file paths and so on.

To be able to pass it, we must remove the ::new() function from the
trait, so we can make the parameter a trait object.

This is no object, as the module gets build non-generically from the
main(), so everything is fine with this.
This commit is contained in:
Matthias Beyer 2015-11-28 12:35:49 +01:00
parent ff873c8fe3
commit 7531b5a6d7
4 changed files with 15 additions and 11 deletions

View File

@ -41,7 +41,7 @@ fn main() {
debug!("Runtime : {:?}", &rt);
if let Some(matches) = rt.config.cli_matches.subcommand_matches("bm") {
let module : BMModule = Module::new(&rt);
let module = BMModule::new(&rt);
let commands = module.get_commands(&rt);
if let Some(command) = matches.subcommand_name() {
debug!("Subcommand: {}", command);
@ -55,7 +55,7 @@ fn main() {
};
let result = match commands.get(command) {
Some(f) => f(cmdenv),
Some(f) => f(&module, cmdenv),
None => Err(ModuleError::new("No subcommand found")),
};

View File

@ -1,6 +1,7 @@
use runtime::Runtime;
use storage::backend::StorageBackend;
use module::Module;
use module::CommandResult;
use module::CommandEnv;
@ -11,7 +12,7 @@ use storage::parser::FileHeaderParser;
use clap::ArgMatches;
use regex::Regex;
pub fn add_command(env: CommandEnv) -> CommandResult {
pub fn add_command(module: &Module, env: CommandEnv) -> CommandResult {
let url = env.matches.value_of("url").unwrap();
let tags = get_tags(env.rt, env.matches);
info!("Adding url '{}' with tags '{:?}'", url, tags);
@ -23,7 +24,7 @@ pub fn add_command(env: CommandEnv) -> CommandResult {
Ok(())
}
pub fn list_command(env: CommandEnv) -> CommandResult {
pub fn list_command(module: &Module, env: CommandEnv) -> CommandResult {
let tags = get_tags(env.rt, env.matches);
let matcher = get_matcher(env.rt, env.matches);
@ -41,7 +42,7 @@ pub fn list_command(env: CommandEnv) -> CommandResult {
Ok(())
}
pub fn remove_command(env: CommandEnv) -> CommandResult {
pub fn remove_command(module: &Module, env: CommandEnv) -> CommandResult {
let tags = get_tags(env.rt, env.matches);
let matcher = get_matcher(env.rt, env.matches);
let id = get_id(env.rt, env.matches);

View File

@ -23,15 +23,19 @@ pub struct BMModule {
const CALLNAMES : &'static [&'static str] = &[ "bm", "bookmark" ];
impl Module for BMModule {
impl BMModule {
fn new(rt : &Runtime) -> BMModule {
pub fn new(rt : &Runtime) -> BMModule {
BMModule {
path: None
}
}
fn callnames() -> &'static [&'static str] {
}
impl Module for BMModule {
fn callnames(&self) -> &'static [&'static str] {
CALLNAMES
}

View File

@ -51,12 +51,11 @@ pub struct CommandEnv<'a> {
pub type ModuleResult = Result<(), ModuleError>;
pub type CommandResult = ModuleResult;
pub type CommandMap<'a> = HashMap<&'a str, fn(CommandEnv) -> CommandResult>;
pub type CommandMap<'a> = HashMap<&'a str, fn(&Module, CommandEnv) -> CommandResult>;
pub trait Module {
fn new(rt : &Runtime) -> Self;
fn callnames() -> &'static [&'static str];
fn callnames(&self) -> &'static [&'static str];
fn name(&self) -> &'static str;
fn shutdown(&self, rt : &Runtime) -> ModuleResult;