From 176b286332af65791b4a31ee14e4e3330d06131d Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 1 Jun 2019 18:41:55 -0700 Subject: [PATCH] Add new ReplAction type --- schala-repl/src/repl/command_tree.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/schala-repl/src/repl/command_tree.rs b/schala-repl/src/repl/command_tree.rs index c8cd05a..2a13d39 100644 --- a/schala-repl/src/repl/command_tree.rs +++ b/schala-repl/src/repl/command_tree.rs @@ -3,7 +3,9 @@ use super::Repl; pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option)>; /// 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)] pub enum CommandTree { Terminal { @@ -11,22 +13,32 @@ pub enum CommandTree { children: Vec, help_msg: Option, function: BoxedCommandFunction, + action: ReplAction, }, NonTerminal { name: String, children: Vec, help_msg: Option, + action: ReplAction, }, + //TODO get rid of Top Top(Vec), } +#[derive(Debug, Clone)] +pub enum ReplAction { + Null, + Help, + QuitProgram +} + impl 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, 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 { @@ -34,6 +46,7 @@ impl CommandTree { name: s.to_string(), help_msg: help.map(|x| x.to_string()), children, + action: ReplAction::Null } }