diff options
| author | mat <git@matdoes.dev> | 2025-02-22 23:01:54 +0000 |
|---|---|---|
| committer | mat <git@matdoes.dev> | 2025-02-22 23:01:54 +0000 |
| commit | 34f53baf85fb5c7163ec5d71a8ab9d45d3f271b6 (patch) | |
| tree | 7920fec1203e8e96463a142f5f6da6164e76e684 /azalea-brigadier/src | |
| parent | bdd2fc91e11e2896d8e1c7046df247e1075bd40d (diff) | |
| download | azalea-drasl-34f53baf85fb5c7163ec5d71a8ab9d45d3f271b6.tar.xz | |
update to rust edition 2024
Diffstat (limited to 'azalea-brigadier/src')
| -rwxr-xr-x | azalea-brigadier/src/command_dispatcher.rs | 149 | ||||
| -rwxr-xr-x | azalea-brigadier/src/context/command_context.rs | 2 | ||||
| -rwxr-xr-x | azalea-brigadier/src/context/command_context_builder.rs | 28 | ||||
| -rwxr-xr-x | azalea-brigadier/src/tree/mod.rs | 27 |
4 files changed, 119 insertions, 87 deletions
diff --git a/azalea-brigadier/src/command_dispatcher.rs b/azalea-brigadier/src/command_dispatcher.rs index 619131e2..15648b42 100755 --- a/azalea-brigadier/src/command_dispatcher.rs +++ b/azalea-brigadier/src/command_dispatcher.rs @@ -108,23 +108,30 @@ impl<S> CommandDispatcher<S> { 1 }) { reader.skip(); - if let Some(redirect) = &child.read().redirect { - let child_context = - CommandContextBuilder::new(self, source, redirect.clone(), reader.cursor); - let parse = self - .parse_nodes(redirect, &reader, child_context) - .expect("Parsing nodes failed"); - context.with_child(Rc::new(parse.context)); - return Ok(ParseResults { - context, - reader: parse.reader, - exceptions: parse.exceptions, - }); - } else { - let parse = self - .parse_nodes(&child, &reader, context) - .expect("Parsing nodes failed"); - potentials.push(parse); + match &child.read().redirect { + Some(redirect) => { + let child_context = CommandContextBuilder::new( + self, + source, + redirect.clone(), + reader.cursor, + ); + let parse = self + .parse_nodes(redirect, &reader, child_context) + .expect("Parsing nodes failed"); + context.with_child(Rc::new(parse.context)); + return Ok(ParseResults { + context, + reader: parse.reader, + exceptions: parse.exceptions, + }); + } + _ => { + let parse = self + .parse_nodes(&child, &reader, context) + .expect("Parsing nodes failed"); + potentials.push(parse); + } } } else { potentials.push(ParseResults { @@ -215,11 +222,14 @@ impl<S> CommandDispatcher<S> { pub fn find_node(&self, path: &[&str]) -> Option<Arc<RwLock<CommandNode<S>>>> { let mut node = self.root.clone(); for name in path { - if let Some(child) = node.clone().read().child(name) { - node = child; - } else { - return None; - } + match node.clone().read().child(name) { + Some(child) => { + node = child; + } + _ => { + return None; + } + }; } Some(node) } @@ -258,31 +268,41 @@ impl<S> CommandDispatcher<S> { let modifier = &context.modifier; if let Some(modifier) = modifier { let results = modifier(context); - if let Ok(results) = results { - if !results.is_empty() { - next.extend(results.iter().map(|s| child.copy_for(s.clone()))); + match results { + Ok(results) => { + if !results.is_empty() { + next.extend( + results.iter().map(|s| child.copy_for(s.clone())), + ); + } } - } else { - // TODO - // self.consumer.on_command_complete(context, false, 0); - if !forked { - return Err(results.err().unwrap()); + _ => { + // TODO + // self.consumer.on_command_complete(context, false, 0); + if !forked { + return Err(results.err().unwrap()); + } } } } else { next.push(child.copy_for(context.source.clone())); } } - } else if let Some(context_command) = &context.command { - found_command = true; + } else { + match &context.command { + Some(context_command) => { + found_command = true; - let value = context_command(context); - result += value; - // consumer.on_command_complete(context, true, value); - successful_forks += 1; + let value = context_command(context); + result += value; + // consumer.on_command_complete(context, true, value); + successful_forks += 1; - // TODO: allow context_command to error and handle those - // errors + // TODO: allow context_command to error and handle + // those errors + } + _ => {} + } } } @@ -332,32 +352,35 @@ impl<S> CommandDispatcher<S> { if node.command.is_some() { result.push(prefix.to_owned()); } - if let Some(redirect) = &node.redirect { - let redirect = if redirect.data_ptr() == self.root.data_ptr() { - "...".to_string() - } else { - format!("-> {}", redirect.read().usage_text()) - }; - if prefix.is_empty() { - result.push(format!("{} {redirect}", node.usage_text())); - } else { - result.push(format!("{prefix} {redirect}")); + match &node.redirect { + Some(redirect) => { + let redirect = if redirect.data_ptr() == self.root.data_ptr() { + "...".to_string() + } else { + format!("-> {}", redirect.read().usage_text()) + }; + if prefix.is_empty() { + result.push(format!("{} {redirect}", node.usage_text())); + } else { + result.push(format!("{prefix} {redirect}")); + } } - } else { - for child in node.children.values() { - let child = child.read(); - self.get_all_usage_recursive( - &child, - source, - result, - if prefix.is_empty() { - child.usage_text() - } else { - format!("{prefix} {}", child.usage_text()) - } - .as_str(), - restricted, - ); + _ => { + for child in node.children.values() { + let child = child.read(); + self.get_all_usage_recursive( + &child, + source, + result, + if prefix.is_empty() { + child.usage_text() + } else { + format!("{prefix} {}", child.usage_text()) + } + .as_str(), + restricted, + ); + } } } } diff --git a/azalea-brigadier/src/context/command_context.rs b/azalea-brigadier/src/context/command_context.rs index 3d401f96..202d6f21 100755 --- a/azalea-brigadier/src/context/command_context.rs +++ b/azalea-brigadier/src/context/command_context.rs @@ -2,7 +2,7 @@ use std::{any::Any, collections::HashMap, fmt::Debug, rc::Rc, sync::Arc}; use parking_lot::RwLock; -use super::{parsed_command_node::ParsedCommandNode, string_range::StringRange, ParsedArgument}; +use super::{ParsedArgument, parsed_command_node::ParsedCommandNode, string_range::StringRange}; use crate::{ modifier::RedirectModifier, tree::{Command, CommandNode}, diff --git a/azalea-brigadier/src/context/command_context_builder.rs b/azalea-brigadier/src/context/command_context_builder.rs index 3fb8ec70..87427bc1 100755 --- a/azalea-brigadier/src/context/command_context_builder.rs +++ b/azalea-brigadier/src/context/command_context_builder.rs @@ -3,8 +3,8 @@ use std::{collections::HashMap, fmt::Debug, rc::Rc, sync::Arc}; use parking_lot::RwLock; use super::{ - command_context::CommandContext, parsed_command_node::ParsedCommandNode, - string_range::StringRange, suggestion_context::SuggestionContext, ParsedArgument, + ParsedArgument, command_context::CommandContext, parsed_command_node::ParsedCommandNode, + string_range::StringRange, suggestion_context::SuggestionContext, }; use crate::{ command_dispatcher::CommandDispatcher, @@ -107,18 +107,18 @@ impl<'a, S> CommandContextBuilder<'a, S> { } if self.range.end() < cursor { - if let Some(child) = &self.child { - child.find_suggestion_context(cursor) - } else if let Some(last) = self.nodes.last() { - SuggestionContext { - parent: Arc::clone(&last.node), - start_pos: last.range.end() + 1, - } - } else { - SuggestionContext { - parent: Arc::clone(&self.root), - start_pos: self.range.start(), - } + match &self.child { + Some(child) => child.find_suggestion_context(cursor), + _ => match self.nodes.last() { + Some(last) => SuggestionContext { + parent: Arc::clone(&last.node), + start_pos: last.range.end() + 1, + }, + _ => SuggestionContext { + parent: Arc::clone(&self.root), + start_pos: self.range.start(), + }, + }, } } else { let mut prev = &self.root; diff --git a/azalea-brigadier/src/tree/mod.rs b/azalea-brigadier/src/tree/mod.rs index dfa3b375..690e5df3 100755 --- a/azalea-brigadier/src/tree/mod.rs +++ b/azalea-brigadier/src/tree/mod.rs @@ -292,18 +292,27 @@ impl<S> PartialEq for CommandNode<S> { } } - if let Some(selfexecutes) = &self.command { - // idk how to do this better since we can't compare `dyn Fn`s - if let Some(otherexecutes) = &other.command { - #[allow(ambiguous_wide_pointer_comparisons)] - if !Arc::ptr_eq(selfexecutes, otherexecutes) { + match &self.command { + Some(selfexecutes) => { + // idk how to do this better since we can't compare `dyn Fn`s + match &other.command { + Some(otherexecutes) => + { + #[allow(ambiguous_wide_pointer_comparisons)] + if !Arc::ptr_eq(selfexecutes, otherexecutes) { + return false; + } + } + _ => { + return false; + } + } + } + _ => { + if other.command.is_some() { return false; } - } else { - return false; } - } else if other.command.is_some() { - return false; } true } |
