diff options
| author | mat <github@matdoes.dev> | 2022-02-01 00:12:46 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-02-01 00:12:46 -0600 |
| commit | d9e52f8d965473517ddf6f11f9ac3be9aa14e14d (patch) | |
| tree | d29b0e3184df0973ba07927a572814e4594f9e9e /azalea-brigadier/src/builder | |
| parent | 30a86e1de5d8bf302f05d091b0a7b4cc6721d911 (diff) | |
| download | azalea-drasl-d9e52f8d965473517ddf6f11f9ac3be9aa14e14d.tar.xz | |
b
Diffstat (limited to 'azalea-brigadier/src/builder')
| -rw-r--r-- | azalea-brigadier/src/builder/argument_builder.rs | 37 | ||||
| -rw-r--r-- | azalea-brigadier/src/builder/literal_argument_builder.rs | 29 | ||||
| -rw-r--r-- | azalea-brigadier/src/builder/required_argument_builder.rs | 35 |
3 files changed, 58 insertions, 43 deletions
diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index ec2756ca..d0770be2 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -4,40 +4,35 @@ use crate::{ redirect_modifier::RedirectModifier, single_redirect_modifier::SingleRedirectModifier, tree::{ - command_node::{BaseCommandNode, CommandNode}, + command_node::{BaseCommandNode, CommandNodeTrait}, root_command_node::RootCommandNode, }, }; +use std::fmt::Debug; -pub struct BaseArgumentBuilder<'a, S> -where - S: Sized, -{ +pub struct BaseArgumentBuilder<'a, S> { arguments: RootCommandNode<'a, S>, command: Option<Box<dyn Command<S>>>, requirement: Box<dyn Fn(&S) -> bool>, - target: Option<Box<dyn CommandNode<S>>>, + target: Option<Box<dyn CommandNodeTrait<S>>>, modifier: Option<Box<dyn RedirectModifier<S>>>, forks: bool, } -pub trait ArgumentBuilder<S, T> -where - T: ArgumentBuilder<S, T>, -{ - fn build(self) -> Box<dyn CommandNode<S>>; +pub trait ArgumentBuilder<S> { + fn build(self) -> Box<dyn CommandNodeTrait<S>>; } impl<'a, S> BaseArgumentBuilder<'a, S> { - pub fn then(&mut self, argument: Box<dyn CommandNode<S>>) -> Result<&mut Self, String> { + pub fn then(&mut self, argument: Box<dyn ArgumentBuilder<S>>) -> Result<&mut Self, String> { if self.target.is_some() { return Err("Cannot add children to a redirected node".to_string()); } - + self.arguments.add_child(argument.build()); Ok(self) } - pub fn arguments(&self) -> &Vec<&dyn CommandNode<S>> { + pub fn arguments(&self) -> &Vec<&dyn CommandNodeTrait<S>> { &self.arguments.get_children() } @@ -59,13 +54,13 @@ impl<'a, S> BaseArgumentBuilder<'a, S> { self.requirement } - pub fn redirect(&mut self, target: Box<dyn CommandNode<S>>) -> &mut Self { + pub fn redirect(&mut self, target: Box<dyn CommandNodeTrait<S>>) -> &mut Self { self.forward(target, None, false) } pub fn redirect_modifier( &mut self, - target: &dyn CommandNode<S>, + target: &dyn CommandNodeTrait<S>, modifier: &dyn SingleRedirectModifier<S>, ) -> &mut Self { // forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false); @@ -74,7 +69,7 @@ impl<'a, S> BaseArgumentBuilder<'a, S> { pub fn fork( &mut self, - target: &dyn CommandNode<S>, + target: &dyn CommandNodeTrait<S>, modifier: &dyn RedirectModifier<S>, ) -> &mut Self { self.forward(target, Some(modifier), true) @@ -82,20 +77,20 @@ impl<'a, S> BaseArgumentBuilder<'a, S> { pub fn forward( &mut self, - target: Box<dyn CommandNode<S>>, - modifier: Option<Box<dyn RedirectModifier<S>>>, + target: Option<Box<dyn CommandNodeTrait<S>>>, + modifier: Option<&dyn RedirectModifier<S>>, fork: bool, ) -> Result<&mut Self, String> { if !self.arguments.get_children().is_empty() { return Err("Cannot forward a node with children".to_string()); } - self.target = Some(target); + self.target = target; self.modifier = modifier; self.forks = fork; Ok(self) } - pub fn get_redirect(&self) -> Option<&dyn CommandNode<S>> { + pub fn get_redirect(&self) -> Option<&dyn CommandNodeTrait<S>> { self.target.as_ref() } diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs index 5bceb6eb..8250d45d 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -4,27 +4,38 @@ use crate::{ command::Command, redirect_modifier::RedirectModifier, tree::{ - command_node::CommandNode, literal_command_node::LiteralCommandNode, + command_node::CommandNodeTrait, literal_command_node::LiteralCommandNode, root_command_node::RootCommandNode, }, }; +use std::fmt::Debug; -pub struct LiteralArgumentBuilder<'a, S> { +pub struct LiteralArgumentBuilder<'a, S> +where + , +{ arguments: RootCommandNode<'a, S>, command: Option<Box<dyn Command<S>>>, requirement: Box<dyn Fn(&S) -> bool>, - target: Option<Box<dyn CommandNode<S>>>, + target: Option<Box<dyn CommandNodeTrait<S>>>, modifier: Option<Box<dyn RedirectModifier<S>>>, forks: bool, - literal: String, } -impl<'a, S> LiteralArgumentBuilder<'a, S> { +impl<'a, S> LiteralArgumentBuilder<'a, S> +where + , +{ pub fn new(literal: String) -> Self { Self { literal, - base: BaseArgumentBuilder::default(), + arguments: RootCommandNode::new(), + command: None, + requirement: Box::new(|_| true), + target: None, + modifier: None, + forks: false, } } @@ -33,11 +44,11 @@ impl<'a, S> LiteralArgumentBuilder<'a, S> { } } -impl<'a, S, T> ArgumentBuilder<S, T> for LiteralArgumentBuilder<'a, S> +impl<'a, S> ArgumentBuilder<S> for LiteralArgumentBuilder<'a, S> where - T: ArgumentBuilder<S, T>, + , { - fn build(self) -> Box<dyn CommandNode<S>> { + fn build(self) -> Box<dyn CommandNodeTrait<S>> { let result = LiteralCommandNode::new(self.literal, self.base.build()); 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 b577f3ed..fe6f2ecc 100644 --- a/azalea-brigadier/src/builder/required_argument_builder.rs +++ b/azalea-brigadier/src/builder/required_argument_builder.rs @@ -1,17 +1,23 @@ +use super::argument_builder::BaseArgumentBuilder; use crate::{ arguments::argument_type::ArgumentType, + command::Command, + redirect_modifier::RedirectModifier, suggestion::suggestion_provider::SuggestionProvider, - tree::{argument_command_node::ArgumentCommandNode, command_node::BaseCommandNode}, + tree::{ + argument_command_node::ArgumentCommandNode, + command_node::{BaseCommandNode, CommandNodeTrait}, + root_command_node::RootCommandNode, + }, }; use std::any::Any; - -use super::argument_builder::BaseArgumentBuilder; +use std::fmt::Debug; pub struct RequiredArgumentBuilder<'a, S> { arguments: RootCommandNode<'a, S>, command: Option<Box<dyn Command<S>>>, requirement: Box<dyn Fn(&S) -> bool>, - target: Option<Box<dyn CommandNode<S>>>, + target: Option<Box<dyn CommandNodeTrait<S>>>, modifier: Option<Box<dyn RedirectModifier<S>>>, forks: bool, @@ -26,7 +32,12 @@ impl<'a, S> RequiredArgumentBuilder<'a, S> { name, type_: type_, suggestions_provider: None, - base: BaseArgumentBuilder::default(), + arguments: RootCommandNode::new(), + command: None, + requirement: Box::new(|_| true), + target: None, + modifier: None, + forks: false, } } @@ -62,15 +73,13 @@ impl<'a, S> RequiredArgumentBuilder<'a, S> { let result = ArgumentCommandNode { name: self.name, type_: self.type_, - base: BaseCommandNode { - command: self.base.command(), - requirement: self.base.requirement(), - redirect: self.base.get_redirect(), - modifier: self.base.get_redirect_modifier(), - forks: self.base.forks, - ..BaseCommandNode::default() - }, + command: self.base.command(), + requirement: self.base.requirement(), + redirect: self.base.get_redirect(), + modifier: self.base.get_redirect_modifier(), + forks: self.base.forks, custom_suggestions: self.base.custom_suggestions, + ..ArgumentCommandNode::default() }; for argument in self.base.arguments() { |
