start adding functions to command data structure

This commit is contained in:
greg 2019-03-14 03:47:56 -07:00
parent 2929362046
commit 0451676ba7

View File

@ -1,16 +1,18 @@
use super::Repl;
type BoxedCommandFunction = Box<(fn(&mut Repl, Vec<String>) -> Option<String>)>;
#[derive(Clone)] #[derive(Clone)]
pub enum CommandTree { pub enum CommandTree {
Terminal { Terminal {
name: String, name: String,
help_msg: Option<String>, help_msg: Option<String>,
function: Option<Box<(fn() -> Option<String>)>>, function: Option<BoxedCommandFunction>,
}, },
NonTerminal { NonTerminal {
name: String, name: String,
children: Vec<CommandTree>, children: Vec<CommandTree>,
help_msg: Option<String>, help_msg: Option<String>,
function: Option<Box<(fn() -> Option<String>)>>,
}, },
Top(Vec<CommandTree>), Top(Vec<CommandTree>),
} }
@ -20,12 +22,15 @@ impl CommandTree {
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: None } CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: None }
} }
pub fn term_with_function(s: &str, help: Option<&str>, func: BoxedCommandFunction) -> CommandTree {
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: Some(func) }
}
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree { pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {
CommandTree::NonTerminal { CommandTree::NonTerminal {
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,
function: None,
} }
} }