From cabc8b60a729ba17f5b75f7a7956c6d1ddcc8919 Mon Sep 17 00:00:00 2001 From: mat Date: Wed, 6 May 2026 18:38:23 -0545 Subject: azalea-brigadier now allows commands to return a Result --- azalea-brigadier/src/tree/mod.rs | 49 +++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 23 deletions(-) (limited to 'azalea-brigadier/src/tree') diff --git a/azalea-brigadier/src/tree/mod.rs b/azalea-brigadier/src/tree/mod.rs index b9b11fd6..4cbe2dd5 100644 --- a/azalea-brigadier/src/tree/mod.rs +++ b/azalea-brigadier/src/tree/mod.rs @@ -20,27 +20,27 @@ use crate::{ suggestion::{Suggestions, SuggestionsBuilder}, }; -pub type Command = - Option) -> Result + Send + Sync>>; +pub type Command = + Option) -> Result + Send + Sync>>; /// An ArgumentBuilder that has been built. #[non_exhaustive] -pub struct CommandNode { - pub value: ArgumentBuilderType, +pub struct CommandNode { + pub value: ArgumentBuilderType, // this is a BTreeMap because children need to be ordered when getting command suggestions - pub children: BTreeMap>>>, - pub literals: HashMap>>>, - pub arguments: HashMap>>>, + pub children: BTreeMap>>>, + pub literals: HashMap>>>, + pub arguments: HashMap>>>, - pub command: Command, + pub command: Command, pub requirement: Arc bool + Send + Sync>, - pub redirect: Option>>>, + pub redirect: Option>>>, pub forks: bool, - pub modifier: Option>>, + pub modifier: Option>>, } -impl Clone for CommandNode { +impl Clone for CommandNode { fn clone(&self) -> Self { Self { value: self.value.clone(), @@ -56,7 +56,7 @@ impl Clone for CommandNode { } } -impl CommandNode { +impl CommandNode { /// Returns the value as a literal from this command node, assuming it's /// already been checked. /// @@ -77,14 +77,17 @@ impl CommandNode { /// /// Will panic if this node is not an argument. Consider using a match /// statement instead. - pub fn argument(&self) -> &Argument { + pub fn argument(&self) -> &Argument { match self.value { ArgumentBuilderType::Argument(ref argument) => argument, _ => panic!("CommandNode::argument() called on non-argument node"), } } - pub fn get_relevant_nodes(&self, input: &mut StringReader) -> Vec>>> { + pub fn get_relevant_nodes( + &self, + input: &mut StringReader, + ) -> Vec>>> { let literals = &self.literals; if literals.is_empty() { @@ -114,7 +117,7 @@ impl CommandNode { (self.requirement)(source) } - pub fn add_child(&mut self, node: &Arc>>) { + pub fn add_child(&mut self, node: &Arc>>) { let child = self.children.get(node.read().name()); if let Some(child) = child { // We've found something to merge onto @@ -152,14 +155,14 @@ impl CommandNode { } } - pub fn child(&self, name: &str) -> Option>>> { + pub fn child(&self, name: &str) -> Option>>> { self.children.get(name).cloned() } pub fn parse_with_context( &self, reader: &mut StringReader, - context_builder: &mut CommandContextBuilder, + context_builder: &mut CommandContextBuilder, ) -> Result<(), CommandSyntaxError> { match self.value { ArgumentBuilderType::Argument(ref argument) => { @@ -225,7 +228,7 @@ impl CommandNode { pub fn list_suggestions( &self, - context: CommandContext, + context: CommandContext, builder: SuggestionsBuilder, ) -> Suggestions { match &self.value { @@ -245,7 +248,7 @@ impl CommandNode { } } -impl Debug for CommandNode { +impl Debug for CommandNode { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("CommandNode") // .field("value", &self.value) @@ -259,7 +262,7 @@ impl Debug for CommandNode { } } -impl Default for CommandNode { +impl Default for CommandNode { fn default() -> Self { Self { value: ArgumentBuilderType::Literal(Literal::default()), @@ -277,7 +280,7 @@ impl Default for CommandNode { } } -impl Hash for CommandNode { +impl Hash for CommandNode { fn hash(&self, state: &mut H) { // hash the children for (k, v) in &self.children { @@ -289,7 +292,7 @@ impl Hash for CommandNode { } } -impl PartialEq for CommandNode { +impl PartialEq for CommandNode { fn eq(&self, other: &Self) -> bool { if self.children.len() != other.children.len() { return false; @@ -324,4 +327,4 @@ impl PartialEq for CommandNode { true } } -impl Eq for CommandNode {} +impl Eq for CommandNode {} -- cgit v1.2.3