aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/context/command_context.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-18 18:14:25 +0000
committermat <github@matdoes.dev>2022-04-18 18:14:25 +0000
commit8d71fbf813391783531a9f7c70e75e105fabaf03 (patch)
treedd1d70370d3068c4aecd8aaf25174278dfbc1b29 /azalea-brigadier/src/context/command_context.rs
parentb3864af9c4af83552e37fd71a46262967572f9e6 (diff)
downloadazalea-drasl-8d71fbf813391783531a9f7c70e75e105fabaf03.tar.xz
change a BTreeMap to a HashMap
Diffstat (limited to 'azalea-brigadier/src/context/command_context.rs')
-rw-r--r--azalea-brigadier/src/context/command_context.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs
new file mode 100644
index 00000000..1834a73d
--- /dev/null
+++ b/azalea-brigadier/src/context/command_context.rs
@@ -0,0 +1,80 @@
+use super::{parsed_command_node::ParsedCommandNode, string_range::StringRange, ParsedArgument};
+use crate::{modifier::RedirectModifier, tree::CommandNode};
+use std::{any::Any, cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
+
+/// A built `CommandContextBuilder`.
+pub struct CommandContext<S> {
+ pub source: Rc<S>,
+ pub input: String,
+ pub arguments: HashMap<String, ParsedArgument>,
+ pub command: Option<Rc<dyn Fn(&CommandContext<S>) -> i32>>,
+ pub root_node: Rc<RefCell<CommandNode<S>>>,
+ pub nodes: Vec<ParsedCommandNode<S>>,
+ pub range: StringRange,
+ pub child: Option<Rc<CommandContext<S>>>,
+ pub modifier: Option<Rc<RedirectModifier<S>>>,
+ pub forks: bool,
+}
+
+impl<S> Clone for CommandContext<S> {
+ fn clone(&self) -> Self {
+ Self {
+ source: self.source.clone(),
+ input: self.input.clone(),
+ arguments: self.arguments.clone(),
+ command: self.command.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,
+ }
+ }
+}
+
+impl<S> Debug for CommandContext<S> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("CommandContext")
+ // .field("source", &self.source)
+ .field("input", &self.input)
+ // .field("arguments", &self.arguments)
+ // .field("command", &self.command)
+ // .field("root_node", &self.root_node)
+ // .field("nodes", &self.nodes)
+ .field("range", &self.range)
+ .field("child", &self.child)
+ // .field("modifier", &self.modifier)
+ .field("forks", &self.forks)
+ .finish()
+ }
+}
+
+impl<S> CommandContext<S> {
+ pub fn copy_for(&self, source: Rc<S>) -> Self {
+ if Rc::ptr_eq(&source, &self.source) {
+ return self.clone();
+ }
+ CommandContext {
+ source,
+ input: self.input.clone(),
+ arguments: self.arguments.clone(),
+ command: self.command.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,
+ }
+ }
+
+ pub fn has_nodes(&self) -> bool {
+ !self.nodes.is_empty()
+ }
+
+ pub fn argument(&self, name: &str) -> Option<Rc<dyn Any>> {
+ let argument = self.arguments.get(name);
+ argument.map(|a| a.result.clone())
+ }
+}