aboutsummaryrefslogtreecommitdiff
path: root/azalea-brigadier/src/context/command_context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'azalea-brigadier/src/context/command_context.rs')
-rw-r--r--azalea-brigadier/src/context/command_context.rs72
1 files changed, 59 insertions, 13 deletions
diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs
index 202d6f21..224f2d63 100644
--- a/azalea-brigadier/src/context/command_context.rs
+++ b/azalea-brigadier/src/context/command_context.rs
@@ -10,16 +10,16 @@ use crate::{
/// A built `CommandContextBuilder`.
pub struct CommandContext<S> {
- pub source: Arc<S>,
- pub input: String,
- pub arguments: HashMap<String, ParsedArgument>,
- pub command: Command<S>,
- pub root_node: Arc<RwLock<CommandNode<S>>>,
- pub nodes: Vec<ParsedCommandNode<S>>,
- pub range: StringRange,
- pub child: Option<Rc<CommandContext<S>>>,
- pub modifier: Option<Arc<RedirectModifier<S>>>,
- pub forks: bool,
+ pub(super) source: Arc<S>,
+ pub(super) input: String,
+ pub(super) arguments: HashMap<String, ParsedArgument>,
+ pub(super) command: Command<S>,
+ pub(super) root_node: Arc<RwLock<CommandNode<S>>>,
+ pub(super) nodes: Vec<ParsedCommandNode<S>>,
+ pub(super) range: StringRange,
+ pub(super) child: Option<Rc<CommandContext<S>>>,
+ pub(super) modifier: Option<Arc<RedirectModifier<S>>>,
+ pub(super) forks: bool,
}
impl<S> Clone for CommandContext<S> {
@@ -59,8 +59,10 @@ impl<S> Debug for CommandContext<S> {
impl<S> CommandContext<S> {
pub fn copy_for(&self, source: Arc<S>) -> Self {
if Arc::ptr_eq(&source, &self.source) {
+ // fast path
return self.clone();
}
+
CommandContext {
source,
input: self.input.clone(),
@@ -75,12 +77,56 @@ impl<S> CommandContext<S> {
}
}
+ pub fn child(&self) -> Option<&CommandContext<S>> {
+ self.child.as_ref().map(|c| c.as_ref())
+ }
+
+ pub fn last_child(&self) -> &CommandContext<S> {
+ let mut result = self;
+ while let Some(child) = result.child() {
+ result = child;
+ }
+ result
+ }
+
+ pub fn command(&self) -> &Command<S> {
+ &self.command
+ }
+
+ pub fn source(&self) -> &Arc<S> {
+ &self.source
+ }
+
+ pub fn argument(&self, name: &str) -> Option<&dyn Any> {
+ let argument = self.arguments.get(name);
+ argument.map(|a| a.result.as_ref())
+ }
+
+ pub fn redirect_modifier(&self) -> Option<&RedirectModifier<S>> {
+ self.modifier.as_ref().map(|m| m.as_ref())
+ }
+
+ pub fn range(&self) -> &StringRange {
+ &self.range
+ }
+
+ pub fn input(&self) -> &str {
+ &self.input
+ }
+
+ pub fn root_node(&self) -> &Arc<RwLock<CommandNode<S>>> {
+ &self.root_node
+ }
+
+ pub fn nodes(&self) -> &[ParsedCommandNode<S>] {
+ &self.nodes
+ }
+
pub fn has_nodes(&self) -> bool {
!self.nodes.is_empty()
}
- pub fn argument(&self, name: &str) -> Option<Arc<dyn Any>> {
- let argument = self.arguments.get(name);
- argument.map(|a| a.result.clone())
+ pub fn is_forked(&self) -> bool {
+ self.forks
}
}