2019-03-14 03:47:56 -07:00
|
|
|
use super::Repl;
|
|
|
|
|
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
|
|
|
|
|
|
|
#[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,
|
2018-10-15 20:52:34 -07:00
|
|
|
},
|
|
|
|
NonTerminal {
|
|
|
|
name: String,
|
|
|
|
children: Vec<CommandTree>,
|
|
|
|
help_msg: Option<String>,
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![] }
|
2018-10-15 20:52:34 -07:00
|
|
|
}
|
2018-10-15 21:46:27 -07:00
|
|
|
|
2019-03-26 19:43:11 -07:00
|
|
|
pub fn term_with_function(s: &str, help: Option<&str>, function: BoxedCommandFunction) -> CommandTree {
|
|
|
|
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children: vec![] }
|
2019-03-14 03:47:56 -07:00
|
|
|
}
|
|
|
|
|
2018-10-15 21:46:27 -07:00
|
|
|
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {
|
|
|
|
CommandTree::NonTerminal {
|
|
|
|
name: s.to_string(),
|
|
|
|
help_msg: help.map(|x| x.to_string()),
|
|
|
|
children,
|
2019-03-19 04:28:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:43:11 -07:00
|
|
|
/*
|
2019-03-19 04:28:54 -07:00
|
|
|
pub fn nonterm_with_function(s: &str, help: Option<&str>, children: Vec<CommandTree>, func: BoxedCommandFunction) -> CommandTree {
|
|
|
|
CommandTree::NonTerminal {
|
|
|
|
name: s.to_string(),
|
|
|
|
help_msg: help.map(|x| x.to_string()),
|
|
|
|
children,
|
|
|
|
function: Some(func),
|
2018-10-15 21:46:27 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 19:43:11 -07:00
|
|
|
*/
|
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> {
|
|
|
|
match self {
|
|
|
|
CommandTree::Terminal { .. } => vec![],
|
|
|
|
CommandTree::NonTerminal { children, .. } => children.iter().map(|x| x.get_cmd()).collect(),
|
|
|
|
CommandTree::Top(children) => children.iter().map(|x| x.get_cmd()).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|