diff options
| author | mat <github@matdoes.dev> | 2022-04-17 20:46:43 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-04-17 20:46:43 -0500 |
| commit | d68233e0b1ff61d09ab2c4e22a42f3326054539b (patch) | |
| tree | d40d421eca3695fd594c1a0b1589af09c59e1f84 /azalea-brigadier/src/context.rs | |
| parent | af4b0d0add23188ab00e89a216cd3db62074cf7e (diff) | |
| download | azalea-drasl-d68233e0b1ff61d09ab2c4e22a42f3326054539b.tar.xz | |
simplify the <S> generic so it's not an Rc
Diffstat (limited to 'azalea-brigadier/src/context.rs')
| -rw-r--r-- | azalea-brigadier/src/context.rs | 73 |
1 files changed, 55 insertions, 18 deletions
diff --git a/azalea-brigadier/src/context.rs b/azalea-brigadier/src/context.rs index 5ffe2028..b798397b 100644 --- a/azalea-brigadier/src/context.rs +++ b/azalea-brigadier/src/context.rs @@ -1,25 +1,43 @@ use std::{any::Any, cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc}; use crate::{ - dispatcher::CommandDispatcher, modifier::RedirectModifier, string_range::StringRange, - tree::CommandNode, + dispatcher::CommandDispatcher, + modifier::RedirectModifier, + string_range::StringRange, + tree::{CommandNode, ParsedCommandNode}, }; -#[derive(Clone)] -pub struct CommandContextBuilder<S: Any + Clone> { +pub struct CommandContextBuilder<S> { pub arguments: HashMap<String, ParsedArgument>, pub root: Rc<RefCell<CommandNode<S>>>, - pub nodes: Vec<Rc<CommandNode<S>>>, + pub nodes: Vec<ParsedCommandNode<S>>, pub dispatcher: Rc<CommandDispatcher<S>>, pub source: Rc<S>, pub command: Option<Rc<dyn Fn(&CommandContext<S>) -> i32>>, pub child: Option<Rc<CommandContextBuilder<S>>>, pub range: StringRange, - pub modifier: Option<Rc<dyn RedirectModifier<S>>>, + pub modifier: Option<Rc<RedirectModifier<S>>>, pub forks: bool, } -impl<S: Any + Clone> CommandContextBuilder<S> { +impl<S> Clone for CommandContextBuilder<S> { + fn clone(&self) -> Self { + Self { + arguments: self.arguments.clone(), + root: self.root.clone(), + nodes: self.nodes.clone(), + dispatcher: self.dispatcher.clone(), + source: self.source.clone(), + command: self.command.clone(), + child: self.child.clone(), + range: self.range.clone(), + modifier: self.modifier.clone(), + forks: self.forks.clone(), + } + } +} + +impl<S> CommandContextBuilder<S> { // CommandDispatcher<S> dispatcher, final S source, final CommandNode<S> rootNode, final int start pub fn new( dispatcher: Rc<CommandDispatcher<S>>, @@ -58,11 +76,14 @@ impl<S: Any + Clone> CommandContextBuilder<S> { self.arguments.insert(name.to_string(), argument); self } - pub fn with_node(&mut self, node: Rc<CommandNode<S>>, range: StringRange) -> &Self { - self.nodes.push(node.clone()); + pub fn with_node(&mut self, node: Rc<RefCell<CommandNode<S>>>, range: StringRange) -> &Self { + self.nodes.push(ParsedCommandNode { + node: node.clone(), + range: range.clone(), + }); self.range = StringRange::encompassing(&self.range, &range); - self.modifier = node.modifier.clone(); - self.forks = node.forks; + self.modifier = node.borrow().modifier.clone(); + self.forks = node.borrow().forks; self } @@ -82,12 +103,12 @@ impl<S: Any + Clone> CommandContextBuilder<S> { } } -impl<S: Any + Clone> Debug for CommandContextBuilder<S> { +impl<S> Debug for CommandContextBuilder<S> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("CommandContextBuilder") // .field("arguments", &self.arguments) .field("root", &self.root) - .field("nodes", &self.nodes) + // .field("nodes", &self.nodes) // .field("dispatcher", &self.dispatcher) // .field("source", &self.source) // .field("command", &self.command) @@ -105,22 +126,38 @@ pub struct ParsedArgument { pub result: Rc<dyn Any>, } -#[derive(Clone)] /// A built `CommandContextBuilder`. -pub struct CommandContext<S: Any + Clone> { +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<Rc<CommandNode<S>>>, + pub nodes: Vec<ParsedCommandNode<S>>, pub range: StringRange, pub child: Option<Rc<CommandContext<S>>>, - pub modifier: Option<Rc<dyn RedirectModifier<S>>>, + pub modifier: Option<Rc<RedirectModifier<S>>>, pub forks: bool, } -impl<S: Any + Clone> CommandContext<S> { +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.clone(), + } + } +} + +impl<S> CommandContext<S> { pub fn copy_for(&self, source: Rc<S>) -> Self { if Rc::ptr_eq(&source, &self.source) { return self.clone(); |
