From eb111be1f107696939b994f5de6e060cf972a732 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 13 Jan 2022 00:43:09 +0000 Subject: [PATCH] a --- azalea-brigadier/src/ambiguity_consumer.rs | 1 + .../src/arguments/argument_type.rs | 26 ++------ .../src/arguments/bool_argument_type.rs | 18 +++--- .../src/arguments/double_argument_type.rs | 1 + .../src/arguments/float_argument_type.rs | 1 + .../src/arguments/integer_argument_type.rs | 1 + .../src/arguments/long_argument_type.rs | 1 + .../src/arguments/string_argument_type.rs | 1 + .../src/builder/argument_builder.rs | 59 +++++++++--------- .../src/builder/literal_argument_builder.rs | 17 ++--- .../src/builder/required_argument_builder.rs | 33 +++++----- azalea-brigadier/src/command.rs | 4 +- azalea-brigadier/src/command_dispatcher.rs | 19 ++---- .../src/context/command_context.rs | 24 +++---- .../src/context/command_context_builder.rs | 62 ++++++++----------- .../src/context/parsed_argument.rs | 3 +- .../src/context/parsed_command_node.rs | 12 ++-- .../src/context/suggestion_context.rs | 4 +- .../exceptions/builtin_exception_provider.rs | 1 + azalea-brigadier/src/literal_message.rs | 1 + azalea-brigadier/src/parse_results.rs | 1 + azalea-brigadier/src/redirect_modifier.rs | 4 +- azalea-brigadier/src/result_consumer.rs | 1 + .../src/single_redirect_modifier.rs | 4 +- .../src/suggestion/suggestion_provider.rs | 4 +- .../src/suggestion/suggestions.rs | 2 +- .../src/tree/argument_command_node.rs | 48 ++++++-------- azalea-brigadier/src/tree/command_node.rs | 44 +++++-------- .../src/tree/literal_command_node.rs | 26 +++----- .../src/tree/root_command_node.rs | 22 +++---- .../tests/command_dispatcher_test.rs | 1 + .../tests/command_dispatcher_usages_test.rs | 1 + .../tests/command_suggestions_test.rs | 1 + 33 files changed, 188 insertions(+), 260 deletions(-) diff --git a/azalea-brigadier/src/ambiguity_consumer.rs b/azalea-brigadier/src/ambiguity_consumer.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/ambiguity_consumer.rs +++ b/azalea-brigadier/src/ambiguity_consumer.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/arguments/argument_type.rs b/azalea-brigadier/src/arguments/argument_type.rs index 890cdea0..3d1b1168 100644 --- a/azalea-brigadier/src/arguments/argument_type.rs +++ b/azalea-brigadier/src/arguments/argument_type.rs @@ -7,19 +7,6 @@ use crate::{ }; use dyn_clonable::*; -#[clonable] -// This should be applied to an Enum -pub trait Types: Clone { - fn bool(value: bool) -> Self - where - Self: Sized; - - /// Get the less specific ArgumentType from this enum - fn inner(&self) -> Box> - where - Self: Sized; -} - /* #[derive(Types)] enum BrigadierTypes { @@ -45,10 +32,8 @@ impl Types for BrigadierTypes { */ #[clonable] -pub trait ArgumentType: Clone -where - T: Types, -{ +pub trait ArgumentType: Clone { + type Into; // T parse(StringReader reader) throws CommandSyntaxException; // default CompletableFuture listSuggestions(final CommandContext context, final SuggestionsBuilder builder) { @@ -59,17 +44,16 @@ where // return Collections.emptyList(); // } - fn parse(&self, reader: &mut StringReader) -> Result, CommandSyntaxException>; + fn parse(&self, reader: &mut StringReader) -> Result; fn list_suggestions( &self, - context: &CommandContext, + context: &CommandContext, builder: &mut SuggestionsBuilder, ) -> Result where Self: Sized, - S: Sized, - T: Sized; + S: Sized; fn get_examples(&self) -> Vec; } diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs index b04488c1..c06f40c1 100644 --- a/azalea-brigadier/src/arguments/bool_argument_type.rs +++ b/azalea-brigadier/src/arguments/bool_argument_type.rs @@ -5,27 +5,25 @@ use crate::{ suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, }; -use super::argument_type::{ArgumentType, Types}; +use super::argument_type::ArgumentType; #[derive(Clone)] pub struct BoolArgumentType {} -impl ArgumentType for BoolArgumentType -where - T: Types, -{ - fn parse(&self, reader: &mut StringReader) -> Result { - Ok(T::bool(reader.read_boolean()?)) +impl ArgumentType for BoolArgumentType { + type Into = bool; + + fn parse(&self, reader: &mut StringReader) -> Result { + Ok(reader.read_boolean()?) } fn list_suggestions( &self, - context: &CommandContext, + context: &CommandContext, builder: &mut SuggestionsBuilder, ) -> Result where S: Sized, - T: Sized, { // if ("true".startsWith(builder.getRemainingLowerCase())) { // builder.suggest("true"); @@ -55,7 +53,7 @@ impl BoolArgumentType { Self {} } - fn get_bool(context: CommandContext, name: String) { + fn get_bool(context: CommandContext, name: String) { context.get_argument::(name) } } diff --git a/azalea-brigadier/src/arguments/double_argument_type.rs b/azalea-brigadier/src/arguments/double_argument_type.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/arguments/double_argument_type.rs +++ b/azalea-brigadier/src/arguments/double_argument_type.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/arguments/float_argument_type.rs b/azalea-brigadier/src/arguments/float_argument_type.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/arguments/float_argument_type.rs +++ b/azalea-brigadier/src/arguments/float_argument_type.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/arguments/integer_argument_type.rs b/azalea-brigadier/src/arguments/integer_argument_type.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/arguments/integer_argument_type.rs +++ b/azalea-brigadier/src/arguments/integer_argument_type.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/arguments/long_argument_type.rs b/azalea-brigadier/src/arguments/long_argument_type.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/arguments/long_argument_type.rs +++ b/azalea-brigadier/src/arguments/long_argument_type.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/arguments/string_argument_type.rs b/azalea-brigadier/src/arguments/string_argument_type.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/arguments/string_argument_type.rs +++ b/azalea-brigadier/src/arguments/string_argument_type.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index 0360b05a..6355456a 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -1,33 +1,32 @@ use crate::{ - arguments::argument_type::{ArgumentType, Types}, + arguments::argument_type::ArgumentType, command::Command, redirect_modifier::RedirectModifier, single_redirect_modifier::SingleRedirectModifier, tree::{command_node::CommandNode, root_command_node::RootCommandNode}, }; -pub struct BaseArgumentBuilder<'a, S, T> +pub struct BaseArgumentBuilder<'a, S> where S: Sized, - T: Sized + ArgumentType, { - arguments: RootCommandNode<'a, S, T>, - command: Option<&'a dyn Command>, + arguments: RootCommandNode<'a, S>, + command: Option<&'a dyn Command>, requirement: &'a dyn Fn(&S) -> bool, - target: Option<&'a dyn CommandNode>, - modifier: Option<&'a dyn RedirectModifier>, + target: Option<&'a dyn CommandNode>, + modifier: Option<&'a dyn RedirectModifier>, forks: bool, } -pub trait ArgumentBuilder { - fn build(self) -> dyn CommandNode; +pub trait ArgumentBuilder +where + T: ArgumentBuilder, +{ + fn build(self) -> dyn CommandNode; } -impl BaseArgumentBuilder<'_, S, T> -where - T: ArgumentType, -{ - pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut T, String> { +impl BaseArgumentBuilder<'_, S> { + pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut Self, String> { if self.target.is_some() { return Err("Cannot add children to a redirected node".to_string()); } @@ -35,20 +34,20 @@ where Ok(self) } - pub fn arguments(&self) -> &Vec<&dyn CommandNode> { + pub fn arguments(&self) -> &Vec<&dyn CommandNode> { &self.arguments.get_children() } - pub fn executes(&mut self, command: dyn Command) -> &mut T { + pub fn executes(&mut self, command: dyn Command) -> &mut Self { self.command = command; self } - pub fn command(&self) -> dyn Command { + pub fn command(&self) -> dyn Command { self.command } - pub fn requires(&mut self, requirement: &dyn Fn(&S) -> bool) -> &mut T { + pub fn requires(&mut self, requirement: &dyn Fn(&S) -> bool) -> &mut Self { self.requirement = requirement; self } @@ -57,33 +56,33 @@ where self.requirement } - pub fn redirect(&mut self, target: &dyn CommandNode) -> &mut T { + pub fn redirect(&mut self, target: &dyn CommandNode) -> &mut Self { self.forward(target, None, false) } pub fn redirect_modifier( &mut self, - target: &dyn CommandNode, - modifier: &dyn SingleRedirectModifier, - ) -> &mut T { + target: &dyn CommandNode, + modifier: &dyn SingleRedirectModifier, + ) -> &mut Self { // forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false); self.forward(target, modifier.map(|m| |o| vec![m.apply(o)]), false) } pub fn fork( &mut self, - target: &dyn CommandNode, - modifier: &dyn RedirectModifier, - ) -> &mut T { + target: &dyn CommandNode, + modifier: &dyn RedirectModifier, + ) -> &mut Self { self.forward(target, Some(modifier), true) } pub fn forward( &mut self, - target: &dyn CommandNode, - modifier: Option<&dyn RedirectModifier>, + target: &dyn CommandNode, + modifier: Option<&dyn RedirectModifier>, fork: bool, - ) -> Result<&mut T, String> { + ) -> Result<&mut Self, String> { if !self.arguments.get_children().is_empty() { return Err("Cannot forward a node with children".to_string()); } @@ -93,11 +92,11 @@ where Ok(self) } - pub fn get_redirect(&self) -> Option<&dyn CommandNode> { + pub fn get_redirect(&self) -> Option<&dyn CommandNode> { self.target.as_ref() } - pub fn get_redirect_modifier(&self) -> Option<&dyn RedirectModifier> { + pub fn get_redirect_modifier(&self) -> Option<&dyn RedirectModifier> { self.modifier.as_ref() } diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs index a4cb3f84..8039dff0 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -1,23 +1,16 @@ use crate::{ - arguments::argument_type::{ArgumentType, Types}, - tree::literal_command_node::LiteralCommandNode, + arguments::argument_type::ArgumentType, tree::literal_command_node::LiteralCommandNode, }; use super::argument_builder::BaseArgumentBuilder; -pub struct LiteralArgumentBuilder<'a, S, T> -where - T: ArgumentType, -{ +pub struct LiteralArgumentBuilder<'a, S> { literal: String, - pub base: BaseArgumentBuilder<'a, S, T>, + pub base: BaseArgumentBuilder<'a, S>, } -impl<'a, S, T> LiteralArgumentBuilder<'a, S, T> -where - T: ArgumentType, -{ +impl<'a, S> LiteralArgumentBuilder<'a, S> { pub fn new(literal: String) -> Self { Self { literal, @@ -29,7 +22,7 @@ where Self::new(name) } - pub fn build(self) -> LiteralCommandNode<'a, S, T> { + pub fn build(self) -> LiteralCommandNode<'a, S> { let result = LiteralCommandNode::new(self.literal, self.base); for argument in self.base.arguments { diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs index b5f99828..29af7f6f 100644 --- a/azalea-brigadier/src/builder/required_argument_builder.rs +++ b/azalea-brigadier/src/builder/required_argument_builder.rs @@ -1,30 +1,25 @@ use crate::{ - arguments::argument_type::{ArgumentType, Types}, + arguments::argument_type::ArgumentType, suggestion::suggestion_provider::SuggestionProvider, tree::{argument_command_node::ArgumentCommandNode, command_node::BaseCommandNode}, }; +use std::any::Any; use super::argument_builder::BaseArgumentBuilder; -pub struct RequiredArgumentBuilder<'a, S, T> -where - T: ArgumentType, -{ +pub struct RequiredArgumentBuilder<'a, S> { // private final String name; // private final ArgumentType type; // private SuggestionProvider suggestionsProvider = null; name: String, - type_: &'a T, - suggestions_provider: Option<&'a dyn SuggestionProvider>, + type_: Box>, + suggestions_provider: Option<&'a dyn SuggestionProvider>, - pub base: BaseArgumentBuilder<'a, S, T>, + pub base: BaseArgumentBuilder<'a, S>, } -impl<'a, S, T> RequiredArgumentBuilder<'a, S, T> -where - T: ArgumentType, -{ - pub fn new(name: String, type_: T) -> Self { +impl<'a, S> RequiredArgumentBuilder<'a, S> { + pub fn new(name: String, type_: dyn ArgumentType) -> Self { Self { name, type_: &type_, @@ -33,20 +28,20 @@ where } } - pub fn argument(name: String, type_: T) -> Self { + pub fn argument(name: String, type_: dyn ArgumentType) -> Self { Self::new(name, type_) } - pub fn suggests(mut self, provider: &dyn SuggestionProvider) -> Self { + pub fn suggests(mut self, provider: &dyn SuggestionProvider) -> Self { self.suggestions_provider = Some(provider); self } - pub fn suggestions_provider(&self) -> Option<&dyn SuggestionProvider> { + pub fn suggestions_provider(&self) -> Option<&dyn SuggestionProvider> { self.suggestions_provider.as_ref() } - pub fn get_type(&self) -> &T { + pub fn get_type(&self) -> &dyn ArgumentType { self.type_ } @@ -54,14 +49,14 @@ where self.name } - // final ArgumentCommandNode result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider()); + // final ArgumentCommandNode result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider()); // for (final CommandNode argument : getArguments()) { // result.addChild(argument); // } // return result; - pub fn build(self) -> ArgumentCommandNode<'a, S, T> { + pub fn build(self) -> ArgumentCommandNode<'a, S> { let result = ArgumentCommandNode { name: self.name, type_: &self.type_, diff --git a/azalea-brigadier/src/command.rs b/azalea-brigadier/src/command.rs index dcbf3ffd..a529f23c 100644 --- a/azalea-brigadier/src/command.rs +++ b/azalea-brigadier/src/command.rs @@ -7,6 +7,6 @@ use dyn_clonable::*; pub const SINGLE_SUCCESS: i32 = 1; #[clonable] -pub trait Command: Clone { - fn run(&self, context: &mut CommandContext) -> Result; +pub trait Command: Clone { + fn run(&self, context: &mut CommandContext) -> Result; } diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index f2fc7528..72a353ad 100644 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -1,22 +1,13 @@ -use crate::{ - arguments::argument_type::{ArgumentType, Types}, - tree::root_command_node::RootCommandNode, -}; +use crate::{arguments::argument_type::ArgumentType, tree::root_command_node::RootCommandNode}; /// The core command dispatcher, for registering, parsing, and executing commands. /// The `S` generic is a custom "source" type, such as a user or originator of a command #[derive(Default, Clone)] -pub struct CommandDispatcher<'a, S, T> -where - T: ArgumentType, -{ - root: RootCommandNode<'a, S, T>, +pub struct CommandDispatcher<'a, S> { + root: RootCommandNode<'a, S>, } -impl CommandDispatcher<'_, S, T> -where - T: ArgumentType, -{ +impl CommandDispatcher<'_, S> { /// The string required to separate individual arguments in an input string /// /// See: [`ARGUMENT_SEPARATOR_CHAR`] @@ -48,7 +39,7 @@ where /// * `root` - the existing [`RootCommandNode`] to use as the basis for this tree /// # Returns /// A new [`CommandDispatcher`] with the specified root node. - fn new(root: RootCommandNode) -> Self { + fn new(root: RootCommandNode) -> Self { Self { root } } } diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs index 68144a40..4f0b4d49 100644 --- a/azalea-brigadier/src/context/command_context.rs +++ b/azalea-brigadier/src/context/command_context.rs @@ -6,22 +6,22 @@ use crate::{ arguments::argument_type::ArgumentType, command::Command, redirect_modifier::RedirectModifier, tree::command_node::CommandNode, }; -use std::collections::HashMap; +use std::{any::Any, collections::HashMap}; -pub struct CommandContext<'a, S, T> { +pub struct CommandContext<'a, S> { source: S, input: String, - command: &'a dyn Command, - arguments: HashMap>, - root_node: &'a dyn CommandNode, - nodes: Vec>, + command: &'a dyn Command, + arguments: HashMap>>, + root_node: &'a dyn CommandNode, + nodes: Vec>, range: StringRange, - child: Option<&'a CommandContext<'a, S, T>>, - modifier: Option<&'a dyn RedirectModifier>, + child: Option<&'a CommandContext<'a, S>>, + modifier: Option<&'a dyn RedirectModifier>, forks: bool, } -impl CommandContext<'_, S, T> +impl CommandContext<'_, S> where S: PartialEq, { @@ -43,11 +43,11 @@ where } } - fn child(&self) -> &Option> { + fn child(&self) -> &Option> { &self.child } - fn last_child(&self) -> &CommandContext { + fn last_child(&self) -> &CommandContext { let mut result = self; while result.child.is_some() { result = result.child.as_ref().unwrap(); @@ -55,7 +55,7 @@ where result } - fn command(&self) -> &dyn Command { + fn command(&self) -> &dyn Command { &self.command } diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs index 639a97ee..95da4064 100644 --- a/azalea-brigadier/src/context/command_context_builder.rs +++ b/azalea-brigadier/src/context/command_context_builder.rs @@ -1,10 +1,8 @@ -use std::collections::HashMap; +use std::{any::Any, collections::HashMap}; use crate::{ - arguments::argument_type::{ArgumentType, Types}, - command::Command, - command_dispatcher::CommandDispatcher, - redirect_modifier::RedirectModifier, + arguments::argument_type::ArgumentType, command::Command, + command_dispatcher::CommandDispatcher, redirect_modifier::RedirectModifier, tree::command_node::CommandNode, }; @@ -27,19 +25,16 @@ use super::{ // private boolean forks; #[derive(Clone)] -pub struct CommandContextBuilder<'a, S, T> -where - T: ArgumentType, -{ - arguments: HashMap>, - root_node: &'a dyn CommandNode, - nodes: Vec>, - dispatcher: CommandDispatcher<'a, S, T>, +pub struct CommandContextBuilder<'a, S> { + arguments: HashMap>>, + root_node: &'a dyn CommandNode, + nodes: Vec>, + dispatcher: CommandDispatcher<'a, S>, source: S, - command: Box>, - child: Option>, + command: Box>, + child: Option>, range: StringRange, - modifier: Option>>, + modifier: Option>>, forks: bool, } @@ -50,14 +45,11 @@ where // this.range = StringRange.at(start); // } -impl CommandContextBuilder<'_, S, T> -where - T: ArgumentType, -{ +impl CommandContextBuilder<'_, S> { pub fn new( - dispatcher: CommandDispatcher, + dispatcher: CommandDispatcher, source: S, - root_node: dyn CommandNode, + root_node: dyn CommandNode, start: usize, ) -> Self { Self { @@ -78,25 +70,25 @@ where &self.source } - pub fn root_node(&self) -> &dyn CommandNode { + pub fn root_node(&self) -> &dyn CommandNode { &self.root_node } - pub fn with_argument(mut self, name: String, argument: ParsedArgument) -> Self { + pub fn with_argument(mut self, name: String, argument: ParsedArgument>) -> Self { self.arguments.insert(name, argument); self } - pub fn arguments(&self) -> &HashMap> { + pub fn arguments(&self) -> &HashMap>> { &self.arguments } - pub fn with_command(mut self, command: &dyn Command) -> Self { + pub fn with_command(mut self, command: &dyn Command) -> Self { self.command = command; self } - pub fn with_node(mut self, node: dyn CommandNode, range: StringRange) -> Self { + pub fn with_node(mut self, node: dyn CommandNode, range: StringRange) -> Self { self.nodes.push(ParsedCommandNode::new(node, range)); self.range = StringRange::encompassing(&self.range, &range); self.modifier = node.redirect_modifier(); @@ -104,16 +96,16 @@ where self } - pub fn with_child(mut self, child: CommandContextBuilder) -> Self { + pub fn with_child(mut self, child: CommandContextBuilder) -> Self { self.child = Some(child); self } - pub fn child(&self) -> Option<&CommandContextBuilder> { + pub fn child(&self) -> Option<&CommandContextBuilder> { self.child.as_ref() } - pub fn last_child(&self) -> Option<&CommandContextBuilder> { + pub fn last_child(&self) -> Option<&CommandContextBuilder> { let mut result = self; while let Some(child) = result.child() { result = child; @@ -121,15 +113,15 @@ where Some(result) } - pub fn command(&self) -> &dyn Command { + pub fn command(&self) -> &dyn Command { &*self.command } - pub fn nodes(&self) -> &Vec> { + pub fn nodes(&self) -> &Vec> { &self.nodes } - pub fn build(self, input: &str) -> CommandContext { + pub fn build(self, input: &str) -> CommandContext { CommandContext { source: self.source, input, @@ -144,7 +136,7 @@ where } } - pub fn dispatcher(&self) -> &CommandDispatcher { + pub fn dispatcher(&self) -> &CommandDispatcher { &self.dispatcher } @@ -152,7 +144,7 @@ where &self.range } - pub fn find_suggestion_context(&self, cursor: i32) -> Result, String> { + pub fn find_suggestion_context(&self, cursor: i32) -> Result, String> { if self.range.start() <= cursor { if self.range.end() < cursor { if let Some(child) = self.child() { diff --git a/azalea-brigadier/src/context/parsed_argument.rs b/azalea-brigadier/src/context/parsed_argument.rs index 447a1223..e0bdf97b 100644 --- a/azalea-brigadier/src/context/parsed_argument.rs +++ b/azalea-brigadier/src/context/parsed_argument.rs @@ -3,12 +3,11 @@ use super::string_range::StringRange; #[derive(PartialEq, Eq, Hash, Clone)] pub struct ParsedArgument { range: StringRange, - // T is an item in an enum result: T, } impl ParsedArgument { - fn new(start: usize, end: usize, result: T) -> Self { + fn new(start: usize, end: usize, result: &T) -> Self { Self { range: StringRange::between(start, end), result, diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs index c0be355c..16c6ed8b 100644 --- a/azalea-brigadier/src/context/parsed_command_node.rs +++ b/azalea-brigadier/src/context/parsed_command_node.rs @@ -1,17 +1,17 @@ use super::string_range::StringRange; use crate::tree::command_node::CommandNode; -pub struct ParsedCommandNode { - node: Box>, +pub struct ParsedCommandNode { + node: Box>, range: StringRange, } -impl ParsedCommandNode { - fn new(node: dyn CommandNode, range: StringRange) -> Self { +impl ParsedCommandNode { + fn new(node: dyn CommandNode, range: StringRange) -> Self { Self { node, range } } - fn node(&self) -> &dyn CommandNode { + fn node(&self) -> &dyn CommandNode { &self.node } @@ -20,7 +20,7 @@ impl ParsedCommandNode { } } -impl Clone for ParsedCommandNode { +impl Clone for ParsedCommandNode { fn clone_from(&mut self, source: &Self) { Self { node: self.node.clone(), diff --git a/azalea-brigadier/src/context/suggestion_context.rs b/azalea-brigadier/src/context/suggestion_context.rs index 42bc550e..252cb6ed 100644 --- a/azalea-brigadier/src/context/suggestion_context.rs +++ b/azalea-brigadier/src/context/suggestion_context.rs @@ -1,6 +1,6 @@ use crate::tree::command_node::CommandNode; -pub struct SuggestionContext<'a, S, T> { - parent: &'a dyn CommandNode, +pub struct SuggestionContext<'a, S> { + parent: &'a dyn CommandNode, start_pos: usize, } diff --git a/azalea-brigadier/src/exceptions/builtin_exception_provider.rs b/azalea-brigadier/src/exceptions/builtin_exception_provider.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/exceptions/builtin_exception_provider.rs +++ b/azalea-brigadier/src/exceptions/builtin_exception_provider.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/literal_message.rs b/azalea-brigadier/src/literal_message.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/literal_message.rs +++ b/azalea-brigadier/src/literal_message.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/parse_results.rs b/azalea-brigadier/src/parse_results.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/parse_results.rs +++ b/azalea-brigadier/src/parse_results.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/redirect_modifier.rs b/azalea-brigadier/src/redirect_modifier.rs index fdb0a080..7a6d4db5 100644 --- a/azalea-brigadier/src/redirect_modifier.rs +++ b/azalea-brigadier/src/redirect_modifier.rs @@ -6,6 +6,6 @@ use crate::{ }; #[clonable] -pub trait RedirectModifier: Clone { - fn apply(&self, context: CommandContext) -> Result, CommandSyntaxException>; +pub trait RedirectModifier: Clone { + fn apply(&self, context: CommandContext) -> Result, CommandSyntaxException>; } diff --git a/azalea-brigadier/src/result_consumer.rs b/azalea-brigadier/src/result_consumer.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/src/result_consumer.rs +++ b/azalea-brigadier/src/result_consumer.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/src/single_redirect_modifier.rs b/azalea-brigadier/src/single_redirect_modifier.rs index 95055fd8..dd63244d 100644 --- a/azalea-brigadier/src/single_redirect_modifier.rs +++ b/azalea-brigadier/src/single_redirect_modifier.rs @@ -3,6 +3,6 @@ use crate::{ exceptions::command_syntax_exception::CommandSyntaxException, }; -pub trait SingleRedirectModifier { - fn apply(&self, context: CommandContext) -> Result; +pub trait SingleRedirectModifier { + fn apply(&self, context: CommandContext) -> Result; } diff --git a/azalea-brigadier/src/suggestion/suggestion_provider.rs b/azalea-brigadier/src/suggestion/suggestion_provider.rs index 9720d3b9..3027d460 100644 --- a/azalea-brigadier/src/suggestion/suggestion_provider.rs +++ b/azalea-brigadier/src/suggestion/suggestion_provider.rs @@ -5,10 +5,10 @@ use crate::{ use super::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}; -pub trait SuggestionProvider { +pub trait SuggestionProvider { fn suggestions( &self, - context: &CommandContext, + context: &CommandContext, builder: &SuggestionsBuilder, ) -> Result; } diff --git a/azalea-brigadier/src/suggestion/suggestions.rs b/azalea-brigadier/src/suggestion/suggestions.rs index 778c5de8..9f0ee06d 100644 --- a/azalea-brigadier/src/suggestion/suggestions.rs +++ b/azalea-brigadier/src/suggestion/suggestions.rs @@ -4,7 +4,7 @@ use crate::{context::string_range::StringRange, message::Message}; use super::suggestion::Suggestion; -#[derive(PartialEq, Eq, Hash, Default)] +#[derive(PartialEq, Eq, Hash)] pub struct Suggestions { range: StringRange, suggestions: Vec, diff --git a/azalea-brigadier/src/tree/argument_command_node.rs b/azalea-brigadier/src/tree/argument_command_node.rs index 3fc1bb50..fb9a75fa 100644 --- a/azalea-brigadier/src/tree/argument_command_node.rs +++ b/azalea-brigadier/src/tree/argument_command_node.rs @@ -1,7 +1,10 @@ -use std::fmt::{Display, Formatter}; +use std::{ + any::Any, + fmt::{Display, Formatter}, +}; use crate::{ - arguments::argument_type::{ArgumentType, Types}, + arguments::argument_type::ArgumentType, builder::required_argument_builder::RequiredArgumentBuilder, context::{ command_context::CommandContext, command_context_builder::CommandContextBuilder, @@ -22,35 +25,27 @@ const USAGE_ARGUMENT_OPEN: &str = "<"; const USAGE_ARGUMENT_CLOSE: &str = ">"; #[derive(Clone)] -pub struct ArgumentCommandNode<'a, S, T> -where - // each argument command node has its own different type - T: ArgumentType, -{ +pub struct ArgumentCommandNode<'a, S> { name: String, - type_: &'a T, - custom_suggestions: Option<&'a dyn SuggestionProvider>, - // custom_suggestions: &'a dyn SuggestionProvider, + type_: Box>, + custom_suggestions: Option<&'a dyn SuggestionProvider>, + // custom_suggestions: &'a dyn SuggestionProvider, // Since Rust doesn't have extending, we put the struct this is extending as the "base" field - pub base: BaseCommandNode<'a, S, T>, + pub base: BaseCommandNode<'a, S>, } -impl ArgumentCommandNode<'_, S, T> -where - T: ArgumentType, -{ - fn get_type(&self) -> &T { - &self.type_ +impl ArgumentCommandNode<'_, S> { + fn get_type(&self) -> &dyn ArgumentType { + self.type_ } - fn custom_suggestions(&self) -> Option<&dyn SuggestionProvider> { + fn custom_suggestions(&self) -> Option<&dyn SuggestionProvider> { self.custom_suggestions } } -impl<'a, S, T> CommandNode for ArgumentCommandNode<'a, S, T> +impl<'a, S> CommandNode for ArgumentCommandNode<'a, S> where - T: ArgumentType + Clone, S: Clone, { fn name(&self) -> &str { @@ -60,11 +55,11 @@ where fn parse( &self, reader: &mut StringReader, - context_builder: CommandContextBuilder, + context_builder: CommandContextBuilder, ) -> Result<(), CommandSyntaxException> { // final int start = reader.getCursor(); // final T result = type.parse(reader); - // final ParsedArgument parsed = new ParsedArgument<>(start, reader.getCursor(), result); + // final ParsedArgument parsed = new ParsedArgument<>(start, reader.getCursor(), result); // contextBuilder.withArgument(name, parsed); // contextBuilder.withNode(this, parsed.getRange()); @@ -81,7 +76,7 @@ where fn list_suggestions( &self, - context: CommandContext, + context: CommandContext, builder: &mut SuggestionsBuilder, ) -> Result { if self.custom_suggestions.is_none() { @@ -105,7 +100,7 @@ where USAGE_ARGUMENT_OPEN + self.name + USAGE_ARGUMENT_CLOSE } - fn create_builder(&self) -> RequiredArgumentBuilder { + fn create_builder(&self) -> RequiredArgumentBuilder { let builder = RequiredArgumentBuilder::argument(&self.name, &self.type_); builder.requires(self.base.get_requirement()); builder.forward( @@ -125,10 +120,7 @@ where } } -impl Display for ArgumentCommandNode<'_, S, T> -where - T: ArgumentType, -{ +impl Display for ArgumentCommandNode<'_, S> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "", self.name, self.type_) } diff --git a/azalea-brigadier/src/tree/command_node.rs b/azalea-brigadier/src/tree/command_node.rs index f3be1597..b8f416eb 100644 --- a/azalea-brigadier/src/tree/command_node.rs +++ b/azalea-brigadier/src/tree/command_node.rs @@ -1,6 +1,6 @@ use super::{argument_command_node::ArgumentCommandNode, literal_command_node::LiteralCommandNode}; use crate::{ - arguments::argument_type::{ArgumentType, Types}, + arguments::argument_type::ArgumentType, builder::argument_builder::ArgumentBuilder, command::Command, context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, @@ -10,29 +10,23 @@ use crate::{ suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, }; use dyn_clonable::*; -use std::{collections::HashMap, fmt::Debug}; +use std::{any::Any, collections::HashMap, fmt::Debug}; #[derive(Default)] -pub struct BaseCommandNode<'a, S, T> -where - T: ArgumentType, -{ - children: HashMap>, - literals: HashMap>, - arguments: HashMap>, +pub struct BaseCommandNode<'a, S> { + children: HashMap>, + literals: HashMap>, + arguments: HashMap>, requirement: Option<&'a dyn Fn(&S) -> bool>, - redirect: Option<&'a dyn CommandNode>, - modifier: Option<&'a dyn RedirectModifier>, + redirect: Option<&'a dyn CommandNode>, + modifier: Option<&'a dyn RedirectModifier>, forks: bool, - command: Option<&'a dyn Command>, + command: Option<&'a dyn Command>, } -impl BaseCommandNode<'_, S, T> where T: ArgumentType {} +impl BaseCommandNode<'_, S> {} -impl Clone for BaseCommandNode<'_, S, T> -where - T: ArgumentType, -{ +impl Clone for BaseCommandNode<'_, S> { fn clone(&self) -> Self { Self { children: self.children.clone(), @@ -47,10 +41,7 @@ where } } -impl Debug for BaseCommandNode<'_, S, T> -where - T: ArgumentType, -{ +impl Debug for BaseCommandNode<'_, S> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("BaseCommandNode") .field("children", &self.children) @@ -66,23 +57,20 @@ where } #[clonable] -pub trait CommandNode: Clone -where - T: ArgumentType, -{ +pub trait CommandNode: Clone { fn name(&self) -> &str; fn usage_text(&self) -> &str; fn parse( &self, reader: &mut StringReader, - context_builder: CommandContextBuilder, + context_builder: CommandContextBuilder, ) -> Result<(), CommandSyntaxException>; fn list_suggestions( &self, - context: CommandContext, + context: CommandContext, builder: SuggestionsBuilder, ) -> Result; fn is_valid_input(&self, input: &str) -> bool; - fn create_builder(&self) -> dyn ArgumentBuilder; + fn create_builder(&self) -> dyn ArgumentBuilder; fn get_examples(&self) -> Vec; } diff --git a/azalea-brigadier/src/tree/literal_command_node.rs b/azalea-brigadier/src/tree/literal_command_node.rs index 021a3ea6..a722121f 100644 --- a/azalea-brigadier/src/tree/literal_command_node.rs +++ b/azalea-brigadier/src/tree/literal_command_node.rs @@ -1,5 +1,5 @@ use crate::{ - arguments::argument_type::{ArgumentType, Types}, + arguments::argument_type::ArgumentType, builder::literal_argument_builder::LiteralArgumentBuilder, command::Command, context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, @@ -14,22 +14,15 @@ use crate::{ use super::command_node::{BaseCommandNode, CommandNode}; #[derive(Debug, Clone)] -pub struct LiteralCommandNode<'a, S, T> -where - // each argument command node has its own different type - T: ArgumentType, -{ +pub struct LiteralCommandNode<'a, S> { literal: String, literal_lowercase: String, // Since Rust doesn't have extending, we put the struct this is extending as the "base" field - pub base: BaseCommandNode<'a, S, T>, + pub base: BaseCommandNode<'a, S>, } -impl<'a, S, T> LiteralCommandNode<'a, S, T> -where - T: ArgumentType, -{ - pub fn new(literal: String, base: BaseCommandNode) -> Self { +impl<'a, S> LiteralCommandNode<'a, S> { + pub fn new(literal: String, base: BaseCommandNode) -> Self { let literal_lowercase = literal.to_lowercase(); Self { literal, @@ -59,9 +52,8 @@ where } } -impl CommandNode for LiteralCommandNode<'_, S, T> +impl CommandNode for LiteralCommandNode<'_, S> where - T: ArgumentType + Clone, S: Clone, { fn name(&self) -> &str { @@ -71,7 +63,7 @@ where fn parse( &self, reader: StringReader, - context_builder: CommandContextBuilder, + context_builder: CommandContextBuilder, ) -> Result<(), CommandSyntaxException> { let start = reader.get_cursor(); let end = self.parse(reader); @@ -87,7 +79,7 @@ where fn list_suggestions( &self, - context: CommandContext, + context: CommandContext, builder: SuggestionsBuilder, ) -> Result { if self @@ -108,7 +100,7 @@ where self.literal } - fn create_builder(&self) -> LiteralArgumentBuilder { + fn create_builder(&self) -> LiteralArgumentBuilder { let builder = LiteralArgumentBuilder::literal(self.literal()); builder.requires(self.requirement()); builder.forward(self.redirect(), self.redirect_modifier(), self.is_fork()); diff --git a/azalea-brigadier/src/tree/root_command_node.rs b/azalea-brigadier/src/tree/root_command_node.rs index ded5fa77..c3139a05 100644 --- a/azalea-brigadier/src/tree/root_command_node.rs +++ b/azalea-brigadier/src/tree/root_command_node.rs @@ -1,7 +1,7 @@ use std::fmt::{Display, Formatter}; use crate::{ - arguments::argument_type::{ArgumentType, Types}, + arguments::argument_type::ArgumentType, context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, exceptions::{ builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, @@ -13,18 +13,13 @@ use crate::{ use super::command_node::{BaseCommandNode, CommandNode}; #[derive(Clone, Default)] -pub struct RootCommandNode<'a, S, T> -where - // each argument command node has its own different type - T: ArgumentType, -{ +pub struct RootCommandNode<'a, S> { // Since Rust doesn't have extending, we put the struct this is extending as the "base" field - pub base: BaseCommandNode<'a, S, T>, + pub base: BaseCommandNode<'a, S>, } -impl CommandNode for RootCommandNode<'_, S, T> +impl CommandNode for RootCommandNode<'_, S> where - T: ArgumentType + Clone, S: Clone, { fn name(&self) -> &str { @@ -34,13 +29,13 @@ where fn parse( &self, reader: StringReader, - context_builder: CommandContextBuilder, + context_builder: CommandContextBuilder, ) -> Result<(), CommandSyntaxException> { } fn list_suggestions( &self, - context: CommandContext, + context: CommandContext, builder: SuggestionsBuilder, ) -> Result { Suggestions::empty() @@ -63,10 +58,7 @@ where } } -impl Display for RootCommandNode<'_, S, T> -where - T: ArgumentType, -{ +impl Display for RootCommandNode<'_, S> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "") } diff --git a/azalea-brigadier/tests/command_dispatcher_test.rs b/azalea-brigadier/tests/command_dispatcher_test.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/tests/command_dispatcher_test.rs +++ b/azalea-brigadier/tests/command_dispatcher_test.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/tests/command_dispatcher_usages_test.rs b/azalea-brigadier/tests/command_dispatcher_usages_test.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/tests/command_dispatcher_usages_test.rs +++ b/azalea-brigadier/tests/command_dispatcher_usages_test.rs @@ -0,0 +1 @@ + diff --git a/azalea-brigadier/tests/command_suggestions_test.rs b/azalea-brigadier/tests/command_suggestions_test.rs index e69de29b..8b137891 100644 --- a/azalea-brigadier/tests/command_suggestions_test.rs +++ b/azalea-brigadier/tests/command_suggestions_test.rs @@ -0,0 +1 @@ +