2019-06-02 00:18:37 -07:00
|
|
|
use super::{Repl, InterpreterDirectiveOutput};
|
2019-06-02 00:27:12 -07:00
|
|
|
use crate::repl::directive_actions::DirectiveAction;
|
2019-06-01 22:17:20 -07:00
|
|
|
use colored::*;
|
|
|
|
|
2019-03-16 18:33:31 -07:00
|
|
|
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
|
2018-10-15 20:52:34 -07:00
|
|
|
|
2019-05-22 03:10:12 -07:00
|
|
|
/// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
|
2019-06-02 00:18:37 -07:00
|
|
|
/// Terminal, it will use the `DirectiveAction` found there to find an appropriate function to execute,
|
2019-06-01 18:41:55 -07:00
|
|
|
/// and then execute it with any remaining arguments
|
|
|
|
//TODO remove function: BoxedCommandFunction
|
2018-10-15 20:52:34 -07:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum CommandTree {
|
|
|
|
Terminal {
|
|
|
|
name: String,
|
2019-03-26 19:43:11 -07:00
|
|
|
children: Vec<CommandTree>,
|
2018-10-15 20:52:34 -07:00
|
|
|
help_msg: Option<String>,
|
2019-03-26 19:43:11 -07:00
|
|
|
function: BoxedCommandFunction,
|
2019-06-02 00:18:37 -07:00
|
|
|
action: DirectiveAction,
|
2018-10-15 20:52:34 -07:00
|
|
|
},
|
|
|
|
NonTerminal {
|
|
|
|
name: String,
|
|
|
|
children: Vec<CommandTree>,
|
|
|
|
help_msg: Option<String>,
|
2019-06-02 00:18:37 -07:00
|
|
|
action: DirectiveAction,
|
2018-10-15 20:52:34 -07:00
|
|
|
},
|
2019-06-01 18:41:55 -07:00
|
|
|
//TODO get rid of Top
|
2018-10-15 20:52:34 -07:00
|
|
|
Top(Vec<CommandTree>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandTree {
|
2019-03-26 19:43:11 -07:00
|
|
|
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> CommandTree {
|
2019-06-02 00:18:37 -07:00
|
|
|
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: DirectiveAction::Null }
|
2018-10-15 20:52:34 -07:00
|
|
|
}
|
2018-10-15 21:46:27 -07:00
|
|
|
|
2019-05-22 03:10:12 -07:00
|
|
|
pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, function: BoxedCommandFunction) -> CommandTree {
|
2019-06-02 00:18:37 -07:00
|
|
|
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children, action: DirectiveAction::Null }
|
2019-03-14 03:47:56 -07:00
|
|
|
}
|
|
|
|
|
2019-06-02 00:18:37 -07:00
|
|
|
pub fn terminal_act(s: &str, help: Option<&str>, children: Vec<CommandTree>, action: DirectiveAction) -> CommandTree {
|
2019-06-01 22:17:20 -07:00
|
|
|
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: Box::new(|_,_| { None }), children, action}
|
2019-03-19 04:28:54 -07:00
|
|
|
}
|
|
|
|
|
2019-06-01 22:17:20 -07:00
|
|
|
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {
|
2019-03-19 04:28:54 -07:00
|
|
|
CommandTree::NonTerminal {
|
|
|
|
name: s.to_string(),
|
|
|
|
help_msg: help.map(|x| x.to_string()),
|
|
|
|
children,
|
2019-06-02 00:18:37 -07:00
|
|
|
action: DirectiveAction::Null
|
2018-10-15 21:46:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:52:34 -07:00
|
|
|
pub fn get_cmd(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
CommandTree::Terminal { name, .. } => name.as_str(),
|
|
|
|
CommandTree::NonTerminal {name, ..} => name.as_str(),
|
|
|
|
CommandTree::Top(_) => "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn get_help(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
|
|
|
CommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
|
|
|
CommandTree::Top(_) => ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn get_children(&self) -> Vec<&str> {
|
2019-03-27 02:20:43 -07:00
|
|
|
use CommandTree::*;
|
2018-10-15 20:52:34 -07:00
|
|
|
match self {
|
2019-03-27 02:20:43 -07:00
|
|
|
Terminal { children, .. } |
|
|
|
|
NonTerminal { children, .. } |
|
|
|
|
Top(children) => children.iter().map(|x| x.get_cmd()).collect()
|
2018-10-15 20:52:34 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-01 22:17:20 -07:00
|
|
|
|
2019-06-02 00:18:37 -07:00
|
|
|
pub fn perform(&self, repl: &mut Repl, arguments: &Vec<&str>) -> InterpreterDirectiveOutput {
|
2019-06-01 22:17:20 -07:00
|
|
|
let mut dir_pointer: &CommandTree = self;
|
|
|
|
let mut idx = 0;
|
|
|
|
|
2019-06-02 00:18:37 -07:00
|
|
|
let res: Result<(DirectiveAction, usize), String> = loop {
|
2019-06-01 22:17:20 -07:00
|
|
|
match dir_pointer {
|
|
|
|
CommandTree::Top(subcommands) | CommandTree::NonTerminal { children: subcommands, .. } => {
|
|
|
|
let next_command = match arguments.get(idx) {
|
|
|
|
Some(cmd) => cmd,
|
|
|
|
None => break Err(format!("Command requires arguments"))
|
|
|
|
};
|
|
|
|
idx += 1;
|
|
|
|
match subcommands.iter().find(|sc| sc.get_cmd() == *next_command) {
|
|
|
|
Some(command_tree) => {
|
|
|
|
dir_pointer = command_tree;
|
|
|
|
},
|
|
|
|
None => break Err(format!("Command {} not found", next_command))
|
|
|
|
};
|
|
|
|
},
|
|
|
|
CommandTree::Terminal { action, .. } => {
|
|
|
|
break Ok((action.clone(), idx));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
2019-06-02 00:18:37 -07:00
|
|
|
Ok((action, idx)) => action.perform(repl, &arguments[idx..]),
|
2019-06-01 22:17:20 -07:00
|
|
|
Err(err) => Some(err.red().to_string())
|
|
|
|
}
|
|
|
|
}
|
2018-10-15 20:52:34 -07:00
|
|
|
}
|