add find_node

This commit is contained in:
mat 2022-04-18 15:28:25 +00:00
parent d25c9926d7
commit 3f87fc5068
2 changed files with 22 additions and 0 deletions

View file

@ -196,6 +196,18 @@ impl<S> CommandDispatcher<S> {
vec![]
}
pub fn find_node(&self, path: &[&str]) -> Option<Rc<RefCell<CommandNode<S>>>> {
let mut node = self.root.clone();
for name in path {
if let Some(child) = node.clone().borrow().child(name) {
node = child
} else {
return None;
}
}
Some(node)
}
/// Executes a given pre-parsed command.
pub fn execute_parsed(parse: ParseResults<S>) -> Result<i32, CommandSyntaxException> {
if parse.reader.can_read() {
@ -993,4 +1005,10 @@ mod tests {
// public void testFindNodeDoesntExist() {
// assertThat(subject.findNode(Lists.newArrayList("foo", "bar")), is(nullValue()));
// }
#[test]
fn find_node_doesnt_exist() {
let subject = CommandDispatcher::<()>::new();
assert_eq!(subject.find_node(&vec!["foo", "bar"]), None)
}
}

View file

@ -138,6 +138,10 @@ impl<S> CommandNode<S> {
}
}
pub fn child(&self, name: &str) -> Option<Rc<RefCell<CommandNode<S>>>> {
self.children.get(name).cloned()
}
pub fn parse_with_context(
&self,
reader: &mut StringReader,