From d959fb2d0cc4d8ad97eae86666876e22b2e50613 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 9 Jan 2022 00:14:39 -0600 Subject: add string_reader --- Cargo.lock | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index eca4d4fa..67259fef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,10 @@ dependencies = [ "uuid", ] +[[package]] +name = "azalea-brigadier" +version = "0.1.0" + [[package]] name = "azalea-chat" version = "0.1.0" -- cgit v1.2.3 From 315f2258190b33c63df7797a97178019f5aea02b Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 9 Jan 2022 22:33:45 -0600 Subject: add some more stuff from brigadier --- .gitignore | 1 + Cargo.lock | 3 + azalea-brigadier/Cargo.toml | 3 +- azalea-brigadier/src/arguments/argument_type.rs | 4 +- .../src/arguments/bool_argument_type.rs | 25 ++- azalea-brigadier/src/arguments/mod.rs | 6 + azalea-brigadier/src/builder/argument_builder.rs | 106 ++++++++++++ azalea-brigadier/src/builder/mod.rs | 3 + azalea-brigadier/src/command.rs | 10 ++ azalea-brigadier/src/command_dispatcher.rs | 23 +++ azalea-brigadier/src/context/command_context.rs | 87 ++++++++++ .../src/context/command_context_builder.rs | 180 +++++++++++++++++++++ azalea-brigadier/src/context/parsed_argument.rs | 24 +++ .../src/context/parsed_command_node.rs | 22 +++ azalea-brigadier/src/context/string_range.rs | 45 ++++++ azalea-brigadier/src/context/suggestion_context.rs | 6 + azalea-brigadier/src/lib.rs | 3 + azalea-brigadier/src/redirect_modifier.rs | 8 + azalea-brigadier/src/single_redirect_modifier.rs | 8 + .../src/suggestion/suggestion_provider.rs | 14 ++ azalea-brigadier/src/tree/argument_command_node.rs | 118 ++++++++++++++ azalea-brigadier/src/tree/command_node.rs | 52 ++++++ azalea-brigadier/src/tree/literal_command_node.rs | 96 +++++++++++ azalea-brigadier/src/tree/mod.rs | 4 + azalea-brigadier/src/tree/root_command_node.rs | 61 +++++++ 25 files changed, 902 insertions(+), 10 deletions(-) (limited to 'Cargo.lock') diff --git a/.gitignore b/.gitignore index ea8c4bf7..f97818c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/doc \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 67259fef..0eeb6520 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,6 +70,9 @@ dependencies = [ [[package]] name = "azalea-brigadier" version = "0.1.0" +dependencies = [ + "lazy_static", +] [[package]] name = "azalea-chat" diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml index c617ffb1..3694a4b7 100644 --- a/azalea-brigadier/Cargo.toml +++ b/azalea-brigadier/Cargo.toml @@ -1,8 +1,9 @@ [package] +edition = "2021" name = "azalea-brigadier" version = "0.1.0" -edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +lazy_static = "^1.4" diff --git a/azalea-brigadier/src/arguments/argument_type.rs b/azalea-brigadier/src/arguments/argument_type.rs index 4c48d6bb..34d57285 100644 --- a/azalea-brigadier/src/arguments/argument_type.rs +++ b/azalea-brigadier/src/arguments/argument_type.rs @@ -5,7 +5,7 @@ use crate::{ suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, }; -pub trait ArgumentType { +pub trait ArgumentType { // T parse(StringReader reader) throws CommandSyntaxException; // default CompletableFuture listSuggestions(final CommandContext context, final SuggestionsBuilder builder) { @@ -16,7 +16,7 @@ pub trait ArgumentType { // return Collections.emptyList(); // } - fn parse(reader: &mut StringReader) -> Result; + fn parse(reader: &mut StringReader) -> Result; fn list_suggestions( context: &CommandContext, diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs index d4a33517..f4c03373 100644 --- a/azalea-brigadier/src/arguments/bool_argument_type.rs +++ b/azalea-brigadier/src/arguments/bool_argument_type.rs @@ -1,8 +1,19 @@ -struct BoolArgumentType { - // private static final Collection EXAMPLES = Arrays.asList("true", "false"); - const EXAMPLES: &'static [&'static str] = &["true", "false"]; -} +use crate::context::command_context::CommandContext; + +use super::argument_type::ArgumentType; + +struct BoolArgumentType {} + +impl ArgumentType for BoolArgumentType {} -impl ArgumentType for BoolArgumentType { - -} \ No newline at end of file +impl BoolArgumentType { + const EXAMPLES: &'static [&'static str] = &["true", "false"]; + + fn bool() -> Self { + Self {} + } + + fn get_bool(context: CommandContext, name: String) { + context.get_argument::(name) + } +} diff --git a/azalea-brigadier/src/arguments/mod.rs b/azalea-brigadier/src/arguments/mod.rs index 50b0f09b..487c5db7 100644 --- a/azalea-brigadier/src/arguments/mod.rs +++ b/azalea-brigadier/src/arguments/mod.rs @@ -1 +1,7 @@ pub mod argument_type; +pub mod bool_argument_type; +pub mod double_argument_type; +pub mod float_argument_type; +pub mod integer_argument_type; +pub mod long_argument_type; +pub mod string_argument_type; diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index e69de29b..8a64a9e4 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -0,0 +1,106 @@ +use crate::{ + command::Command, + redirect_modifier::RedirectModifier, + single_redirect_modifier::SingleRedirectModifier, + tree::{command_node::CommandNode, root_command_node::RootCommandNode}, +}; + +pub struct BaseArgumentBuilder +where + T: ArgumentBuilder, +{ + arguments: RootCommandNode, + command: dyn Command, + requirement: dyn Fn(&S) -> bool, + target: Option>, + modifier: Option>, + forks: bool, +} + +pub trait ArgumentBuilder { + fn this() -> T; + fn build(self) -> dyn CommandNode; +} + +impl BaseArgumentBuilder +where + T: ArgumentBuilder, +{ + pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut T, String> { + if self.target.is_some() { + return Err("Cannot add children to a redirected node".to_string()); + } + self.command = command; + Ok(self) + } + + pub fn arguments(&self) -> &Vec> { + &self.arguments.get_children() + } + + pub fn executes(&mut self, command: dyn Command) -> &mut T { + self.command = command; + self + } + + pub fn command(&self) -> dyn Command { + self.command + } + + pub fn requires(&mut self, requirement: dyn Fn(&S) -> bool) -> &mut T { + self.requirement = requirement; + self + } + + pub fn requirement(&self) -> dyn Fn(&S) -> bool { + self.requirement + } + + pub fn redirect(&mut self, target: dyn CommandNode) -> &mut T { + self.forward(target, None, false) + } + + pub fn redirect_modifier( + &mut self, + target: dyn CommandNode, + modifier: dyn SingleRedirectModifier, + ) -> &mut T { + // forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false); + self.forward(target, modifier.map(|m| |o| vec![m.apply(o)]), false) + } + + pub fn fork( + &mut self, + target: dyn CommandNode, + modifier: dyn RedirectModifier, + ) -> &mut T { + self.forward(target, Some(modifier), true) + } + + pub fn forward( + &mut self, + target: dyn CommandNode, + modifier: Option>, + fork: bool, + ) -> Result<&mut T, String> { + if !self.arguments.get_children().is_empty() { + return Err("Cannot forward a node with children".to_string()); + } + self.target = Some(target); + self.modifier = modifier; + self.forks = fork; + Ok(self) + } + + pub fn redirect(&self) -> Option<&dyn CommandNode> { + self.target.as_ref() + } + + pub fn redirect_modifier(&self) -> Option<&dyn RedirectModifier> { + self.modifier.as_ref() + } + + pub fn is_fork(&self) -> bool { + self.forks + } +} diff --git a/azalea-brigadier/src/builder/mod.rs b/azalea-brigadier/src/builder/mod.rs index e69de29b..26f2f644 100644 --- a/azalea-brigadier/src/builder/mod.rs +++ b/azalea-brigadier/src/builder/mod.rs @@ -0,0 +1,3 @@ +pub mod argument_builder; +pub mod literal_argument_builder; +pub mod required_argument_builder; diff --git a/azalea-brigadier/src/command.rs b/azalea-brigadier/src/command.rs index e69de29b..a76454b7 100644 --- a/azalea-brigadier/src/command.rs +++ b/azalea-brigadier/src/command.rs @@ -0,0 +1,10 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +pub const SINGLE_SUCCESS: i32 = 1; + +pub trait Command { + fn run(&self, context: &mut CommandContext) -> Result; +} diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index e69de29b..c476a39b 100644 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -0,0 +1,23 @@ +/// The core command dispatcher, for registering, parsing, and executing commands. +/// The `S` generic is a custom "source" type, such as a user or originator of a command +pub struct CommandDispatcher { + root: RootCommandNode, +} + +impl CommandDispatcher { + /// The string required to separate individual arguments in an input string + /// + /// See: [`ARGUMENT_SEPARATOR_CHAR`] + const ARGUMENT_SEPARATOR: &'static str = " "; + + /// The char required to separate individual arguments in an input string + /// + /// See: [`ARGUMENT_SEPARATOR`] + const ARGUMENT_SEPARATOR_CHAR: char = ' '; + + const USAGE_OPTIONAL_OPEN: &'static str = "["; + const USAGE_OPTIONAL_CLOSE: &'static str = "]"; + const USAGE_REQUIRED_OPEN: &'static str = "("; + const USAGE_REQUIRED_CLOSE: &'static str = ")"; + const USAGE_OR: &'static str = "|"; +} diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs index ddbb447e..c6210a88 100644 --- a/azalea-brigadier/src/context/command_context.rs +++ b/azalea-brigadier/src/context/command_context.rs @@ -1,3 +1,90 @@ +use super::{ + parsed_argument::ParsedArgument, parsed_command_node::ParsedCommandNode, + string_range::StringRange, +}; +use crate::{ + arguments::argument_type::ArgumentType, command::Command, redirect_modifier::RedirectModifier, + tree::command_node::CommandNode, +}; +use std::collections::HashMap; + pub struct CommandContext { source: S, + input: String, + command: dyn Command, + arguments: HashMap>, + root_node: dyn CommandNode, + nodes: Vec>, + range: StringRange, + child: Option>, + modifier: Option>, + forks: bool, +} + +impl CommandContext { + pub fn clone_for(&self, source: S) -> Self { + if self.source == source { + return self.clone(); + } + Self { + source, + input: self.input.clone(), + command: self.command.clone(), + arguments: self.arguments.clone(), + root_node: self.root_node.clone(), + nodes: self.nodes.clone(), + range: self.range.clone(), + child: self.child.clone(), + modifier: self.modifier.clone(), + forks: self.forks, + } + } + + fn child(&self) -> &Option> { + &self.child + } + + fn last_child(&self) -> &CommandContext { + let mut result = self; + while result.child.is_some() { + result = result.child.as_ref().unwrap(); + } + result + } + + fn command(&self) -> &dyn Command { + &self.command + } + + fn source(&self) -> &S { + &self.source + } + + // public V getArgument(final String name, final Class clazz) { + // final ParsedArgument argument = arguments.get(name); + + // if (argument == null) { + // throw new IllegalArgumentException("No such argument '" + name + "' exists on this command"); + // } + + // final Object result = argument.getResult(); + // if (PRIMITIVE_TO_WRAPPER.getOrDefault(clazz, clazz).isAssignableFrom(result.getClass())) { + // return (V) result; + // } else { + // throw new IllegalArgumentException("Argument '" + name + "' is defined as " + result.getClass().getSimpleName() + ", not " + clazz); + // } + // } + fn get_argument(&self, name: &str) -> Result { + let argument = self.arguments.get(name); + + if argument.is_none() { + return Err(format!( + "No such argument '{}' exists on this command", + name + )); + } + + let result = argument.unwrap().result(); + Ok(result) + } } diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs index e69de29b..e74b5b1c 100644 --- a/azalea-brigadier/src/context/command_context_builder.rs +++ b/azalea-brigadier/src/context/command_context_builder.rs @@ -0,0 +1,180 @@ +use std::collections::HashMap; + +use crate::{ + arguments::argument_type::ArgumentType, command::Command, + command_dispatcher::CommandDispatcher, redirect_modifier::RedirectModifier, + tree::command_node::CommandNode, +}; + +use super::{ + command_context::CommandContext, parsed_argument::ParsedArgument, + parsed_command_node::ParsedCommandNode, string_range::StringRange, + suggestion_context::SuggestionContext, +}; + +// public class CommandContextBuilder { +// private final Map> arguments = new LinkedHashMap<>(); +// private final CommandNode rootNode; +// private final List> nodes = new ArrayList<>(); +// private final CommandDispatcher dispatcher; +// private S source; +// private Command command; +// private CommandContextBuilder child; +// private StringRange range; +// private RedirectModifier modifier = null; +// private boolean forks; + +#[derive(Clone)] +pub struct CommandContextBuilder { + arguments: HashMap>, + root_node: dyn CommandNode, + nodes: Vec>, + dispatcher: CommandDispatcher, + source: S, + command: Box>, + child: Option>, + range: StringRange, + modifier: Option>>, + forks: bool, +} + +// public CommandContextBuilder(final CommandDispatcher dispatcher, final S source, final CommandNode rootNode, final int start) { +// this.rootNode = rootNode; +// this.dispatcher = dispatcher; +// this.source = source; +// this.range = StringRange.at(start); +// } + +impl CommandContextBuilder { + pub fn new( + dispatcher: CommandDispatcher, + source: S, + root_node: dyn CommandNode, + start: usize, + ) -> Self { + Self { + root_node, + dispatcher, + source, + range: StringRange::at(start), + ..Default::default() + } + } + + pub fn with_source(mut self, source: S) -> Self { + self.source = source; + self + } + + pub fn source(&self) -> &S { + &self.source + } + + pub fn root_node(&self) -> &dyn CommandNode { + &self.root_node + } + + pub fn with_argument( + mut self, + name: String, + argument: ParsedArgument, + ) -> Self { + self.arguments.insert(name, argument); + self + } + + pub fn arguments(&self) -> &HashMap> { + &self.arguments + } + + pub fn with_command(mut self, command: Box>) -> Self { + self.command = command; + self + } + + pub fn with_node(mut self, node: dyn CommandNode, range: StringRange) -> Self { + self.nodes.push(ParsedCommandNode::new(node, range)); + self.range = StringRange::encompassing(&self.range, &range); + self.modifier = node.redirect_modifier(); + self.forks = node.is_fork(); + self + } + + pub fn with_child(mut self, child: CommandContextBuilder) -> Self { + self.child = Some(child); + self + } + + pub fn child(&self) -> Option<&CommandContextBuilder> { + self.child.as_ref() + } + + pub fn last_child(&self) -> Option<&CommandContextBuilder> { + let mut result = self; + while let Some(child) = result.child() { + result = child; + } + Some(result) + } + + pub fn command(&self) -> &dyn Command { + &*self.command + } + + pub fn nodes(&self) -> &Vec> { + &self.nodes + } + + pub fn build(self, input: &str) -> CommandContext { + CommandContext { + source: self.source, + input, + arguments: self.arguments, + command: self.command, + root_node: self.root_node, + nodes: self.nodes, + range: self.range, + child: self.child.map(|child| child.build(input)), + modifier: self.modifier, + forks: self.forks, + } + } + + pub fn dispatcher(&self) -> &CommandDispatcher { + &self.dispatcher + } + + pub fn range(&self) -> &StringRange { + &self.range + } + + pub fn find_suggestion_context(&self, cursor: i32) -> Result, String> { + if self.range.start() <= cursor { + if self.range.end() < cursor { + if let Some(child) = self.child() { + child.find_suggestion_context(cursor); + } else if !self.nodes.is_empty() { + let last = self.nodes.last().unwrap(); + let end = last.range().end() + 1; + return SuggestionContext::new(last.node(), end); + } else { + return SuggestionContext::new(self.root_node, self.range.start()); + } + } else { + let prev = self.root_node; + for node in &self.nodes { + let node_range = node.range(); + if node_range.start() <= cursor && cursor <= node_range.end() { + return SuggestionContext::new(prev, node_range.start()); + } + prev = node.node(); + } + if prev.is_none() { + return Err(String::from("Can't find node before cursor")); + } + return SuggestionContext::new(prev.unwrap(), self.range.start()); + } + } + Err(String::from("Can't find node before cursor")) + } +} diff --git a/azalea-brigadier/src/context/parsed_argument.rs b/azalea-brigadier/src/context/parsed_argument.rs index e69de29b..5f9c2cdb 100644 --- a/azalea-brigadier/src/context/parsed_argument.rs +++ b/azalea-brigadier/src/context/parsed_argument.rs @@ -0,0 +1,24 @@ +use super::string_range::StringRange; + +#[derive(PartialEq, Eq, Hash)] +pub struct ParsedArgument { + range: StringRange, + result: T, +} + +impl ParsedArgument { + fn new(start: usize, end: usize, result: T) -> Self { + Self { + range: StringRange::between(start, end), + result, + } + } + + fn range(&self) -> &StringRange { + &self.range + } + + fn result(&self) -> &T { + &self.result + } +} diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs index e69de29b..98e99959 100644 --- a/azalea-brigadier/src/context/parsed_command_node.rs +++ b/azalea-brigadier/src/context/parsed_command_node.rs @@ -0,0 +1,22 @@ +use super::string_range::StringRange; +use crate::tree::command_node::CommandNode; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct ParsedCommandNode { + node: dyn CommandNode, + range: StringRange, +} + +impl ParsedCommandNode { + fn new(node: dyn CommandNode, range: StringRange) -> Self { + Self { node, range } + } + + fn node(&self) -> &dyn CommandNode { + &self.node + } + + fn range(&self) -> &StringRange { + &self.range + } +} diff --git a/azalea-brigadier/src/context/string_range.rs b/azalea-brigadier/src/context/string_range.rs index e69de29b..d775ab68 100644 --- a/azalea-brigadier/src/context/string_range.rs +++ b/azalea-brigadier/src/context/string_range.rs @@ -0,0 +1,45 @@ +use std::cmp; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct StringRange { + start: usize, + end: usize, +} + +impl StringRange { + pub fn new(start: usize, end: usize) -> Self { + Self { start, end } + } + + pub fn at(pos: usize) -> Self { + Self::new(pos, pos) + } + + pub fn between(start: usize, end: usize) -> Self { + Self::new(start, end) + } + + pub fn encompassing(a: &Self, b: &Self) -> Self { + Self::new(cmp::min(a.start, b.start), cmp::max(a.end, b.end)) + } + + pub fn start(&self) -> usize { + self.start + } + + pub fn end(&self) -> usize { + self.end + } + + pub fn get(&self, reader: &str) -> &str { + &reader[self.start..self.end] + } + + pub fn is_empty(&self) -> bool { + self.start == self.end + } + + pub fn length(&self) -> usize { + self.end - self.start + } +} diff --git a/azalea-brigadier/src/context/suggestion_context.rs b/azalea-brigadier/src/context/suggestion_context.rs index e69de29b..540a5f23 100644 --- a/azalea-brigadier/src/context/suggestion_context.rs +++ b/azalea-brigadier/src/context/suggestion_context.rs @@ -0,0 +1,6 @@ +use crate::tree::command_node::CommandNode; + +pub struct SuggestionContext { + parent: dyn CommandNode, + start_pos: usize, +} diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs index d0966de3..b2345abb 100644 --- a/azalea-brigadier/src/lib.rs +++ b/azalea-brigadier/src/lib.rs @@ -1,3 +1,6 @@ +#[macro_use] +extern crate lazy_static; + mod ambiguity_consumer; mod arguments; mod builder; diff --git a/azalea-brigadier/src/redirect_modifier.rs b/azalea-brigadier/src/redirect_modifier.rs index e69de29b..cfefd120 100644 --- a/azalea-brigadier/src/redirect_modifier.rs +++ b/azalea-brigadier/src/redirect_modifier.rs @@ -0,0 +1,8 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +pub trait RedirectModifier { + fn apply(&self, context: CommandContext) -> Result, CommandSyntaxException>; +} diff --git a/azalea-brigadier/src/single_redirect_modifier.rs b/azalea-brigadier/src/single_redirect_modifier.rs index e69de29b..dd63244d 100644 --- a/azalea-brigadier/src/single_redirect_modifier.rs +++ b/azalea-brigadier/src/single_redirect_modifier.rs @@ -0,0 +1,8 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +pub trait SingleRedirectModifier { + fn apply(&self, context: CommandContext) -> Result; +} diff --git a/azalea-brigadier/src/suggestion/suggestion_provider.rs b/azalea-brigadier/src/suggestion/suggestion_provider.rs index e69de29b..3027d460 100644 --- a/azalea-brigadier/src/suggestion/suggestion_provider.rs +++ b/azalea-brigadier/src/suggestion/suggestion_provider.rs @@ -0,0 +1,14 @@ +use crate::{ + context::command_context::CommandContext, + exceptions::command_syntax_exception::CommandSyntaxException, +}; + +use super::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}; + +pub trait SuggestionProvider { + fn suggestions( + &self, + context: &CommandContext, + builder: &SuggestionsBuilder, + ) -> Result; +} diff --git a/azalea-brigadier/src/tree/argument_command_node.rs b/azalea-brigadier/src/tree/argument_command_node.rs index e69de29b..df7d3f5c 100644 --- a/azalea-brigadier/src/tree/argument_command_node.rs +++ b/azalea-brigadier/src/tree/argument_command_node.rs @@ -0,0 +1,118 @@ +use std::fmt::{Display, Formatter}; + +use crate::{ + arguments::argument_type::ArgumentType, + context::{ + command_context::CommandContext, command_context_builder::CommandContextBuilder, + parsed_argument::ParsedArgument, + }, + exceptions::command_syntax_exception::CommandSyntaxException, + string_reader::StringReader, + suggestion::{ + suggestion_provider::SuggestionProvider, suggestions::Suggestions, + suggestions_builder::SuggestionsBuilder, + }, +}; + +use super::command_node::{BaseCommandNode, CommandNode}; + +const USAGE_ARGUMENT_OPEN: &str = "<"; +const USAGE_ARGUMENT_CLOSE: &str = ">"; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct ArgumentCommandNode { + name: String, + type_: dyn ArgumentType, + custom_suggestions: dyn SuggestionProvider, + // Since Rust doesn't have extending, we put the struct this is extending as the "base" field + pub base: BaseCommandNode, +} + +impl ArgumentCommandNode { + fn get_type(&self) -> &dyn ArgumentType { + &self.type_ + } + + fn custom_suggestions(&self) -> &dyn SuggestionProvider { + &self.custom_suggestions + } +} + +impl CommandNode for ArgumentCommandNode { + fn name(&self) -> &str { + &self.name + } + + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException> { + // final int start = reader.getCursor(); + // final T result = type.parse(reader); + // final ParsedArgument parsed = new ParsedArgument<>(start, reader.getCursor(), result); + + // contextBuilder.withArgument(name, parsed); + // contextBuilder.withNode(this, parsed.getRange()); + + let start = reader.get_cursor(); + let result = self.get_type().parse(reader)?; + let parsed = ParsedArgument::new(start, reader.get_cursor(), result); + + context_builder.with_argument(&self.name, parsed); + context_builder.with_node(self, parsed.get_range()); + + Ok(()) + } + + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result { + if self.custom_suggestions.is_none() { + self.get_type().list_suggestions(context, builder) + } else { + self.custom_suggestions.get_suggestions(context, builder) + } + } + + fn is_valid_input(&self, input: &str) -> bool { + let reader = StringReader::new(input); + let result = self.get_type().parse(reader); + if result.is_ok() { + return !reader.can_read() || reader.peek() == ' '; + } else { + return false; + } + } + + fn usage_text(&self) -> &str { + USAGE_ARGUMENT_OPEN + self.name + USAGE_ARGUMENT_CLOSE + } + + fn create_builder(&self) -> RequiredArgumentBuilder { + let builder = RequiredArgumentBuilder::argument(&self.name, &self.type_); + builder.requires(self.base.get_requirement()); + builder.forward( + self.base.get_redirect(), + self.base.get_redirect_modifier(), + self.base.is_fork(), + ); + builder.suggests(self.custom_suggestions()); + if self.base.get_command() != None { + builder.executes(self.base.get_command().unwrap()); + } + builder + } + + fn get_examples(&self) -> Vec { + self.type_.get_examples() + } +} + +impl Display for ArgumentCommandNode { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "", self.name, self.type_) + } +} diff --git a/azalea-brigadier/src/tree/command_node.rs b/azalea-brigadier/src/tree/command_node.rs index e69de29b..286820b9 100644 --- a/azalea-brigadier/src/tree/command_node.rs +++ b/azalea-brigadier/src/tree/command_node.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; + +use crate::{ + builder::argument_builder::ArgumentBuilder, + command::Command, + context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, + exceptions::command_syntax_exception::CommandSyntaxException, + redirect_modifier::RedirectModifier, + string_reader::StringReader, + suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, +}; + +use super::{argument_command_node::ArgumentCommandNode, literal_command_node::LiteralCommandNode}; + +pub struct BaseCommandNode { + // private final Map> children = new LinkedHashMap<>(); + // private final Map> literals = new LinkedHashMap<>(); + // private final Map> arguments = new LinkedHashMap<>(); + // private final Predicate requirement; + // private final CommandNode redirect; + // private final RedirectModifier modifier; + // private final boolean forks; + // private Command command; + children: HashMap>, + literals: HashMap>, + arguments: HashMap>, + requirement: Option bool>, + redirect: Option>, + modifier: Option>, + forks: bool, + command: Option>, +} + +impl BaseCommandNode {} + +pub trait CommandNode { + fn name(&self) -> &str; + fn usage_text(&self) -> &str; + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException>; + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result; + fn is_valid_input(&self, input: &str) -> bool; + fn create_builder(&self) -> dyn ArgumentBuilder; + fn get_examples(&self) -> Vec; +} diff --git a/azalea-brigadier/src/tree/literal_command_node.rs b/azalea-brigadier/src/tree/literal_command_node.rs index e69de29b..bb0e613c 100644 --- a/azalea-brigadier/src/tree/literal_command_node.rs +++ b/azalea-brigadier/src/tree/literal_command_node.rs @@ -0,0 +1,96 @@ +use crate::{ + context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, + exceptions::{ + builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, + }, + string_reader::StringReader, + suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, +}; + +use super::command_node::{BaseCommandNode, CommandNode}; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct LiteralCommandNode { + 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, +} + +impl LiteralCommandNode { + pub fn literal(&self) -> &String { + &self.literal + } + + pub fn parse(&self, reader: StringReader) -> i32 { + let start = reader.get_cursor(); + if reader.can_read(self.literal.len()) { + let end = start + self.literal.len(); + if reader.get_string()[start..end].eq(&self.literal) { + reader.set_cursor(end); + if !reader.can_read() || reader.peek() == ' ' { + return end as i32; + } else { + reader.set_cursor(start); + } + } + } + -1 + } +} + +impl CommandNode for LiteralCommandNode { + fn name(&self) -> &str { + &self.literal + } + + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException> { + let start = reader.get_cursor(); + let end = self.parse(reader); + if end > -1 { + return Ok(()); + } + + Err(BuiltInExceptions::LiteralIncorrect { + expected: self.literal(), + } + .create_with_context(reader)) + } + + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result { + if self + .literal_lowercase + .starts_with(&builder.remaining_lowercase()) + { + builder.suggest(self.literal()) + } else { + Suggestions::empty() + } + } + + fn is_valid_input(&self, input: &str) -> bool { + self.parse(StringReader::from(input)) > -1 + } + + fn usage_text(&self) -> &str { + self.literal + } + + fn create_builder(&self) -> LiteralArgumentBuilder { + let builder = LiteralArgumentBuilder::literal(self.literal()); + builder.requires(self.requirement()); + builder.forward(self.redirect(), self.redirect_modifier(), self.is_fork()); + if self.command().is_some() { + builder.executes(self.command().unwrap()); + } + builder + } +} diff --git a/azalea-brigadier/src/tree/mod.rs b/azalea-brigadier/src/tree/mod.rs index e69de29b..3dc22583 100644 --- a/azalea-brigadier/src/tree/mod.rs +++ b/azalea-brigadier/src/tree/mod.rs @@ -0,0 +1,4 @@ +pub mod argument_command_node; +pub mod command_node; +pub mod literal_command_node; +pub mod root_command_node; diff --git a/azalea-brigadier/src/tree/root_command_node.rs b/azalea-brigadier/src/tree/root_command_node.rs index e69de29b..004ab6a8 100644 --- a/azalea-brigadier/src/tree/root_command_node.rs +++ b/azalea-brigadier/src/tree/root_command_node.rs @@ -0,0 +1,61 @@ +use std::fmt::{Display, Formatter}; + +use crate::{ + context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, + exceptions::{ + builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, + }, + string_reader::StringReader, + suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, +}; + +use super::command_node::{BaseCommandNode, CommandNode}; + +#[derive(Hash, PartialEq, Eq, Debug, Clone)] +pub struct RootCommandNode { + // Since Rust doesn't have extending, we put the struct this is extending as the "base" field + pub base: BaseCommandNode, +} + +impl CommandNode for RootCommandNode { + fn name(&self) -> &str { + "" + } + + fn parse( + &self, + reader: StringReader, + context_builder: CommandContextBuilder, + ) -> Result<(), CommandSyntaxException> { + } + + fn list_suggestions( + &self, + context: CommandContext, + builder: SuggestionsBuilder, + ) -> Result { + Suggestions::empty() + } + + fn is_valid_input(&self, input: &str) -> bool { + false + } + + fn usage_text(&self) -> &str { + "" + } + + fn create_builder(&self) -> () { + panic!("Cannot convert root into a builder"); + } + + fn get_examples(&self) -> Vec { + vec![] + } +} + +impl Display for RootCommandNode<()> { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "") + } +} -- cgit v1.2.3 From 270507736af57aae6801dc9eb3c3132139d0d07b Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Wed, 12 Jan 2022 00:40:43 +0000 Subject: a --- Cargo.lock | 28 +++++++ azalea-brigadier/Cargo.toml | 1 + azalea-brigadier/README.md | 1 + azalea-brigadier/src/arguments/argument_type.rs | 28 +++++-- .../src/arguments/bool_argument_type.rs | 1 + azalea-brigadier/src/builder/argument_builder.rs | 8 +- .../src/builder/literal_argument_builder.rs | 15 +++- .../src/builder/required_argument_builder.rs | 11 ++- azalea-brigadier/src/command.rs | 4 +- azalea-brigadier/src/command_dispatcher.rs | 17 ++++- .../src/context/command_context_builder.rs | 16 +++- .../src/context/parsed_command_node.rs | 9 +++ azalea-brigadier/src/redirect_modifier.rs | 5 +- .../src/suggestion/integer_suggestion.rs | 1 + azalea-brigadier/src/suggestion/suggestions.rs | 86 +++++++++++++++++++++- .../src/suggestion/suggestions_builder.rs | 4 +- azalea-brigadier/src/tree/argument_command_node.rs | 17 ++++- azalea-brigadier/src/tree/command_node.rs | 59 +++++++++++++-- azalea-brigadier/src/tree/literal_command_node.rs | 20 ++++- azalea-brigadier/src/tree/root_command_node.rs | 19 ++++- 20 files changed, 307 insertions(+), 43 deletions(-) (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index 0eeb6520..a0ca81fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,6 +71,7 @@ dependencies = [ name = "azalea-brigadier" version = "0.1.0" dependencies = [ + "dyn-clonable", "lazy_static", ] @@ -336,6 +337,33 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" +[[package]] +name = "dyn-clonable" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e9232f0e607a262ceb9bd5141a3dfb3e4db6994b31989bbfd845878cba59fd4" +dependencies = [ + "dyn-clonable-impl", + "dyn-clone", +] + +[[package]] +name = "dyn-clonable-impl" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "558e40ea573c374cf53507fd240b7ee2f5477df7cfebdb97323ec61c719399c5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "dyn-clone" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee2626afccd7561a06cf1367e2950c4718ea04565e20fb5029b6c7d8ad09abcf" + [[package]] name = "either" version = "1.6.1" diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml index 3694a4b7..4e8968d7 100644 --- a/azalea-brigadier/Cargo.toml +++ b/azalea-brigadier/Cargo.toml @@ -7,3 +7,4 @@ version = "0.1.0" [dependencies] lazy_static = "^1.4" +dyn-clonable = "^0.9" diff --git a/azalea-brigadier/README.md b/azalea-brigadier/README.md index 92c0d27e..df69b5c0 100644 --- a/azalea-brigadier/README.md +++ b/azalea-brigadier/README.md @@ -1,3 +1,4 @@ # Azalea Brigadier A Rustier port of Mojang's [Brigadier](https://github.com/Mojang/brigadier) command parsing and dispatching library. + diff --git a/azalea-brigadier/src/arguments/argument_type.rs b/azalea-brigadier/src/arguments/argument_type.rs index 46026735..890cdea0 100644 --- a/azalea-brigadier/src/arguments/argument_type.rs +++ b/azalea-brigadier/src/arguments/argument_type.rs @@ -5,11 +5,19 @@ use crate::{ string_reader::StringReader, suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, }; +use dyn_clonable::*; -pub trait Types { +#[clonable] +// This should be applied to an Enum +pub trait Types: Clone { fn bool(value: bool) -> Self where Self: Sized; + + /// Get the less specific ArgumentType from this enum + fn inner(&self) -> Box> + where + Self: Sized; } /* @@ -25,12 +33,21 @@ enum BrigadierTypes { Entity(EntityArgumentType) } + +impl Types for BrigadierTypes { + fn inner(&self) -> dyn ArgumentType { + match self { + Bool(t) => t, + Entity(t) => t + } + } +} */ -pub trait ArgumentType +#[clonable] +pub trait ArgumentType: Clone where - Self: Sized, - T: Types + ?Sized, + T: Types, { // T parse(StringReader reader) throws CommandSyntaxException; @@ -42,7 +59,7 @@ where // return Collections.emptyList(); // } - fn parse(&self, reader: &mut StringReader) -> Result; + fn parse(&self, reader: &mut StringReader) -> Result, CommandSyntaxException>; fn list_suggestions( &self, @@ -50,6 +67,7 @@ where builder: &mut SuggestionsBuilder, ) -> Result where + Self: Sized, S: Sized, T: Sized; diff --git a/azalea-brigadier/src/arguments/bool_argument_type.rs b/azalea-brigadier/src/arguments/bool_argument_type.rs index 1237caa0..b04488c1 100644 --- a/azalea-brigadier/src/arguments/bool_argument_type.rs +++ b/azalea-brigadier/src/arguments/bool_argument_type.rs @@ -7,6 +7,7 @@ use crate::{ use super::argument_type::{ArgumentType, Types}; +#[derive(Clone)] pub struct BoolArgumentType {} impl ArgumentType for BoolArgumentType diff --git a/azalea-brigadier/src/builder/argument_builder.rs b/azalea-brigadier/src/builder/argument_builder.rs index bd2a2c15..0360b05a 100644 --- a/azalea-brigadier/src/builder/argument_builder.rs +++ b/azalea-brigadier/src/builder/argument_builder.rs @@ -1,4 +1,5 @@ use crate::{ + arguments::argument_type::{ArgumentType, Types}, command::Command, redirect_modifier::RedirectModifier, single_redirect_modifier::SingleRedirectModifier, @@ -8,7 +9,7 @@ use crate::{ pub struct BaseArgumentBuilder<'a, S, T> where S: Sized, - T: Sized, + T: Sized + ArgumentType, { arguments: RootCommandNode<'a, S, T>, command: Option<&'a dyn Command>, @@ -22,7 +23,10 @@ pub trait ArgumentBuilder { fn build(self) -> dyn CommandNode; } -impl BaseArgumentBuilder<'_, S, T> { +impl BaseArgumentBuilder<'_, S, T> +where + T: ArgumentType, +{ pub fn then(&mut self, command: dyn CommandNode) -> Result<&mut T, String> { if self.target.is_some() { return Err("Cannot add children to a redirected node".to_string()); diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs index cf9f1ee9..a4cb3f84 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -1,14 +1,23 @@ -use crate::tree::literal_command_node::LiteralCommandNode; +use crate::{ + arguments::argument_type::{ArgumentType, Types}, + tree::literal_command_node::LiteralCommandNode, +}; use super::argument_builder::BaseArgumentBuilder; -pub struct LiteralArgumentBuilder<'a, S, T> { +pub struct LiteralArgumentBuilder<'a, S, T> +where + T: ArgumentType, +{ literal: String, pub base: BaseArgumentBuilder<'a, S, T>, } -impl<'a, S, T> LiteralArgumentBuilder<'a, S, T> { +impl<'a, S, T> LiteralArgumentBuilder<'a, S, T> +where + T: ArgumentType, +{ pub fn new(literal: String) -> Self { Self { literal, diff --git a/azalea-brigadier/src/builder/required_argument_builder.rs b/azalea-brigadier/src/builder/required_argument_builder.rs index 6f6fa8eb..b5f99828 100644 --- a/azalea-brigadier/src/builder/required_argument_builder.rs +++ b/azalea-brigadier/src/builder/required_argument_builder.rs @@ -1,11 +1,15 @@ use crate::{ + arguments::argument_type::{ArgumentType, Types}, suggestion::suggestion_provider::SuggestionProvider, tree::{argument_command_node::ArgumentCommandNode, command_node::BaseCommandNode}, }; use super::argument_builder::BaseArgumentBuilder; -pub struct RequiredArgumentBuilder<'a, S, T> { +pub struct RequiredArgumentBuilder<'a, S, T> +where + T: ArgumentType, +{ // private final String name; // private final ArgumentType type; // private SuggestionProvider suggestionsProvider = null; @@ -16,7 +20,10 @@ pub struct RequiredArgumentBuilder<'a, S, T> { pub base: BaseArgumentBuilder<'a, S, T>, } -impl<'a, S, T> RequiredArgumentBuilder<'a, S, T> { +impl<'a, S, T> RequiredArgumentBuilder<'a, S, T> +where + T: ArgumentType, +{ pub fn new(name: String, type_: T) -> Self { Self { name, diff --git a/azalea-brigadier/src/command.rs b/azalea-brigadier/src/command.rs index 520c8a52..dcbf3ffd 100644 --- a/azalea-brigadier/src/command.rs +++ b/azalea-brigadier/src/command.rs @@ -2,9 +2,11 @@ use crate::{ context::command_context::CommandContext, exceptions::command_syntax_exception::CommandSyntaxException, }; +use dyn_clonable::*; pub const SINGLE_SUCCESS: i32 = 1; -pub trait Command { +#[clonable] +pub trait Command: Clone { fn run(&self, context: &mut CommandContext) -> Result; } diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index d0351547..f2fc7528 100644 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -1,13 +1,22 @@ -use crate::tree::root_command_node::RootCommandNode; +use crate::{ + arguments::argument_type::{ArgumentType, Types}, + tree::root_command_node::RootCommandNode, +}; /// The core command dispatcher, for registering, parsing, and executing commands. /// The `S` generic is a custom "source" type, such as a user or originator of a command -#[derive(Default)] -pub struct CommandDispatcher<'a, S, T> { +#[derive(Default, Clone)] +pub struct CommandDispatcher<'a, S, T> +where + T: ArgumentType, +{ root: RootCommandNode<'a, S, T>, } -impl CommandDispatcher<'_, S, T> { +impl CommandDispatcher<'_, S, T> +where + T: ArgumentType, +{ /// The string required to separate individual arguments in an input string /// /// See: [`ARGUMENT_SEPARATOR_CHAR`] diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs index 88e26343..639a97ee 100644 --- a/azalea-brigadier/src/context/command_context_builder.rs +++ b/azalea-brigadier/src/context/command_context_builder.rs @@ -1,8 +1,10 @@ use std::collections::HashMap; use crate::{ - arguments::argument_type::ArgumentType, command::Command, - command_dispatcher::CommandDispatcher, redirect_modifier::RedirectModifier, + arguments::argument_type::{ArgumentType, Types}, + command::Command, + command_dispatcher::CommandDispatcher, + redirect_modifier::RedirectModifier, tree::command_node::CommandNode, }; @@ -25,7 +27,10 @@ use super::{ // private boolean forks; #[derive(Clone)] -pub struct CommandContextBuilder<'a, S, T> { +pub struct CommandContextBuilder<'a, S, T> +where + T: ArgumentType, +{ arguments: HashMap>, root_node: &'a dyn CommandNode, nodes: Vec>, @@ -45,7 +50,10 @@ pub struct CommandContextBuilder<'a, S, T> { // this.range = StringRange.at(start); // } -impl CommandContextBuilder<'_, S, T> { +impl CommandContextBuilder<'_, S, T> +where + T: ArgumentType, +{ pub fn new( dispatcher: CommandDispatcher, source: S, diff --git a/azalea-brigadier/src/context/parsed_command_node.rs b/azalea-brigadier/src/context/parsed_command_node.rs index 14168a06..c0be355c 100644 --- a/azalea-brigadier/src/context/parsed_command_node.rs +++ b/azalea-brigadier/src/context/parsed_command_node.rs @@ -19,3 +19,12 @@ impl ParsedCommandNode { &self.range } } + +impl Clone for ParsedCommandNode { + fn clone_from(&mut self, source: &Self) { + Self { + node: self.node.clone(), + range: self.range.clone(), + } + } +} diff --git a/azalea-brigadier/src/redirect_modifier.rs b/azalea-brigadier/src/redirect_modifier.rs index fd2e1bf7..fdb0a080 100644 --- a/azalea-brigadier/src/redirect_modifier.rs +++ b/azalea-brigadier/src/redirect_modifier.rs @@ -1,8 +1,11 @@ +use dyn_clonable::*; + use crate::{ context::command_context::CommandContext, exceptions::command_syntax_exception::CommandSyntaxException, }; -pub trait RedirectModifier { +#[clonable] +pub trait RedirectModifier: Clone { fn apply(&self, context: CommandContext) -> Result, CommandSyntaxException>; } diff --git a/azalea-brigadier/src/suggestion/integer_suggestion.rs b/azalea-brigadier/src/suggestion/integer_suggestion.rs index e69de29b..acee2329 100644 --- a/azalea-brigadier/src/suggestion/integer_suggestion.rs +++ b/azalea-brigadier/src/suggestion/integer_suggestion.rs @@ -0,0 +1 @@ +pub struct IntegerSuggestion {} diff --git a/azalea-brigadier/src/suggestion/suggestions.rs b/azalea-brigadier/src/suggestion/suggestions.rs index 18572d20..778c5de8 100644 --- a/azalea-brigadier/src/suggestion/suggestions.rs +++ b/azalea-brigadier/src/suggestion/suggestions.rs @@ -1,8 +1,90 @@ -use std::cmp; +use std::{cmp, collections::HashSet}; use crate::{context::string_range::StringRange, message::Message}; -pub struct Suggestions {} +use super::suggestion::Suggestion; + +#[derive(PartialEq, Eq, Hash, Default)] +pub struct Suggestions { + range: StringRange, + suggestions: Vec, +} + +impl Suggestions { + fn range(&self) -> &StringRange { + &self.range + } + + fn list(&self) -> &Vec { + &self.suggestions + } + + fn is_empty(&self) -> bool { + self.suggestions.is_empty() + } + + fn merge(command: &str, input: &Vec) { + if input.is_empty() { + return Self::default(); + } else if input.len() == 1 { + return input.iter().next(); + } + let texts = HashSet::new(); + for suggestions in input { + texts.extend(suggestions.list()) + } + Self::new(command, texts) + } + + // public static Suggestions create(final String command, final Collection suggestions) { + // if (suggestions.isEmpty()) { + // return EMPTY; + // } + // int start = Integer.MAX_VALUE; + // int end = Integer.MIN_VALUE; + // for (final Suggestion suggestion : suggestions) { + // start = Math.min(suggestion.getRange().getStart(), start); + // end = Math.max(suggestion.getRange().getEnd(), end); + // } + // final StringRange range = new StringRange(start, end); + // final Set texts = new HashSet<>(); + // for (final Suggestion suggestion : suggestions) { + // texts.add(suggestion.expand(command, range)); + // } + // final List sorted = new ArrayList<>(texts); + // sorted.sort((a, b) -> a.compareToIgnoreCase(b)); + // return new Suggestions(range, sorted); + pub fn new(command: String, suggestions: Vec) -> Self { + if suggestions.is_empty() { + return Self::default(); + } + let mut start = usize::MAX; + let mut end = usize::MIN; + for suggestion in suggestions { + let start = cmp::min(suggestion.range().start(), start); + let end = cmp::max(suggestion.range().end(), end); + } + let range = StringRange::new(start, end); + let texts = HashSet::new(); + for suggestion in suggestions { + texts.insert(suggestion.expand(command, range)); + } + let sorted = texts.sort_by(|a, b| a.compare_ignore_case(b)); + Suggestions { + range, + suggestions: sorted, + } + } +} + +impl Default for Suggestions { + fn default() -> Self { + Self { + range: StringRange::at(0), + suggestions: vec![], + } + } +} // #[cfg(test)] // mod tests { diff --git a/azalea-brigadier/src/suggestion/suggestions_builder.rs b/azalea-brigadier/src/suggestion/suggestions_builder.rs index 7853a3a2..bc8f6f5d 100644 --- a/azalea-brigadier/src/suggestion/suggestions_builder.rs +++ b/azalea-brigadier/src/suggestion/suggestions_builder.rs @@ -1,6 +1,8 @@ use crate::context::string_range::StringRange; -use super::{suggestion::Suggestion, suggestions::Suggestions}; +use super::{ + integer_suggestion::IntegerSuggestion, suggestion::Suggestion, suggestions::Suggestions, +}; pub struct SuggestionsBuilder { input: String, 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 ArgumentCommandNode<'_, S, T> { +impl ArgumentCommandNode<'_, S, T> +where + T: ArgumentType, +{ fn get_type(&self) -> &T { &self.type_ } @@ -44,7 +48,11 @@ impl ArgumentCommandNode<'_, S, T> { } } -impl<'a, S, T> CommandNode for ArgumentCommandNode<'a, S, T> { +impl<'a, S, T> CommandNode for ArgumentCommandNode<'a, S, T> +where + T: ArgumentType + Clone, + S: Clone, +{ fn name(&self) -> &str { &self.name } @@ -117,7 +125,10 @@ impl<'a, S, T> CommandNode for ArgumentCommandNode<'a, S, T> { } } -impl Display for ArgumentCommandNode<'_, (), (), ()> { +impl Display for ArgumentCommandNode<'_, S, T> +where + T: ArgumentType, +{ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "", 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, +{ children: HashMap>, literals: HashMap>, arguments: HashMap>, @@ -24,9 +27,49 @@ pub struct BaseCommandNode<'a, S, T> { command: Option<&'a dyn Command>, } -impl BaseCommandNode<'_, S, T> {} +impl BaseCommandNode<'_, S, T> where T: ArgumentType {} + +impl Clone for BaseCommandNode<'_, S, T> +where + T: ArgumentType, +{ + 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 Debug for BaseCommandNode<'_, S, T> +where + T: ArgumentType, +{ + 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 { +#[clonable] +pub trait CommandNode: Clone +where + T: ArgumentType, +{ 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, +{ 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, +{ pub fn new(literal: String, base: BaseCommandNode) -> Self { let literal_lowercase = literal.to_lowercase(); Self { @@ -51,7 +59,11 @@ impl<'a, S, T> LiteralCommandNode<'a, S, T> { } } -impl CommandNode for LiteralCommandNode<'_, S, T> { +impl CommandNode for LiteralCommandNode<'_, S, T> +where + T: ArgumentType + 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, +{ // Since Rust doesn't have extending, we put the struct this is extending as the "base" field pub base: BaseCommandNode<'a, S, T>, } -impl CommandNode for RootCommandNode<'_, S, T> { +impl CommandNode for RootCommandNode<'_, S, T> +where + T: ArgumentType + Clone, + S: Clone, +{ fn name(&self) -> &str { "" } @@ -53,7 +63,10 @@ impl CommandNode for RootCommandNode<'_, S, T> { } } -impl Display for RootCommandNode<'_, S, T> { +impl Display for RootCommandNode<'_, S, T> +where + T: ArgumentType, +{ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "") } -- cgit v1.2.3 From 4ff67d4917ce333232189e86aee09f2d82451fc6 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 3 Feb 2022 02:16:24 +0000 Subject: a --- Cargo.lock | 19 ++++++++ azalea-brigadier/Cargo.toml | 1 + .../src/builder/literal_argument_builder.rs | 17 ++----- azalea-brigadier/src/command_dispatcher.rs | 6 +-- azalea-brigadier/src/lib.rs | 3 ++ azalea-brigadier/src/tree/argument_command_node.rs | 24 ++++------ azalea-brigadier/src/tree/command_node.rs | 56 +++++++--------------- azalea-brigadier/src/tree/literal_command_node.rs | 30 ++++++++---- azalea-brigadier/src/tree/root_command_node.rs | 19 ++++++-- 9 files changed, 91 insertions(+), 84 deletions(-) (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index a0ca81fb..d52792ce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,6 +72,7 @@ name = "azalea-brigadier" version = "0.1.0" dependencies = [ "dyn-clonable", + "enum_dispatch", "lazy_static", ] @@ -382,6 +383,18 @@ dependencies = [ "syn", ] +[[package]] +name = "enum_dispatch" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "flate2" version = "1.0.22" @@ -717,6 +730,12 @@ dependencies = [ "libc", ] +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + [[package]] name = "oorandom" version = "11.1.3" diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml index 4e8968d7..6dead502 100644 --- a/azalea-brigadier/Cargo.toml +++ b/azalea-brigadier/Cargo.toml @@ -8,3 +8,4 @@ version = "0.1.0" [dependencies] lazy_static = "^1.4" dyn-clonable = "^0.9" +enum_dispatch = "^0.3" diff --git a/azalea-brigadier/src/builder/literal_argument_builder.rs b/azalea-brigadier/src/builder/literal_argument_builder.rs index 8250d45d..4a95755c 100644 --- a/azalea-brigadier/src/builder/literal_argument_builder.rs +++ b/azalea-brigadier/src/builder/literal_argument_builder.rs @@ -10,11 +10,8 @@ use crate::{ }; use std::fmt::Debug; -pub struct LiteralArgumentBuilder<'a, S> -where - , -{ - arguments: RootCommandNode<'a, S>, +pub struct LiteralArgumentBuilder { + arguments: RootCommandNode, command: Option>>, requirement: Box bool>, target: Option>>, @@ -23,10 +20,7 @@ where literal: String, } -impl<'a, S> LiteralArgumentBuilder<'a, S> -where - , -{ +impl LiteralArgumentBuilder { pub fn new(literal: String) -> Self { Self { literal, @@ -44,10 +38,7 @@ where } } -impl<'a, S> ArgumentBuilder for LiteralArgumentBuilder<'a, S> -where - , -{ +impl ArgumentBuilder for LiteralArgumentBuilder { fn build(self) -> Box> { let result = LiteralCommandNode::new(self.literal, self.base.build()); diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index 98288a48..f8ffddff 100644 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -4,11 +4,11 @@ use std::fmt::Debug; /// The core command dispatcher, for registering, parsing, and executing commands. /// The `S` generic is a custom "source" type, such as a user or originator of a command #[derive(Default, Clone)] -pub struct CommandDispatcher<'a, S> { - root: RootCommandNode<'a, S>, +pub struct CommandDispatcher { + root: RootCommandNode, } -impl CommandDispatcher<'_, S> { +impl CommandDispatcher { /// The string required to separate individual arguments in an input string /// /// See: [`ARGUMENT_SEPARATOR_CHAR`] diff --git a/azalea-brigadier/src/lib.rs b/azalea-brigadier/src/lib.rs index b2345abb..a1ac267c 100644 --- a/azalea-brigadier/src/lib.rs +++ b/azalea-brigadier/src/lib.rs @@ -1,6 +1,9 @@ #[macro_use] extern crate lazy_static; +#[macro_use] +extern crate enum_dispatch; + mod ambiguity_consumer; mod arguments; mod builder; diff --git a/azalea-brigadier/src/tree/argument_command_node.rs b/azalea-brigadier/src/tree/argument_command_node.rs index 0997ec17..9d2af14e 100644 --- a/azalea-brigadier/src/tree/argument_command_node.rs +++ b/azalea-brigadier/src/tree/argument_command_node.rs @@ -31,18 +31,14 @@ use super::{ const USAGE_ARGUMENT_OPEN: &str = "<"; const USAGE_ARGUMENT_CLOSE: &str = ">"; -#[derive(Clone, Debug)] -pub struct ArgumentCommandNode<'a, S> { +pub struct ArgumentCommandNode { name: String, type_: Box>, - custom_suggestions: Option<&'a dyn SuggestionProvider>, - // custom_suggestions: &'a dyn SuggestionProvider, - // Since Rust doesn't have extending, we put the struct this is extending as the "base" field - pub base: BaseCommandNode<'a, S>, + custom_suggestions: Option>>, children: HashMap>>, - literals: HashMap>, - arguments: HashMap>, + literals: HashMap>, + arguments: HashMap>, pub requirement: Box bool>, redirect: Option>>, modifier: Option>>, @@ -50,17 +46,17 @@ pub struct ArgumentCommandNode<'a, S> { pub command: Option>>, } -impl ArgumentCommandNode<'_, S> { +impl ArgumentCommandNode { fn get_type(&self) -> &dyn ArgumentType { - self.type_ + &*self.type_ } - fn custom_suggestions(&self) -> Option<&dyn SuggestionProvider> { - self.custom_suggestions + fn custom_suggestions(&self) -> &Option>> { + &self.custom_suggestions } } -impl<'a, S> CommandNodeTrait for ArgumentCommandNode<'a, S> { +impl CommandNodeTrait for ArgumentCommandNode { fn name(&self) -> &str { &self.name } @@ -173,7 +169,7 @@ impl<'a, S> CommandNodeTrait for ArgumentCommandNode<'a, S> { } } -impl Display for ArgumentCommandNode<'_, S> { +impl Display for ArgumentCommandNode { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "", self.name, self.type_) } 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>; } diff --git a/azalea-brigadier/src/tree/literal_command_node.rs b/azalea-brigadier/src/tree/literal_command_node.rs index 253b6dc5..2db31d97 100644 --- a/azalea-brigadier/src/tree/literal_command_node.rs +++ b/azalea-brigadier/src/tree/literal_command_node.rs @@ -3,33 +3,45 @@ use crate::{ builder::{ argument_builder::ArgumentBuilder, literal_argument_builder::LiteralArgumentBuilder, }, + command::Command, context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, exceptions::{ builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, }, immutable_string_reader::ImmutableStringReader, + redirect_modifier::RedirectModifier, string_reader::StringReader, suggestion::{suggestions::Suggestions, suggestions_builder::SuggestionsBuilder}, }; -use std::fmt::Debug; +use std::{collections::HashMap, fmt::Debug}; -use super::command_node::{BaseCommandNode, CommandNodeTrait}; +use super::{ + argument_command_node::ArgumentCommandNode, + command_node::{BaseCommandNode, CommandNodeTrait}, +}; #[derive(Debug, Clone)] -pub struct LiteralCommandNode<'a, S> { +pub struct LiteralCommandNode { 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>, + + children: HashMap>>, + literals: HashMap>, + arguments: HashMap>, + pub requirement: Box bool>, + redirect: Option>>, + modifier: Option>>, + forks: bool, + pub command: Option>>, } -impl<'a, S> LiteralCommandNode<'a, S> { - pub fn new(literal: String, base: BaseCommandNode<'a, S>) -> Self { +impl LiteralCommandNode { + pub fn new(literal: String) -> Self { let literal_lowercase = literal.to_lowercase(); Self { literal, literal_lowercase, - base, + ..Default::default() } } @@ -54,7 +66,7 @@ impl<'a, S> LiteralCommandNode<'a, S> { } } -impl CommandNodeTrait for LiteralCommandNode<'_, S> { +impl CommandNodeTrait for LiteralCommandNode { 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 0fcb8d97..6b8bc157 100644 --- a/azalea-brigadier/src/tree/root_command_node.rs +++ b/azalea-brigadier/src/tree/root_command_node.rs @@ -6,6 +6,7 @@ use super::{ use crate::{ arguments::argument_type::ArgumentType, builder::argument_builder::ArgumentBuilder, + command::Command, context::{command_context::CommandContext, command_context_builder::CommandContextBuilder}, exceptions::{ builtin_exceptions::BuiltInExceptions, command_syntax_exception::CommandSyntaxException, @@ -16,16 +17,24 @@ use crate::{ }; use std::{ any::Any, + collections::HashMap, fmt::{Debug, Display, Formatter}, }; -#[derive(Clone, Default, Debug)] -pub struct RootCommandNode<'a, S> { +#[derive(Default)] +pub struct RootCommandNode { // Since Rust doesn't have extending, we put the struct this is extending as the "base" field - pub base: BaseCommandNode<'a, S>, + children: HashMap>>, + literals: HashMap>, + arguments: HashMap>, + pub requirement: Box bool>, + redirect: Option>>, + modifier: Option>>, + forks: bool, + pub command: Option>>, } -impl CommandNodeTrait for RootCommandNode<'_, S> { +impl CommandNodeTrait for RootCommandNode { fn name(&self) -> &str { "" } @@ -63,7 +72,7 @@ impl CommandNodeTrait for RootCommandNode<'_, S> { } } -impl Display for RootCommandNode<'_, S> { +impl Display for RootCommandNode { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "") } -- cgit v1.2.3 From 82ed6baea5c4e0d00f5fc2bbeb45cbb2838a3784 Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 17 Apr 2022 14:03:21 -0500 Subject: Clean up some old stuff --- Cargo.lock | 50 -------------------------------------------- azalea-brigadier/Cargo.toml | 3 --- azalea-brigadier/src/main.rs | 38 --------------------------------- 3 files changed, 91 deletions(-) delete mode 100644 azalea-brigadier/src/main.rs (limited to 'Cargo.lock') diff --git a/Cargo.lock b/Cargo.lock index d52792ce..67259fef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,11 +70,6 @@ dependencies = [ [[package]] name = "azalea-brigadier" version = "0.1.0" -dependencies = [ - "dyn-clonable", - "enum_dispatch", - "lazy_static", -] [[package]] name = "azalea-chat" @@ -338,33 +333,6 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57" -[[package]] -name = "dyn-clonable" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e9232f0e607a262ceb9bd5141a3dfb3e4db6994b31989bbfd845878cba59fd4" -dependencies = [ - "dyn-clonable-impl", - "dyn-clone", -] - -[[package]] -name = "dyn-clonable-impl" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "558e40ea573c374cf53507fd240b7ee2f5477df7cfebdb97323ec61c719399c5" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "dyn-clone" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee2626afccd7561a06cf1367e2950c4718ea04565e20fb5029b6c7d8ad09abcf" - [[package]] name = "either" version = "1.6.1" @@ -383,18 +351,6 @@ dependencies = [ "syn", ] -[[package]] -name = "enum_dispatch" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd53b3fde38a39a06b2e66dc282f3e86191e53bd04cc499929c15742beae3df8" -dependencies = [ - "once_cell", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "flate2" version = "1.0.22" @@ -730,12 +686,6 @@ dependencies = [ "libc", ] -[[package]] -name = "once_cell" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" - [[package]] name = "oorandom" version = "11.1.3" diff --git a/azalea-brigadier/Cargo.toml b/azalea-brigadier/Cargo.toml index 6dead502..a7ebf618 100644 --- a/azalea-brigadier/Cargo.toml +++ b/azalea-brigadier/Cargo.toml @@ -6,6 +6,3 @@ version = "0.1.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lazy_static = "^1.4" -dyn-clonable = "^0.9" -enum_dispatch = "^0.3" diff --git a/azalea-brigadier/src/main.rs b/azalea-brigadier/src/main.rs deleted file mode 100644 index 53f2efa8..00000000 --- a/azalea-brigadier/src/main.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::rc::Rc; - -use rust_command_parser::{ - builder::{literal_argument_builder::literal, required_argument_builder::argument}, - dispatcher::CommandDispatcher, - parsers::integer, -}; - -struct CommandSourceStack { - player: String, -} - -pub fn main() { - let mut dispatcher = CommandDispatcher::>::new(); - - let source = Rc::new(CommandSourceStack { - player: "player".to_string(), - }); - - dispatcher.register( - literal("foo") - .then(argument("bar", integer()).executes(|c| { - // println!("Bar is {}", get_integer(c, "bar")); - 2 - })) - .executes(|c| { - println!("Called foo with no arguments"); - 1 - }), - ); - - let parse = dispatcher.parse("foo 123".to_string().into(), source); - println!("{:?}", parse); - println!( - "{}", - CommandDispatcher::>::execute(parse).unwrap() - ); -} -- cgit v1.2.3