aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/builder/required_argument_builder.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-24 17:37:57 -0500
committermat <github@matdoes.dev>2022-04-24 17:37:57 -0500
commit3e507f0db4020eaf406ba69aae3d4dc1301d29ac (patch)
treeca6c127c9db6dfd14511e98944fc031fe5f1e43a /azalea-brigadier/src/builder/required_argument_builder.rs
parent9f576c5600ba9a244bc0d433bb7de174284066a2 (diff)
parentb7641ff308aab7840d2a2253ae50f8ee496b2a97 (diff)
downloadazalea-drasl-3e507f0db4020eaf406ba69aae3d4dc1301d29ac.tar.xz
Merge branch 'main' into auth
Diffstat (limited to 'azalea-brigadier/src/builder/required_argument_builder.rs')
-rwxr-xr-xazalea-brigadier/src/builder/required_argument_builder.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs
new file mode 100755
index 00000000..9d4d9e0a
--- /dev/null
+++ b/azalea-brigadier/src/builder/required_argument_builder.rs
@@ -0,0 +1,45 @@
+use super::argument_builder::{ArgumentBuilder, ArgumentBuilderType};
+use crate::{
+ arguments::ArgumentType, exceptions::CommandSyntaxException, string_reader::StringReader,
+};
+use std::{any::Any, fmt::Debug, rc::Rc};
+
+/// An argument node type. The `T` type parameter is the type of the argument,
+/// which can be anything.
+#[derive(Clone)]
+pub struct Argument {
+ pub name: String,
+ parser: Rc<dyn ArgumentType>,
+}
+impl Argument {
+ pub fn new(name: &str, parser: Rc<dyn ArgumentType>) -> Self {
+ Self {
+ name: name.to_string(),
+ parser,
+ }
+ }
+
+ pub fn parse(&self, reader: &mut StringReader) -> Result<Rc<dyn Any>, CommandSyntaxException> {
+ self.parser.parse(reader)
+ }
+}
+
+impl From<Argument> for ArgumentBuilderType {
+ fn from(argument: Argument) -> Self {
+ Self::Argument(argument)
+ }
+}
+
+impl Debug for Argument {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("Argument")
+ .field("name", &self.name)
+ // .field("parser", &self.parser)
+ .finish()
+ }
+}
+
+/// Shortcut for creating a new argument builder node.
+pub fn argument<S>(name: &str, parser: impl ArgumentType + 'static) -> ArgumentBuilder<S> {
+ ArgumentBuilder::new(Argument::new(name, Rc::new(parser)).into())
+}