mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
Implement working redirects
This commit is contained in:
parent
14625e2bce
commit
af4b0d0add
3 changed files with 33 additions and 37 deletions
|
@ -1,7 +1,7 @@
|
|||
use crate::{context::CommandContext, modifier::RedirectModifier, tree::CommandNode};
|
||||
|
||||
use super::{literal_argument_builder::Literal, required_argument_builder::Argument};
|
||||
use std::{any::Any, cell::RefCell, collections::BTreeMap, fmt::Debug, rc::Rc};
|
||||
use std::{any::Any, cell::RefCell, fmt::Debug, rc::Rc};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ArgumentBuilderType {
|
||||
|
@ -38,6 +38,7 @@ impl<S: Any + Clone> 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 {
|
||||
self.arguments
|
||||
.add_child(&Rc::new(RefCell::new(argument.build())));
|
||||
|
@ -82,10 +83,10 @@ impl<S: Any + Clone> ArgumentBuilder<S> {
|
|||
pub fn build(self) -> CommandNode<S> {
|
||||
let mut result = CommandNode {
|
||||
value: self.arguments.value,
|
||||
command: self.command.clone(),
|
||||
requirement: self.requirement.clone(),
|
||||
redirect: self.target.clone(),
|
||||
modifier: self.modifier.clone(),
|
||||
command: self.command,
|
||||
requirement: self.requirement,
|
||||
redirect: self.target,
|
||||
modifier: self.modifier,
|
||||
forks: self.forks,
|
||||
..Default::default()
|
||||
};
|
||||
|
|
|
@ -104,6 +104,11 @@ impl<S: Any + Clone> CommandDispatcher<S> {
|
|||
.parse_nodes(redirect, &reader, child_context)
|
||||
.expect("Parsing nodes failed");
|
||||
context.with_child(Rc::new(parse.context));
|
||||
return Ok(ParseResults {
|
||||
context,
|
||||
reader: parse.reader,
|
||||
exceptions: parse.exceptions,
|
||||
});
|
||||
} else {
|
||||
let parse = self
|
||||
.parse_nodes(&child, &reader, context)
|
||||
|
@ -629,25 +634,25 @@ mod tests {
|
|||
// verify(subCommand).run(any(CommandContext.class));
|
||||
// verify(command, never()).run(any());
|
||||
// }
|
||||
// #[test]
|
||||
// fn test_execute_ambiguious_parent_subcommand_via_redirect() {
|
||||
// let mut subject = CommandDispatcher::new();
|
||||
#[test]
|
||||
fn test_execute_ambiguious_parent_subcommand_via_redirect() {
|
||||
let mut subject = CommandDispatcher::new();
|
||||
|
||||
// let real = subject.register(
|
||||
// literal("test")
|
||||
// .then(argument("incorrect", integer()).executes(|_| 42))
|
||||
// .then(
|
||||
// argument("right", integer()).then(argument("sub", integer()).executes(|_| 100)),
|
||||
// ),
|
||||
// );
|
||||
let real = subject.register(
|
||||
literal("test")
|
||||
.then(argument("incorrect", integer()).executes(|_| 42))
|
||||
.then(
|
||||
argument("right", integer()).then(argument("sub", integer()).executes(|_| 100)),
|
||||
),
|
||||
);
|
||||
|
||||
// subject.register(literal("redirect").redirect(real));
|
||||
subject.register(literal("redirect").redirect(real));
|
||||
|
||||
// assert_eq!(
|
||||
// subject
|
||||
// .execute("redirect 1 2".into(), Rc::new(CommandSource {}))
|
||||
// .unwrap(),
|
||||
// 100
|
||||
// );
|
||||
// }
|
||||
assert_eq!(
|
||||
subject
|
||||
.execute("redirect 1 2".into(), Rc::new(CommandSource {}))
|
||||
.unwrap(),
|
||||
100
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,15 +32,6 @@ pub struct CommandNode<S: Any + Clone> {
|
|||
}
|
||||
|
||||
impl<S: Any + Clone> CommandNode<S> {
|
||||
// pub fn new()
|
||||
// TODO: precalculate `literals` and `arguments` and include them in CommandNode
|
||||
fn literals(&self) -> &BTreeMap<String, Rc<RefCell<CommandNode<S>>>> {
|
||||
&self.literals
|
||||
}
|
||||
fn arguments(&self) -> &BTreeMap<String, Rc<RefCell<CommandNode<S>>>> {
|
||||
&self.arguments
|
||||
}
|
||||
|
||||
/// Gets the literal, or panics. You should use match if you're not certain about the type.
|
||||
pub fn literal(&self) -> &Literal {
|
||||
match self.value {
|
||||
|
@ -57,7 +48,7 @@ impl<S: Any + Clone> CommandNode<S> {
|
|||
}
|
||||
|
||||
pub fn get_relevant_nodes(&self, input: &mut StringReader) -> Vec<Rc<RefCell<CommandNode<S>>>> {
|
||||
let literals = self.literals();
|
||||
let literals = &self.literals;
|
||||
|
||||
if !literals.is_empty() {
|
||||
let cursor = input.cursor();
|
||||
|
@ -75,10 +66,10 @@ impl<S: Any + Clone> CommandNode<S> {
|
|||
if let Some(literal) = literal {
|
||||
return vec![literal.clone()];
|
||||
} else {
|
||||
return self.arguments().values().cloned().collect();
|
||||
return self.arguments.values().cloned().collect();
|
||||
}
|
||||
} else {
|
||||
self.arguments().values().cloned().collect()
|
||||
self.arguments.values().cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -245,8 +236,7 @@ impl<S: Any + Clone> PartialEq for CommandNode<S> {
|
|||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if other.command.is_some() {
|
||||
} else if other.command.is_some() {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
|
|
Loading…
Add table
Reference in a new issue