From 6460764dbbb8998d3afbba25591abf37d172ed29 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 11 Dec 2018 11:31:36 +0100 Subject: [PATCH] cli/command.rs: add new type CliCommandMap --- src/bin/pbs.rs | 30 +++++++++++++----------------- src/cli/command.rs | 36 ++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/bin/pbs.rs b/src/bin/pbs.rs index c8027974..22ffea52 100644 --- a/src/bin/pbs.rs +++ b/src/bin/pbs.rs @@ -7,32 +7,28 @@ use apitest::cli::command::*; fn datastore_commands() -> CommandLineInterface { - let mut cmd_def = HashMap::::new(); - use apitest::api3::config::datastore; - cmd_def.insert("list".to_owned(), CliCommand::new(datastore::get()).into()); - - cmd_def.insert("create".to_owned(), - CliCommand::new(datastore::post()) - .arg_param(vec!["name", "path"]) - .into()); - - cmd_def.insert("remove".to_owned(), - CliCommand::new(api3::config::datastore::delete()) - .arg_param(vec!["name"]) - .into()); + let cmd_def = CliCommandMap::new() + .insert("list", CliCommand::new(datastore::get()).into()) + .insert("create", + CliCommand::new(datastore::post()) + .arg_param(vec!["name", "path"]) + .into()) + .insert("remove", + CliCommand::new(datastore::delete()) + .arg_param(vec!["name"]) + .into()); cmd_def.into() } fn main() { - let mut cmd_def = HashMap::new(); + let cmd_def = CliCommandMap::new() + .insert("datastore".to_owned(), datastore_commands()); - cmd_def.insert("datastore".to_owned(), datastore_commands()); - - if let Err(err) = run_cli_command(&CommandLineInterface::Nested(cmd_def)) { + if let Err(err) = run_cli_command(&cmd_def.into()) { eprintln!("Error: {}", err); print_cli_usage(); std::process::exit(-1); diff --git a/src/cli/command.rs b/src/cli/command.rs index cdf89977..8929de59 100644 --- a/src/cli/command.rs +++ b/src/cli/command.rs @@ -27,32 +27,32 @@ fn handle_simple_command(cli_cmd: &CliCommand, args: Vec) -> Result<(), Ok(()) } -fn find_command<'a>(def: &'a HashMap, name: &str) -> Option<&'a CommandLineInterface> { +fn find_command<'a>(def: &'a CliCommandMap, name: &str) -> Option<&'a CommandLineInterface> { - if let Some(sub_cmd) = def.get(name) { + if let Some(sub_cmd) = def.commands.get(name) { return Some(sub_cmd); }; let mut matches: Vec<&str> = vec![]; - for cmd in def.keys() { + for cmd in def.commands.keys() { if cmd.starts_with(name) { matches.push(cmd); } } if matches.len() != 1 { return None; } - if let Some(sub_cmd) = def.get(matches[0]) { + if let Some(sub_cmd) = def.commands.get(matches[0]) { return Some(sub_cmd); }; None } -fn handle_nested_command(def: &HashMap, mut args: Vec) -> Result<(), Error> { +fn handle_nested_command(def: &CliCommandMap, mut args: Vec) -> Result<(), Error> { if args.len() < 1 { - let mut cmds: Vec<&String> = def.keys().collect(); + let mut cmds: Vec<&String> = def.commands.keys().collect(); cmds.sort(); let list = cmds.iter().fold(String::new(),|mut s,item| { @@ -116,9 +116,25 @@ impl CliCommand { } } +pub struct CliCommandMap { + pub commands: HashMap, +} + +impl CliCommandMap { + + pub fn new() -> Self { + Self { commands: HashMap:: new() } + } + + pub fn insert>(mut self, name: S, cli: CommandLineInterface) -> Self { + self.commands.insert(name.into(), cli); + self + } +} + pub enum CommandLineInterface { Simple(CliCommand), - Nested(HashMap), + Nested(CliCommandMap), } impl From for CommandLineInterface { @@ -127,8 +143,8 @@ impl From for CommandLineInterface { } } -impl From> for CommandLineInterface { - fn from(map: HashMap) -> Self { - CommandLineInterface::Nested(map) +impl From for CommandLineInterface { + fn from(list: CliCommandMap) -> Self { + CommandLineInterface::Nested(list) } }