From 60b129b3a62b66dd389d1775892405fe735b9540 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 10 Jan 2022 20:29:46 -0600 Subject: progress --- azalea-brigadier/src/builder/argument_builder.rs | 50 ++++++++-------- .../src/builder/literal_argument_builder.rs | 32 ++++++++++ .../src/builder/required_argument_builder.rs | 70 +++++----------------- 3 files changed, 70 insertions(+), 82 deletions(-) (limited to 'azalea-brigadier/src/builder') diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index 19706a22..bd2a2c15 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -5,27 +5,25 @@ use crate::{ tree::{command_node::CommandNode, root_command_node::RootCommandNode}, }; -pub struct BaseArgumentBuilder +pub struct BaseArgumentBuilder<'a, S, T> where - T: ArgumentBuilder, + S: Sized, + T: Sized, { - arguments: RootCommandNode, - command: Option>, - requirement: dyn Fn(&S) -> bool, - target: Option>, - modifier: Option>, + arguments: RootCommandNode<'a, S, T>, + command: Option<&'a dyn Command>, + requirement: &'a dyn Fn(&S) -> bool, + target: Option<&'a dyn CommandNode>, + modifier: Option<&'a dyn RedirectModifier>, forks: bool, } pub trait ArgumentBuilder { - fn build(self) -> dyn CommandNode; + fn build(self) -> dyn CommandNode; } -impl BaseArgumentBuilder -where - T: ArgumentBuilder, -{ - pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut T, String> { +impl BaseArgumentBuilder<'_, S, T> { + pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut T, String> { if self.target.is_some() { return Err("Cannot add children to a redirected node".to_string()); } @@ -33,20 +31,20 @@ where Ok(self) } - pub fn arguments(&self) -> &Vec> { + 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 T { 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 T { self.requirement = requirement; self } @@ -55,14 +53,14 @@ where self.requirement } - pub fn redirect(&mut self, target: dyn CommandNode) -> &mut T { + pub fn redirect(&mut self, target: &dyn CommandNode) -> &mut T { self.forward(target, None, false) } pub fn redirect_modifier( &mut self, - target: dyn CommandNode, - modifier: dyn SingleRedirectModifier, + target: &dyn CommandNode, + modifier: &dyn SingleRedirectModifier, ) -> &mut T { // forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false); self.forward(target, modifier.map(|m| |o| vec![m.apply(o)]), false) @@ -70,16 +68,16 @@ where pub fn fork( &mut self, - target: dyn CommandNode, - modifier: dyn RedirectModifier, + target: &dyn CommandNode, + modifier: &dyn RedirectModifier, ) -> &mut T { self.forward(target, Some(modifier), true) } pub fn forward( &mut self, - target: dyn CommandNode, - modifier: Option>, + target: &dyn CommandNode, + modifier: Option<&dyn RedirectModifier>, fork: bool, ) -> Result<&mut T, String> { if !self.arguments.get_children().is_empty() { @@ -91,11 +89,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 e69de29b..cf9f1ee9 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -0,0 +1,32 @@ +use crate::tree::literal_command_node::LiteralCommandNode; + +use super::argument_builder::BaseArgumentBuilder; + +pub struct LiteralArgumentBuilder<'a, S, T> { + literal: String, + + pub base: BaseArgumentBuilder<'a, S, T>, +} + +impl<'a, S, T> LiteralArgumentBuilder<'a, S, T> { + pub fn new(literal: String) -> Self { + Self { + literal, + base: BaseArgumentBuilder::default(), + } + } + + pub fn literal(name: String) -> Self { + Self::new(name) + } + + pub fn build(self) -> LiteralCommandNode<'a, S, T> { + let result = LiteralCommandNode::new(self.literal, self.base); + + for argument in self.base.arguments { + result.add_child(argument); + } + + result + } +} diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs index 3ec613c4..6f6fa8eb 100644 --- a/azalea-brigadier/src/builder/required_argument_builder.rs +++ b/azalea-brigadier/src/builder/required_argument_builder.rs @@ -1,92 +1,50 @@ use crate::{ - arguments::argument_type::ArgumentType, suggestion::suggestion_provider::SuggestionProvider, tree::{argument_command_node::ArgumentCommandNode, command_node::BaseCommandNode}, }; use super::argument_builder::BaseArgumentBuilder; -// private RequiredArgumentBuilder(final String name, final ArgumentType type) { -// this.name = name; -// this.type = type; -// } - -// public static RequiredArgumentBuilder argument(final String name, final ArgumentType type) { -// return new RequiredArgumentBuilder<>(name, type); -// } - -// public RequiredArgumentBuilder suggests(final SuggestionProvider provider) { -// this.suggestionsProvider = provider; -// return getThis(); -// } - -// public SuggestionProvider getSuggestionsProvider() { -// return suggestionsProvider; -// } - -// @Override -// protected RequiredArgumentBuilder getThis() { -// return this; -// } - -// public ArgumentType getType() { -// return type; -// } - -// public String getName() { -// return name; -// } - -// public ArgumentCommandNode build() { -// final ArgumentCommandNode result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider()); - -// for (final CommandNode argument : getArguments()) { -// result.addChild(argument); -// } - -// return result; -// } - -pub struct RequiredArgumentBuilder { +pub struct RequiredArgumentBuilder<'a, S, T> { // private final String name; // private final ArgumentType type; // private SuggestionProvider suggestionsProvider = null; name: String, - type_: dyn ArgumentType, - suggestions_provider: Option>, + type_: &'a T, + suggestions_provider: Option<&'a dyn SuggestionProvider>, - pub base: BaseArgumentBuilder, + pub base: BaseArgumentBuilder<'a, S, T>, } -impl RequiredArgumentBuilder { - pub fn new(name: String, type_: dyn ArgumentType) -> Self { +impl<'a, S, T> RequiredArgumentBuilder<'a, S, T> { + pub fn new(name: String, type_: T) -> Self { Self { name, - type_, + type_: &type_, suggestions_provider: None, base: BaseArgumentBuilder::new(name, type_), } } - pub fn argument(name: String, type_: dyn ArgumentType) -> Self { + pub fn argument(name: String, type_: T) -> 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) -> &dyn ArgumentType { - &self.type_ + pub fn get_type(&self) -> &T { + self.type_ } pub fn name(&self) -> &str { - &self.name + self.name } // final ArgumentCommandNode result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect(), getRedirectModifier(), isFork(), getSuggestionsProvider()); @@ -96,7 +54,7 @@ impl RequiredArgumentBuilder { // } // return result; - pub fn build(self) -> ArgumentCommandNode { + pub fn build(self) -> ArgumentCommandNode<'a, S, T> { let result = ArgumentCommandNode { name: self.name, type_: &self.type_, -- cgit v1.2.3