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:
parent
ff873c8fe3
commit
7531b5a6d7
4 changed files with 15 additions and 11 deletions
|
@ -41,7 +41,7 @@ fn main() {
|
||||||
debug!("Runtime : {:?}", &rt);
|
debug!("Runtime : {:?}", &rt);
|
||||||
|
|
||||||
if let Some(matches) = rt.config.cli_matches.subcommand_matches("bm") {
|
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);
|
let commands = module.get_commands(&rt);
|
||||||
if let Some(command) = matches.subcommand_name() {
|
if let Some(command) = matches.subcommand_name() {
|
||||||
debug!("Subcommand: {}", command);
|
debug!("Subcommand: {}", command);
|
||||||
|
@ -55,7 +55,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = match commands.get(command) {
|
let result = match commands.get(command) {
|
||||||
Some(f) => f(cmdenv),
|
Some(f) => f(&module, cmdenv),
|
||||||
None => Err(ModuleError::new("No subcommand found")),
|
None => Err(ModuleError::new("No subcommand found")),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use runtime::Runtime;
|
use runtime::Runtime;
|
||||||
use storage::backend::StorageBackend;
|
use storage::backend::StorageBackend;
|
||||||
|
|
||||||
|
use module::Module;
|
||||||
use module::CommandResult;
|
use module::CommandResult;
|
||||||
use module::CommandEnv;
|
use module::CommandEnv;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ use storage::parser::FileHeaderParser;
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use regex::Regex;
|
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 url = env.matches.value_of("url").unwrap();
|
||||||
let tags = get_tags(env.rt, env.matches);
|
let tags = get_tags(env.rt, env.matches);
|
||||||
info!("Adding url '{}' with tags '{:?}'", url, tags);
|
info!("Adding url '{}' with tags '{:?}'", url, tags);
|
||||||
|
@ -23,7 +24,7 @@ pub fn add_command(env: CommandEnv) -> CommandResult {
|
||||||
Ok(())
|
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 tags = get_tags(env.rt, env.matches);
|
||||||
let matcher = get_matcher(env.rt, env.matches);
|
let matcher = get_matcher(env.rt, env.matches);
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ pub fn list_command(env: CommandEnv) -> CommandResult {
|
||||||
Ok(())
|
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 tags = get_tags(env.rt, env.matches);
|
||||||
let matcher = get_matcher(env.rt, env.matches);
|
let matcher = get_matcher(env.rt, env.matches);
|
||||||
let id = get_id(env.rt, env.matches);
|
let id = get_id(env.rt, env.matches);
|
||||||
|
|
|
@ -23,15 +23,19 @@ pub struct BMModule {
|
||||||
|
|
||||||
const CALLNAMES : &'static [&'static str] = &[ "bm", "bookmark" ];
|
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 {
|
BMModule {
|
||||||
path: None
|
path: None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn callnames() -> &'static [&'static str] {
|
}
|
||||||
|
|
||||||
|
impl Module for BMModule {
|
||||||
|
|
||||||
|
fn callnames(&self) -> &'static [&'static str] {
|
||||||
CALLNAMES
|
CALLNAMES
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,12 +51,11 @@ pub struct CommandEnv<'a> {
|
||||||
|
|
||||||
pub type ModuleResult = Result<(), ModuleError>;
|
pub type ModuleResult = Result<(), ModuleError>;
|
||||||
pub type CommandResult = ModuleResult;
|
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 {
|
pub trait Module {
|
||||||
|
|
||||||
fn new(rt : &Runtime) -> Self;
|
fn callnames(&self) -> &'static [&'static str];
|
||||||
fn callnames() -> &'static [&'static str];
|
|
||||||
fn name(&self) -> &'static str;
|
fn name(&self) -> &'static str;
|
||||||
fn shutdown(&self, rt : &Runtime) -> ModuleResult;
|
fn shutdown(&self, rt : &Runtime) -> ModuleResult;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue