aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/builder/required_argument_builder.rs
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-04-20 01:34:12 +0000
committerGitHub <noreply@github.com>2022-04-20 01:34:12 +0000
commit5fd87615cf1514c7f9a0358988964768ded3f06e (patch)
tree001c3c760fdae8fe7b72cacb1f87d3703cc4e82c /azalea-brigadier/src/builder/required_argument_builder.rs
parentd09762f5d38ab1200fb08ca3b1178813b4e47081 (diff)
parentbe194c1ca136100fd8f53ed068d82c9f7ae32870 (diff)
downloadazalea-drasl-5fd87615cf1514c7f9a0358988964768ded3f06e.tar.xz
Merge pull request #1 from mat-1/brigadier
azalea-brigadier
Diffstat (limited to 'azalea-brigadier/src/builder/required_argument_builder.rs')
-rw-r--r--azalea-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 100644
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())
+}