diff options
| author | Ubuntu <github@matdoes.dev> | 2022-01-13 00:43:09 +0000 |
|---|---|---|
| committer | Ubuntu <github@matdoes.dev> | 2022-01-13 00:43:09 +0000 |
| commit | eb111be1f107696939b994f5de6e060cf972a732 (patch) | |
| tree | 055deab4179088c5e91a179bfe465a7859c45ab6 /azalea-brigadier/src/tree | |
| parent | 270507736af57aae6801dc9eb3c3132139d0d07b (diff) | |
| download | azalea-drasl-eb111be1f107696939b994f5de6e060cf972a732.tar.xz | |
a
Diffstat (limited to 'azalea-brigadier/src/tree')
| -rw-r--r-- | azalea-brigadier/src/tree/argument_command_node.rs | 48 | ||||
| -rw-r--r-- | azalea-brigadier/src/tree/command_node.rs | 44 | ||||
| -rw-r--r-- | azalea-brigadier/src/tree/literal_command_node.rs | 26 | ||||
| -rw-r--r-- | azalea-brigadier/src/tree/root_command_node.rs | 22 |
4 files changed, 52 insertions, 88 deletions
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<dyn Types>, -{ +pub struct ArgumentCommandNode<'a, S> { name: String, - type_: &'a T, - custom_suggestions: Option<&'a dyn SuggestionProvider<S, T>>, - // custom_suggestions: &'a dyn SuggestionProvider<S, T>, + type_: Box<dyn ArgumentType<Into = dyn Any>>, + custom_suggestions: Option<&'a dyn SuggestionProvider<S>>, + // custom_suggestions: &'a dyn SuggestionProvider<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<S, T> ArgumentCommandNode<'_, S, T> -where - T: ArgumentType<dyn Types>, -{ - fn get_type(&self) -> &T { - &self.type_ +impl<S> ArgumentCommandNode<'_, S> { + fn get_type(&self) -> &dyn ArgumentType<Into = dyn Any> { + self.type_ } - fn custom_suggestions(&self) -> Option<&dyn SuggestionProvider<S, T>> { + fn custom_suggestions(&self) -> Option<&dyn SuggestionProvider<S>> { self.custom_suggestions } } -impl<'a, S, T> CommandNode<S, T> for ArgumentCommandNode<'a, S, T> +impl<'a, S> CommandNode<S> for ArgumentCommandNode<'a, S> where - T: ArgumentType<dyn Types> + Clone, S: Clone, { fn name(&self) -> &str { @@ -60,11 +55,11 @@ where fn parse( &self, reader: &mut StringReader, - context_builder: CommandContextBuilder<S, T>, + context_builder: CommandContextBuilder<S>, ) -> Result<(), CommandSyntaxException> { // final int start = reader.getCursor(); // final T result = type.parse(reader); - // final ParsedArgument<S, T> parsed = new ParsedArgument<>(start, reader.getCursor(), result); + // final ParsedArgument<S> 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<S, T>, + context: CommandContext<S>, builder: &mut SuggestionsBuilder, ) -> Result<Suggestions, CommandSyntaxException> { if self.custom_suggestions.is_none() { @@ -105,7 +100,7 @@ where USAGE_ARGUMENT_OPEN + self.name + USAGE_ARGUMENT_CLOSE } - fn create_builder(&self) -> RequiredArgumentBuilder<S, T> { + fn create_builder(&self) -> RequiredArgumentBuilder<S> { let builder = RequiredArgumentBuilder::argument(&self.name, &self.type_); builder.requires(self.base.get_requirement()); builder.forward( @@ -125,10 +120,7 @@ where } } -impl<S, T> Display for ArgumentCommandNode<'_, S, T> -where - T: ArgumentType<dyn Types>, -{ +impl<S> Display for ArgumentCommandNode<'_, S> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "<argument {}: {}>", 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<dyn Types>, -{ - children: HashMap<String, &'a dyn CommandNode<S, T>>, - literals: HashMap<String, LiteralCommandNode<'a, S, T>>, - arguments: HashMap<String, ArgumentCommandNode<'a, S, T>>, +pub struct BaseCommandNode<'a, S> { + children: HashMap<String, &'a dyn CommandNode<S>>, + literals: HashMap<String, LiteralCommandNode<'a, S>>, + arguments: HashMap<String, ArgumentCommandNode<'a, S>>, requirement: Option<&'a dyn Fn(&S) -> bool>, - redirect: Option<&'a dyn CommandNode<S, T>>, - modifier: Option<&'a dyn RedirectModifier<S, T>>, + redirect: Option<&'a dyn CommandNode<S>>, + modifier: Option<&'a dyn RedirectModifier<S>>, forks: bool, - command: Option<&'a dyn Command<S, T>>, + command: Option<&'a dyn Command<S>>, } -impl<S, T> BaseCommandNode<'_, S, T> where T: ArgumentType<dyn Types> {} +impl<S> BaseCommandNode<'_, S> {} -impl<S, T> Clone for BaseCommandNode<'_, S, T> -where - T: ArgumentType<dyn Types>, -{ +impl<S> Clone for BaseCommandNode<'_, S> { fn clone(&self) -> Self { Self { children: self.children.clone(), @@ -47,10 +41,7 @@ where } } -impl<S, T> Debug for BaseCommandNode<'_, S, T> -where - T: ArgumentType<dyn Types>, -{ +impl<S> 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<S, T>: Clone -where - T: ArgumentType<dyn Types>, -{ +pub trait CommandNode<S>: Clone { fn name(&self) -> &str; fn usage_text(&self) -> &str; fn parse( &self, reader: &mut StringReader, - context_builder: CommandContextBuilder<S, T>, + context_builder: CommandContextBuilder<S>, ) -> Result<(), CommandSyntaxException>; fn list_suggestions( &self, - context: CommandContext<S, T>, + context: CommandContext<S>, builder: SuggestionsBuilder, ) -> Result<Suggestions, CommandSyntaxException>; fn is_valid_input(&self, input: &str) -> bool; - fn create_builder(&self) -> dyn ArgumentBuilder<S, T>; + fn create_builder(&self) -> dyn ArgumentBuilder<S, dyn Any>; fn get_examples(&self) -> Vec<String>; } 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<dyn Types>, -{ +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<dyn Types>, -{ - pub fn new(literal: String, base: BaseCommandNode<S, T>) -> Self { +impl<'a, S> LiteralCommandNode<'a, S> { + pub fn new(literal: String, base: BaseCommandNode<S>) -> Self { let literal_lowercase = literal.to_lowercase(); Self { literal, @@ -59,9 +52,8 @@ where } } -impl<S, T> CommandNode<S, T> for LiteralCommandNode<'_, S, T> +impl<S> CommandNode<S> for LiteralCommandNode<'_, S> where - T: ArgumentType<dyn Types> + Clone, S: Clone, { fn name(&self) -> &str { @@ -71,7 +63,7 @@ where fn parse( &self, reader: StringReader, - context_builder: CommandContextBuilder<S, T>, + context_builder: CommandContextBuilder<S>, ) -> Result<(), CommandSyntaxException> { let start = reader.get_cursor(); let end = self.parse(reader); @@ -87,7 +79,7 @@ where fn list_suggestions( &self, - context: CommandContext<S, T>, + context: CommandContext<S>, builder: SuggestionsBuilder, ) -> Result<Suggestions, CommandSyntaxException> { if self @@ -108,7 +100,7 @@ where self.literal } - fn create_builder(&self) -> LiteralArgumentBuilder<S, T> { + fn create_builder(&self) -> LiteralArgumentBuilder<S> { 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<dyn Types>, -{ +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<S, T> CommandNode<S, T> for RootCommandNode<'_, S, T> +impl<S> CommandNode<S> for RootCommandNode<'_, S> where - T: ArgumentType<dyn Types> + Clone, S: Clone, { fn name(&self) -> &str { @@ -34,13 +29,13 @@ where fn parse( &self, reader: StringReader, - context_builder: CommandContextBuilder<S, T>, + context_builder: CommandContextBuilder<S>, ) -> Result<(), CommandSyntaxException> { } fn list_suggestions( &self, - context: CommandContext<S, T>, + context: CommandContext<S>, builder: SuggestionsBuilder, ) -> Result<Suggestions, CommandSyntaxException> { Suggestions::empty() @@ -63,10 +58,7 @@ where } } -impl<S, T> Display for RootCommandNode<'_, S, T> -where - T: ArgumentType<dyn Types>, -{ +impl<S> Display for RootCommandNode<'_, S> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "<root>") } |
