diff options
| author | mat <github@matdoes.dev> | 2022-01-13 20:08:53 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-01-13 20:08:53 -0600 |
| commit | 760816c81fab414cc42ab1e75506fc816bcf9681 (patch) | |
| tree | e5a52f4f90969ed06db0b81a0fe37e7a94fdca53 /azalea-brigadier/src/builder | |
| parent | eb111be1f107696939b994f5de6e060cf972a732 (diff) | |
| download | azalea-drasl-760816c81fab414cc42ab1e75506fc816bcf9681.tar.xz | |
stuff
Diffstat (limited to 'azalea-brigadier/src/builder')
| -rw-r--r-- | azalea-brigadier/src/builder/argument_builder.rs | 44 | ||||
| -rw-r--r-- | azalea-brigadier/src/builder/literal_argument_builder.rs | 14 | ||||
| -rw-r--r-- | azalea-brigadier/src/builder/required_argument_builder.rs | 4 |
3 files changed, 47 insertions, 15 deletions
diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index 6355456a..8c30bd44 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -3,7 +3,10 @@ use crate::{ command::Command, redirect_modifier::RedirectModifier, single_redirect_modifier::SingleRedirectModifier, - tree::{command_node::CommandNode, root_command_node::RootCommandNode}, + tree::{ + command_node::{BaseCommandNode, CommandNode}, + root_command_node::RootCommandNode, + }, }; pub struct BaseArgumentBuilder<'a, S> @@ -11,10 +14,10 @@ where S: Sized, { arguments: RootCommandNode<'a, S>, - command: Option<&'a dyn Command<S>>, - requirement: &'a dyn Fn(&S) -> bool, - target: Option<&'a dyn CommandNode<S>>, - modifier: Option<&'a dyn RedirectModifier<S>>, + command: Option<Box<dyn Command<S>>>, + requirement: Box<dyn Fn(&S) -> bool>, + target: Option<Box<dyn CommandNode<S>>>, + modifier: Option<Box<dyn RedirectModifier<S>>>, forks: bool, } @@ -22,15 +25,18 @@ pub trait ArgumentBuilder<S, T> where T: ArgumentBuilder<S, T>, { - fn build(self) -> dyn CommandNode<S>; + fn build(self) -> Box<dyn CommandNode<S>>; } -impl<S> BaseArgumentBuilder<'_, S> { - pub fn then(&mut self, command: dyn CommandNode<S>) -> Result<&mut Self, String> { +impl<'a, S> BaseArgumentBuilder<'a, S> { + pub fn then( + &mut self, + command: Box<dyn ArgumentBuilder<S, Self>>, + ) -> Result<&mut Self, String> { if self.target.is_some() { return Err("Cannot add children to a redirected node".to_string()); } - self.command = command; + self.command = Some(command); Ok(self) } @@ -103,4 +109,24 @@ impl<S> BaseArgumentBuilder<'_, S> { pub fn is_fork(&self) -> bool { self.forks } + + pub fn build(self) -> BaseCommandNode<'a, S> { + let result: BaseCommandNode<'a, S> = BaseCommandNode { + command: self.command, + requirement: self.requirement, + redirect: self.target, + modifier: self.modifier, + forks: self.forks, + + arguments: Default::default(), + children: Default::default(), + literals: Default::default(), + }; + + for argument in self.arguments() { + result.add_child(argument); + } + + result + } } diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs index 8039dff0..0bd6071c 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -1,8 +1,9 @@ use crate::{ - arguments::argument_type::ArgumentType, tree::literal_command_node::LiteralCommandNode, + arguments::argument_type::ArgumentType, + tree::{command_node::CommandNode, literal_command_node::LiteralCommandNode}, }; -use super::argument_builder::BaseArgumentBuilder; +use super::argument_builder::{ArgumentBuilder, BaseArgumentBuilder}; pub struct LiteralArgumentBuilder<'a, S> { literal: String, @@ -21,9 +22,14 @@ impl<'a, S> LiteralArgumentBuilder<'a, S> { pub fn literal(name: String) -> Self { Self::new(name) } +} - pub fn build(self) -> LiteralCommandNode<'a, S> { - let result = LiteralCommandNode::new(self.literal, self.base); +impl<'a, S, T> ArgumentBuilder<S, T> for LiteralArgumentBuilder<'a, S> +where + T: ArgumentBuilder<S, T>, +{ + fn build(self) -> Box<dyn CommandNode<S>> { + let result = LiteralCommandNode::new(self.literal, self.base.build()); for argument in self.base.arguments { result.add_child(argument); diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs index 29af7f6f..b69c9dab 100644 --- a/azalea-brigadier/src/builder/required_argument_builder.rs +++ b/azalea-brigadier/src/builder/required_argument_builder.rs @@ -19,10 +19,10 @@ pub struct RequiredArgumentBuilder<'a, S> { } impl<'a, S> RequiredArgumentBuilder<'a, S> { - pub fn new(name: String, type_: dyn ArgumentType<Into = dyn Any>) -> Self { + pub fn new(name: String, type_: Box<dyn ArgumentType<Into = dyn Any>>) -> Self { Self { name, - type_: &type_, + type_: type_, suggestions_provider: None, base: BaseArgumentBuilder::new(name, type_), } |
