From 4ff67d4917ce333232189e86aee09f2d82451fc6 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 3 Feb 2022 02:16:24 +0000 Subject: a --- azalea-brigadier/src/tree/command_node.rs | 56 +++++++++---------------------- 1 file changed, 16 insertions(+), 40 deletions(-) (limited to 'azalea-brigadier/src/tree/command_node.rs') diff --git a/azalea-brigadier/src/tree/command_node.rs b/azalea-brigadier/src/tree/command_node.rs index 30907163..207e114e 100644 --- a/azalea-brigadier/src/tree/command_node.rs +++ b/azalea-brigadier/src/tree/command_node.rs @@ -15,43 +15,14 @@ use crate::{ use std::ops::Deref; use std::{any::Any, collections::HashMap, fmt::Debug}; -enum CommandNodeEnum<'a, S> { - Literal(LiteralCommandNode<'a, S>), - Argument(ArgumentCommandNode<'a, S>), - Root(RootCommandNode<'a, S>), +#[enum_dispatch(CommandNodeTrait)] +enum CommandNodeEnum { + Literal(LiteralCommandNode), + Argument(ArgumentCommandNode), + Root(RootCommandNode), } -impl<'a, S> Deref for CommandNodeEnum<'a, S> { - type Target = dyn CommandNodeTrait; - - fn deref(&self) -> &Self::Target { - match self { - CommandNodeEnum::Literal(node) => *node as &Self::Target, - CommandNodeEnum::Argument(node) => *node as &Self::Target, - CommandNodeEnum::Root(node) => *node as &Self::Target, - } - } -} - -impl From> for CommandNodeEnum<'_, S> { - fn from(node: LiteralCommandNode<'_, S>) -> Self { - CommandNodeEnum::Literal(node) - } -} - -impl From> for CommandNodeEnum<'_, S> { - fn from(node: ArgumentCommandNode<'_, S>) -> Self { - CommandNodeEnum::Argument(node) - } -} - -impl From> for CommandNodeEnum<'_, S> { - fn from(node: RootCommandNode<'_, S>) -> Self { - CommandNodeEnum::Root(node) - } -} - -impl CommandNodeEnum<'_, S> { +impl CommandNodeEnum { fn redirect_modifier(&self) -> Option<&dyn RedirectModifier> { (*self).modifier.as_ref().map(|modifier| modifier.as_ref()) } @@ -92,10 +63,10 @@ impl CommandNodeEnum<'_, S> { } } } -pub struct BaseCommandNode<'a, S> { +pub struct BaseCommandNode { children: HashMap>>, - literals: HashMap>, - arguments: HashMap>, + literals: HashMap>, + arguments: HashMap>, pub requirement: Box bool>, redirect: Option>>, modifier: Option>>, @@ -118,7 +89,7 @@ pub struct BaseCommandNode<'a, S> { // } // } -impl Debug for BaseCommandNode<'_, S> { +impl Debug for BaseCommandNode { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("BaseCommandNode") .field("children", &self.children) @@ -133,7 +104,7 @@ impl Debug for BaseCommandNode<'_, S> { } } -impl Default for BaseCommandNode<'_, S> { +impl Default for BaseCommandNode { fn default() -> Self { Self { children: HashMap::new(), @@ -148,6 +119,7 @@ impl Default for BaseCommandNode<'_, S> { } } +#[enum_dispatch] pub trait CommandNodeTrait { fn name(&self) -> &str; fn usage_text(&self) -> &str; @@ -164,4 +136,8 @@ pub trait CommandNodeTrait { fn is_valid_input(&self, input: &str) -> bool; fn create_builder(&self) -> Box>; fn get_examples(&self) -> Vec; + + fn redirect_modifier(&self) -> Option<&dyn RedirectModifier>; + fn can_use(&self, source: S) -> bool; + fn add_child(&self, node: &Box>) -> Result<(), String>; } -- cgit v1.2.3