From d68233e0b1ff61d09ab2c4e22a42f3326054539b Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 17 Apr 2022 20:46:43 -0500 Subject: simplify the generic so it's not an Rc --- azalea-brigadier/src/context.rs | 73 +++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 18 deletions(-) (limited to 'azalea-brigadier/src/context.rs') 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 { +pub struct CommandContextBuilder { pub arguments: HashMap, pub root: Rc>>, - pub nodes: Vec>>, + pub nodes: Vec>, pub dispatcher: Rc>, pub source: Rc, pub command: Option) -> i32>>, pub child: Option>>, pub range: StringRange, - pub modifier: Option>>, + pub modifier: Option>>, pub forks: bool, } -impl CommandContextBuilder { +impl Clone for CommandContextBuilder { + 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 CommandContextBuilder { // CommandDispatcher dispatcher, final S source, final CommandNode rootNode, final int start pub fn new( dispatcher: Rc>, @@ -58,11 +76,14 @@ impl CommandContextBuilder { self.arguments.insert(name.to_string(), argument); self } - pub fn with_node(&mut self, node: Rc>, range: StringRange) -> &Self { - self.nodes.push(node.clone()); + pub fn with_node(&mut self, node: Rc>>, 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 CommandContextBuilder { } } -impl Debug for CommandContextBuilder { +impl Debug for CommandContextBuilder { 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, } -#[derive(Clone)] /// A built `CommandContextBuilder`. -pub struct CommandContext { +pub struct CommandContext { pub source: Rc, pub input: String, pub arguments: HashMap, pub command: Option) -> i32>>, pub root_node: Rc>>, - pub nodes: Vec>>, + pub nodes: Vec>, pub range: StringRange, pub child: Option>>, - pub modifier: Option>>, + pub modifier: Option>>, pub forks: bool, } -impl CommandContext { +impl Clone for CommandContext { + 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 CommandContext { pub fn copy_for(&self, source: Rc) -> Self { if Rc::ptr_eq(&source, &self.source) { return self.clone(); -- cgit v1.2.3