aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/tree
diff options
context:
space:
mode:
authorUbuntu <github@matdoes.dev>2022-01-12 00:40:43 +0000
committerUbuntu <github@matdoes.dev>2022-01-12 00:40:43 +0000
commit270507736af57aae6801dc9eb3c3132139d0d07b (patch)
treea8fa1d1b8d038eb9d7e2061342026d23ddbd9027 /azalea-brigadier/src/tree
parentcc4fe62fc82842e0bde628437a45d55c6a82f1f3 (diff)
downloadazalea-drasl-270507736af57aae6801dc9eb3c3132139d0d07b.tar.xz
a
Diffstat (limited to 'azalea-brigadier/src/tree')
-rw-r--r--azalea-brigadier/src/tree/argument_command_node.rs17
-rw-r--r--azalea-brigadier/src/tree/command_node.rs59
-rw-r--r--azalea-brigadier/src/tree/literal_command_node.rs20
-rw-r--r--azalea-brigadier/src/tree/root_command_node.rs19
4 files changed, 97 insertions, 18 deletions
diff --git a/azalea-brigadier/src/tree/argument_command_node.rs b/azalea-brigadier/src/tree/argument_command_node.rs
index 647b6c35..3fc1bb50 100644
--- a/azalea-brigadier/src/tree/argument_command_node.rs
+++ b/azalea-brigadier/src/tree/argument_command_node.rs
@@ -21,6 +21,7 @@ use super::command_node::{BaseCommandNode, CommandNode};
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
@@ -34,7 +35,10 @@ where
pub base: BaseCommandNode<'a, S, T>,
}
-impl<S, T> ArgumentCommandNode<'_, S, T> {
+impl<S, T> ArgumentCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types>,
+{
fn get_type(&self) -> &T {
&self.type_
}
@@ -44,7 +48,11 @@ impl<S, T> ArgumentCommandNode<'_, S, T> {
}
}
-impl<'a, S, T> CommandNode<S, T> for ArgumentCommandNode<'a, S, T> {
+impl<'a, S, T> CommandNode<S, T> for ArgumentCommandNode<'a, S, T>
+where
+ T: ArgumentType<dyn Types> + Clone,
+ S: Clone,
+{
fn name(&self) -> &str {
&self.name
}
@@ -117,7 +125,10 @@ impl<'a, S, T> CommandNode<S, T> for ArgumentCommandNode<'a, S, T> {
}
}
-impl Display for ArgumentCommandNode<'_, (), (), ()> {
+impl<S, T> Display for ArgumentCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types>,
+{
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 8e262f0b..f3be1597 100644
--- a/azalea-brigadier/src/tree/command_node.rs
+++ b/azalea-brigadier/src/tree/command_node.rs
@@ -1,7 +1,6 @@
-use std::collections::HashMap;
-
+use super::{argument_command_node::ArgumentCommandNode, literal_command_node::LiteralCommandNode};
use crate::{
- arguments::argument_type::ArgumentType,
+ arguments::argument_type::{ArgumentType, Types},
builder::argument_builder::ArgumentBuilder,
command::Command,
context::{command_context::CommandContext, command_context_builder::CommandContextBuilder},
@@ -10,10 +9,14 @@ use crate::{
string_reader::StringReader,
suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder},
};
+use dyn_clonable::*;
+use std::{collections::HashMap, fmt::Debug};
-use super::{argument_command_node::ArgumentCommandNode, literal_command_node::LiteralCommandNode};
-
-pub struct BaseCommandNode<'a, S, T> {
+#[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>>,
@@ -24,9 +27,49 @@ pub struct BaseCommandNode<'a, S, T> {
command: Option<&'a dyn Command<S, T>>,
}
-impl<S, T> BaseCommandNode<'_, S, T> {}
+impl<S, T> BaseCommandNode<'_, S, T> where T: ArgumentType<dyn Types> {}
+
+impl<S, T> Clone for BaseCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types>,
+{
+ fn clone(&self) -> Self {
+ Self {
+ children: self.children.clone(),
+ literals: self.literals.clone(),
+ arguments: self.arguments.clone(),
+ requirement: self.requirement.clone(),
+ redirect: self.redirect.clone(),
+ modifier: self.modifier.clone(),
+ forks: self.forks.clone(),
+ command: self.command.clone(),
+ }
+ }
+}
+
+impl<S, T> Debug for BaseCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types>,
+{
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("BaseCommandNode")
+ .field("children", &self.children)
+ .field("literals", &self.literals)
+ .field("arguments", &self.arguments)
+ .field("requirement", &self.requirement)
+ .field("redirect", &self.redirect)
+ .field("modifier", &self.modifier)
+ .field("forks", &self.forks)
+ .field("command", &self.command)
+ .finish()
+ }
+}
-pub trait CommandNode<S, T> {
+#[clonable]
+pub trait CommandNode<S, T>: Clone
+where
+ T: ArgumentType<dyn Types>,
+{
fn name(&self) -> &str;
fn usage_text(&self) -> &str;
fn parse(
diff --git a/azalea-brigadier/src/tree/literal_command_node.rs b/azalea-brigadier/src/tree/literal_command_node.rs
index fe933669..021a3ea6 100644
--- a/azalea-brigadier/src/tree/literal_command_node.rs
+++ b/azalea-brigadier/src/tree/literal_command_node.rs
@@ -1,4 +1,5 @@
use crate::{
+ arguments::argument_type::{ArgumentType, Types},
builder::literal_argument_builder::LiteralArgumentBuilder,
command::Command,
context::{command_context::CommandContext, command_context_builder::CommandContextBuilder},
@@ -12,15 +13,22 @@ use crate::{
use super::command_node::{BaseCommandNode, CommandNode};
-#[derive(Hash, PartialEq, Eq, Debug, Clone)]
-pub struct LiteralCommandNode<'a, S, T> {
+#[derive(Debug, Clone)]
+pub struct LiteralCommandNode<'a, S, T>
+where
+ // each argument command node has its own different type
+ T: ArgumentType<dyn Types>,
+{
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>,
}
-impl<'a, S, T> LiteralCommandNode<'a, S, T> {
+impl<'a, S, T> LiteralCommandNode<'a, S, T>
+where
+ T: ArgumentType<dyn Types>,
+{
pub fn new(literal: String, base: BaseCommandNode<S, T>) -> Self {
let literal_lowercase = literal.to_lowercase();
Self {
@@ -51,7 +59,11 @@ impl<'a, S, T> LiteralCommandNode<'a, S, T> {
}
}
-impl<S, T> CommandNode<S, T> for LiteralCommandNode<'_, S, T> {
+impl<S, T> CommandNode<S, T> for LiteralCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types> + Clone,
+ S: Clone,
+{
fn name(&self) -> &str {
&self.literal
}
diff --git a/azalea-brigadier/src/tree/root_command_node.rs b/azalea-brigadier/src/tree/root_command_node.rs
index 36787340..ded5fa77 100644
--- a/azalea-brigadier/src/tree/root_command_node.rs
+++ b/azalea-brigadier/src/tree/root_command_node.rs
@@ -1,6 +1,7 @@
use std::fmt::{Display, Formatter};
use crate::{
+ arguments::argument_type::{ArgumentType, Types},
context::{command_context::CommandContext, command_context_builder::CommandContextBuilder},
exceptions::{
builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException,
@@ -11,12 +12,21 @@ use crate::{
use super::command_node::{BaseCommandNode, CommandNode};
-pub struct RootCommandNode<'a, S, T> {
+#[derive(Clone, Default)]
+pub struct RootCommandNode<'a, S, T>
+where
+ // each argument command node has its own different type
+ T: ArgumentType<dyn Types>,
+{
// Since Rust doesn't have extending, we put the struct this is extending as the "base" field
pub base: BaseCommandNode<'a, S, T>,
}
-impl<S, T> CommandNode<S, T> for RootCommandNode<'_, S, T> {
+impl<S, T> CommandNode<S, T> for RootCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types> + Clone,
+ S: Clone,
+{
fn name(&self) -> &str {
""
}
@@ -53,7 +63,10 @@ impl<S, T> CommandNode<S, T> for RootCommandNode<'_, S, T> {
}
}
-impl<S, T> Display for RootCommandNode<'_, S, T> {
+impl<S, T> Display for RootCommandNode<'_, S, T>
+where
+ T: ArgumentType<dyn Types>,
+{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "<root>")
}