aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/command_dispatcher.rs
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2026-05-06 18:38:23 -0545
committermat <git@matdoes.dev>2026-05-07 08:05:58 -1200
commitcabc8b60a729ba17f5b75f7a7956c6d1ddcc8919 (patch)
tree237fd12a9768fe7431ce42dfbdde60f4c7850e06 /azalea-brigadier/src/command_dispatcher.rs
parent9ffd0e80bbb3feace231553d6539124585b03e3c (diff)
downloadazalea-drasl-cabc8b60a729ba17f5b75f7a7956c6d1ddcc8919.tar.xz
azalea-brigadier now allows commands to return a Result
Diffstat (limited to 'azalea-brigadier/src/command_dispatcher.rs')
-rw-r--r--azalea-brigadier/src/command_dispatcher.rs56
1 files changed, 28 insertions, 28 deletions
diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs
index ecf5d157..5b162964 100644
--- a/azalea-brigadier/src/command_dispatcher.rs
+++ b/azalea-brigadier/src/command_dispatcher.rs
@@ -11,7 +11,7 @@ use parking_lot::RwLock;
use crate::{
builder::argument_builder::ArgumentBuilder,
context::{CommandContextBuilder, ContextChain},
- errors::{BuiltInError, CommandSyntaxError},
+ errors::{BuiltInError, CommandResultTrait, CommandSyntaxError},
parse_results::ParseResults,
result_consumer::{DefaultResultConsumer, ResultConsumer},
string_reader::StringReader,
@@ -26,15 +26,15 @@ use crate::{
/// # struct CommandSource;
/// let mut subject = CommandDispatcher::<CommandSource>::new();
/// ```
-pub struct CommandDispatcher<S>
+pub struct CommandDispatcher<S, R = i32>
where
Self: Sync + Send,
{
- pub root: Arc<RwLock<CommandNode<S>>>,
- consumer: Box<dyn ResultConsumer<S> + Send + Sync>,
+ pub root: Arc<RwLock<CommandNode<S, R>>>,
+ consumer: Box<dyn ResultConsumer<S, R> + Send + Sync>,
}
-impl<S> CommandDispatcher<S> {
+impl<S, R: CommandResultTrait> CommandDispatcher<S, R> {
pub fn new() -> Self {
Self {
root: Arc::new(RwLock::new(CommandNode::default())),
@@ -49,13 +49,13 @@ impl<S> CommandDispatcher<S> {
/// # let mut subject = CommandDispatcher::<()>::new();
/// subject.register(literal("foo").executes(|_| 42));
/// ```
- pub fn register(&mut self, node: ArgumentBuilder<S>) -> Arc<RwLock<CommandNode<S>>> {
+ pub fn register(&mut self, node: ArgumentBuilder<S, R>) -> Arc<RwLock<CommandNode<S, R>>> {
let build = Arc::new(RwLock::new(node.build()));
self.root.write().add_child(&build);
build
}
- pub fn parse(&self, command: StringReader, source: S) -> ParseResults<'_, S> {
+ pub fn parse(&self, command: StringReader, source: S) -> ParseResults<'_, S, R> {
let source = Arc::new(source);
let context = CommandContextBuilder::new(self, source, self.root.clone(), command.cursor());
@@ -64,14 +64,14 @@ impl<S> CommandDispatcher<S> {
fn parse_nodes<'a>(
&'a self,
- node: &Arc<RwLock<CommandNode<S>>>,
+ node: &Arc<RwLock<CommandNode<S, R>>>,
original_reader: &StringReader,
- context_so_far: CommandContextBuilder<'a, S>,
- ) -> Result<ParseResults<'a, S>, CommandSyntaxError> {
+ context_so_far: CommandContextBuilder<'a, S, R>,
+ ) -> Result<ParseResults<'a, S, R>, CommandSyntaxError> {
let source = context_so_far.source.clone();
#[allow(clippy::mutable_key_type)] // this is fine because we don't mutate the key
- let mut errors = HashMap::<Rc<CommandNode<S>>, CommandSyntaxError>::new();
- let mut potentials: Vec<ParseResults<S>> = vec![];
+ let mut errors = HashMap::<Rc<CommandNode<S, R>>, CommandSyntaxError>::new();
+ let mut potentials: Vec<ParseResults<S, R>> = vec![];
let cursor = original_reader.cursor();
for child in node.read().get_relevant_nodes(&mut original_reader.clone()) {
@@ -183,7 +183,7 @@ impl<S> CommandDispatcher<S> {
&self,
input: impl Into<StringReader>,
source: S,
- ) -> Result<i32, CommandSyntaxError> {
+ ) -> Result<R, CommandSyntaxError> {
let input = input.into();
let parse = self.parse(input, source);
@@ -191,9 +191,9 @@ impl<S> CommandDispatcher<S> {
}
pub fn add_paths(
- node: Arc<RwLock<CommandNode<S>>>,
- result: &mut Vec<Vec<Arc<RwLock<CommandNode<S>>>>>,
- parents: Vec<Arc<RwLock<CommandNode<S>>>>,
+ node: Arc<RwLock<CommandNode<S, R>>>,
+ result: &mut Vec<Vec<Arc<RwLock<CommandNode<S, R>>>>>,
+ parents: Vec<Arc<RwLock<CommandNode<S, R>>>>,
) {
let mut current = parents;
current.push(node.clone());
@@ -204,9 +204,9 @@ impl<S> CommandDispatcher<S> {
}
}
- pub fn get_path(&self, target: CommandNode<S>) -> Vec<String> {
+ pub fn get_path(&self, target: CommandNode<S, R>) -> Vec<String> {
let rc_target = Arc::new(RwLock::new(target));
- let mut nodes: Vec<Vec<Arc<RwLock<CommandNode<S>>>>> = Vec::new();
+ let mut nodes: Vec<Vec<Arc<RwLock<CommandNode<S, R>>>>> = Vec::new();
Self::add_paths(self.root.clone(), &mut nodes, vec![]);
for list in nodes {
@@ -223,7 +223,7 @@ impl<S> CommandDispatcher<S> {
vec![]
}
- pub fn find_node(&self, path: &[&str]) -> Option<Arc<RwLock<CommandNode<S>>>> {
+ pub fn find_node(&self, path: &[&str]) -> Option<Arc<RwLock<CommandNode<S, R>>>> {
let mut node = self.root.clone();
for name in path {
match node.clone().read().child(name) {
@@ -239,7 +239,7 @@ impl<S> CommandDispatcher<S> {
}
/// Executes a given pre-parsed command.
- pub fn execute_parsed(&self, parse: ParseResults<S>) -> Result<i32, CommandSyntaxError> {
+ pub fn execute_parsed(&self, parse: ParseResults<S, R>) -> Result<R, CommandSyntaxError> {
if parse.reader.can_read() {
return Err(if parse.exceptions.len() == 1 {
parse.exceptions.values().next().unwrap().clone()
@@ -263,7 +263,7 @@ impl<S> CommandDispatcher<S> {
pub fn get_all_usage(
&self,
- node: &CommandNode<S>,
+ node: &CommandNode<S, R>,
source: &S,
restricted: bool,
) -> Vec<String> {
@@ -274,7 +274,7 @@ impl<S> CommandDispatcher<S> {
fn get_all_usage_recursive(
&self,
- node: &CommandNode<S>,
+ node: &CommandNode<S, R>,
source: &S,
result: &mut Vec<String>,
prefix: &str,
@@ -325,9 +325,9 @@ impl<S> CommandDispatcher<S> {
/// command tree.
pub fn get_smart_usage(
&self,
- node: &CommandNode<S>,
+ node: &CommandNode<S, R>,
source: &S,
- ) -> Vec<(Arc<RwLock<CommandNode<S>>>, String)> {
+ ) -> Vec<(Arc<RwLock<CommandNode<S, R>>>, String)> {
let mut result = Vec::new();
let optional = node.command.is_some();
@@ -343,7 +343,7 @@ impl<S> CommandDispatcher<S> {
fn get_smart_usage_recursive(
&self,
- node: &CommandNode<S>,
+ node: &CommandNode<S, R>,
source: &S,
optional: bool,
deep: bool,
@@ -435,13 +435,13 @@ impl<S> CommandDispatcher<S> {
Some(this)
}
- pub fn get_completion_suggestions(parse: ParseResults<S>) -> Suggestions {
+ pub fn get_completion_suggestions(parse: ParseResults<S, R>) -> Suggestions {
let cursor = parse.reader.total_length();
Self::get_completion_suggestions_with_cursor(parse, cursor)
}
pub fn get_completion_suggestions_with_cursor(
- parse: ParseResults<S>,
+ parse: ParseResults<S, R>,
cursor: usize,
) -> Suggestions {
let context = parse.context;
@@ -471,7 +471,7 @@ impl<S> CommandDispatcher<S> {
}
}
-impl<S> Default for CommandDispatcher<S> {
+impl<S, R: CommandResultTrait> Default for CommandDispatcher<S, R> {
fn default() -> Self {
Self::new()
}