Add new ReplAction type

This commit is contained in:
greg 2019-06-01 18:41:55 -07:00
parent 3987360f8e
commit 176b286332
1 changed files with 16 additions and 3 deletions

View File

@ -3,7 +3,9 @@ use super::Repl;
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>; pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
/// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first /// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
/// Terminal, it will execute the `BoxedCommandFunction` found there with any remaining arguments /// Terminal, it will use the `ReplAction` found there to find an appropriate function to execute,
/// and then execute it with any remaining arguments
//TODO remove function: BoxedCommandFunction
#[derive(Clone)] #[derive(Clone)]
pub enum CommandTree { pub enum CommandTree {
Terminal { Terminal {
@ -11,22 +13,32 @@ pub enum CommandTree {
children: Vec<CommandTree>, children: Vec<CommandTree>,
help_msg: Option<String>, help_msg: Option<String>,
function: BoxedCommandFunction, function: BoxedCommandFunction,
action: ReplAction,
}, },
NonTerminal { NonTerminal {
name: String, name: String,
children: Vec<CommandTree>, children: Vec<CommandTree>,
help_msg: Option<String>, help_msg: Option<String>,
action: ReplAction,
}, },
//TODO get rid of Top
Top(Vec<CommandTree>), Top(Vec<CommandTree>),
} }
#[derive(Debug, Clone)]
pub enum ReplAction {
Null,
Help,
QuitProgram
}
impl CommandTree { impl CommandTree {
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> CommandTree { pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> CommandTree {
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![] } CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: ReplAction::Null }
} }
pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, function: BoxedCommandFunction) -> CommandTree { pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, function: BoxedCommandFunction) -> CommandTree {
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children } CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children, action: ReplAction::Null }
} }
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree { pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {
@ -34,6 +46,7 @@ impl CommandTree {
name: s.to_string(), name: s.to_string(),
help_msg: help.map(|x| x.to_string()), help_msg: help.map(|x| x.to_string()),
children, children,
action: ReplAction::Null
} }
} }