mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 14:26:04 +00:00
add get_path
This commit is contained in:
parent
b7d4b437dc
commit
d25c9926d7
2 changed files with 41 additions and 6 deletions
|
@ -53,10 +53,12 @@ impl<S> ArgumentBuilder<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// do we need to be cloning here? maybe we could return a ref to self?
|
|
||||||
pub fn then(&mut self, argument: ArgumentBuilder<S>) -> Self {
|
pub fn then(&mut self, argument: ArgumentBuilder<S>) -> Self {
|
||||||
self.arguments
|
self.then_built(argument.build())
|
||||||
.add_child(&Rc::new(RefCell::new(argument.build())));
|
}
|
||||||
|
|
||||||
|
pub fn then_built(&mut self, argument: CommandNode<S>) -> Self {
|
||||||
|
self.arguments.add_child(&Rc::new(RefCell::new(argument)));
|
||||||
self.clone()
|
self.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -162,6 +162,40 @@ impl<S> CommandDispatcher<S> {
|
||||||
Self::execute_parsed(parse)
|
Self::execute_parsed(parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_paths(
|
||||||
|
&self,
|
||||||
|
node: Rc<RefCell<CommandNode<S>>>,
|
||||||
|
result: &mut Vec<Vec<Rc<RefCell<CommandNode<S>>>>>,
|
||||||
|
parents: Vec<Rc<RefCell<CommandNode<S>>>>,
|
||||||
|
) {
|
||||||
|
let mut current = parents.clone();
|
||||||
|
current.push(node.clone());
|
||||||
|
result.push(current.clone());
|
||||||
|
|
||||||
|
for child in node.borrow().children.values() {
|
||||||
|
self.add_paths(child.clone(), result, current.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_path(&self, target: CommandNode<S>) -> Vec<String> {
|
||||||
|
let rc_target = Rc::new(RefCell::new(target.clone()));
|
||||||
|
let mut nodes: Vec<Vec<Rc<RefCell<CommandNode<S>>>>> = Vec::new();
|
||||||
|
self.add_paths(self.root.clone(), &mut nodes, vec![]);
|
||||||
|
|
||||||
|
for list in nodes {
|
||||||
|
if *list.last().expect("Nothing in list").borrow() == *rc_target.borrow() {
|
||||||
|
let mut result: Vec<String> = Vec::with_capacity(list.len());
|
||||||
|
for node in list {
|
||||||
|
if node != self.root {
|
||||||
|
result.push(node.borrow().name().to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
|
||||||
/// Executes a given pre-parsed command.
|
/// Executes a given pre-parsed command.
|
||||||
pub fn execute_parsed(parse: ParseResults<S>) -> Result<i32, CommandSyntaxException> {
|
pub fn execute_parsed(parse: ParseResults<S>) -> Result<i32, CommandSyntaxException> {
|
||||||
if parse.reader.can_read() {
|
if parse.reader.can_read() {
|
||||||
|
@ -236,7 +270,6 @@ impl<S> CommandDispatcher<S> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("forked: {}, successful forks: {}", forked, successful_forks);
|
|
||||||
// TODO: this is not how vanilla does it but it works
|
// TODO: this is not how vanilla does it but it works
|
||||||
Ok(if successful_forks >= 2 {
|
Ok(if successful_forks >= 2 {
|
||||||
successful_forks
|
successful_forks
|
||||||
|
@ -945,10 +978,10 @@ mod tests {
|
||||||
// }
|
// }
|
||||||
#[test]
|
#[test]
|
||||||
fn get_path() {
|
fn get_path() {
|
||||||
let mut subject = CommandDispatcher::new();
|
let mut subject = CommandDispatcher::<()>::new();
|
||||||
|
|
||||||
let bar = literal("bar").build();
|
let bar = literal("bar").build();
|
||||||
subject.register(literal("foo").then(bar));
|
subject.register(literal("foo").then_built(bar.clone()));
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
subject.get_path(bar),
|
subject.get_path(bar),
|
||||||
|
|
Loading…
Add table
Reference in a new issue