1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

add .requires

This commit is contained in:
mat 2022-04-17 16:52:34 -05:00
parent 2e90422561
commit 6f3c41e01c
3 changed files with 48 additions and 13 deletions

View file

@ -66,6 +66,14 @@ impl<S: Any + Clone> ArgumentBuilder<S> {
self.clone() self.clone()
} }
pub fn requires<F>(&mut self, requirement: F) -> Self
where
F: Fn(Rc<S>) -> bool + 'static,
{
self.requirement = Rc::new(requirement);
self.clone()
}
pub fn build(self) -> CommandNode<S> { pub fn build(self) -> CommandNode<S> {
CommandNode { CommandNode {
value: self.value, value: self.value,

View file

@ -330,18 +330,45 @@ mod tests {
// assertThat(ex.getCursor(), is(0)); // assertThat(ex.getCursor(), is(0));
// } // }
// } // }
// #[test] #[test]
// fn execute_unknown_command() { fn execute_unknown_command() {
// let mut subject = CommandDispatcher::<Rc<CommandSource>>::new(); let mut subject = CommandDispatcher::<Rc<CommandSource>>::new();
// subject.register(literal("bar")); subject.register(literal("bar"));
// subject.register(literal("baz")); subject.register(literal("baz"));
// assert_eq!( let execute_result = subject.execute("foo".into(), Rc::new(CommandSource {}));
// subject
// .execute("foo".into(), Rc::new(CommandSource {})) let err = execute_result.err().unwrap();
// .err() match err.type_ {
// .unwrap(), BuiltInExceptions::DispatcherUnknownCommand => {}
// BuiltInExceptions::DispatcherUnknownCommand.create() _ => panic!("Unexpected error"),
// ); }
assert_eq!(err.cursor().unwrap(), 0);
}
// @Test
// public void testExecuteImpermissibleCommand() throws Exception {
// subject.register(literal("foo").requires(s -> false));
// try {
// subject.execute("foo", source);
// fail();
// } catch (final CommandSyntaxException ex) {
// assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
// assertThat(ex.getCursor(), is(0));
// }
// } // }
#[test]
fn execute_impermissible_command() {
let mut subject = CommandDispatcher::<Rc<CommandSource>>::new();
subject.register(literal("foo").requires(|_| false));
let execute_result = subject.execute("foo".into(), Rc::new(CommandSource {}));
let err = execute_result.err().unwrap();
match err.type_ {
BuiltInExceptions::DispatcherUnknownCommand => {}
_ => panic!("Unexpected error"),
}
assert_eq!(err.cursor().unwrap(), 0);
}
} }

View file

@ -5,7 +5,7 @@ use crate::message::Message;
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct CommandSyntaxException { pub struct CommandSyntaxException {
type_: BuiltInExceptions, pub type_: BuiltInExceptions,
message: Message, message: Message,
input: Option<String>, input: Option<String>,
cursor: Option<usize>, cursor: Option<usize>,