fix clippy lints
This commit is contained in:
1 parent
23458e85b8
commit
4bc1f7b0b2
6 files changed
+52
-47
No files matched your search
@@ -5,39 +5,50 @@ use crate::commands::dispatcher::InvalidTreeError;
|
|||||||
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
|
use crate::commands::dispatcher::InvalidTreeError::InvalidConsumptionError;
|
||||||
use crate::commands::tree::{ConsumedArgs, RawArgs};
|
use crate::commands::tree::{ConsumedArgs, RawArgs};
|
||||||
|
|
||||||
/// todo: implement (so far only own name + @s is implemented)
|
/// todo: implement (so far only own name + @s/@p is implemented)
|
||||||
pub fn consume_arg_player(src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
pub fn consume_arg_player(src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
||||||
let s = args.pop()?;
|
let s = args.pop()?;
|
||||||
|
|
||||||
if let Player(client) = src {
|
match s {
|
||||||
if s == "@s" {
|
"@s" if src.is_player() => Some(s.into()),
|
||||||
return Some(s.into())
|
"@p" if src.is_player() => Some(s.into()),
|
||||||
}
|
"@r" => None, // todo: implement random player target selector
|
||||||
if let Some(profile) = &client.gameprofile {
|
"@a" | "@e" => None, // todo: implement all players target selector
|
||||||
if profile.name == s {
|
_ => {
|
||||||
return Some(s.into())
|
// todo: implement any other player than sender
|
||||||
|
if let Player(client) = src {
|
||||||
|
if let Some(profile) = &client.gameprofile {
|
||||||
|
if profile.name == s {
|
||||||
|
return Some(s.into())
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
None
|
||||||
};
|
}
|
||||||
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// todo: implement (so far only own name + @s is implemented)
|
/// todo: implement (so far only own name + @s/@p is implemented)
|
||||||
pub fn parse_arg_player<'a>(src: &'a mut CommandSender, arg_name: &str, consumed_args: &ConsumedArgs) -> Result<&'a mut Client, InvalidTreeError> {
|
pub fn parse_arg_player<'a>(src: &'a mut CommandSender, arg_name: &str, consumed_args: &ConsumedArgs) -> Result<&'a mut Client, InvalidTreeError> {
|
||||||
let s = consumed_args.get(arg_name)
|
let s = consumed_args.get(arg_name)
|
||||||
.ok_or(InvalidConsumptionError(None))?;
|
.ok_or(InvalidConsumptionError(None))?
|
||||||
|
.as_str();
|
||||||
|
|
||||||
if let Player(client) = src {
|
match s {
|
||||||
if s == "@s" {
|
"@s" if src.is_player() => Ok(src.as_mut_player().unwrap()),
|
||||||
return Ok(client)
|
"@p" if src.is_player() => Ok(src.as_mut_player().unwrap()),
|
||||||
}
|
"@r" => Err(InvalidConsumptionError(Some(s.into()))), // todo: implement random player target selector
|
||||||
if let Some(profile) = &client.gameprofile {
|
"@a" | "@e" => Err(InvalidConsumptionError(Some(s.into()))), // todo: implement all players target selector
|
||||||
if profile.name == s.as_ref() {
|
_ => {
|
||||||
return Ok(client)
|
// todo: implement any other player than sender
|
||||||
|
if let Player(client) = src {
|
||||||
|
if let Some(profile) = &client.gameprofile {
|
||||||
|
if profile.name == s {
|
||||||
|
return Ok(client)
|
||||||
|
};
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
Err(InvalidConsumptionError(Some(s.into())))
|
||||||
};
|
}
|
||||||
|
}
|
||||||
Err(InvalidConsumptionError(Some(s.into())))
|
|
||||||
}
|
}
|
||||||
@@ -30,7 +30,7 @@ pub fn consume_arg_gamemode(_src: &CommandSender, args: &mut RawArgs) -> Option<
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
return match GameMode::from_str(s) {
|
match GameMode::from_str(s) {
|
||||||
Err(_) | Ok(GameMode::Undefined) => None,
|
Err(_) | Ok(GameMode::Undefined) => None,
|
||||||
Ok(_) => Some(s.into())
|
Ok(_) => Some(s.into())
|
||||||
}
|
}
|
||||||
@@ -47,10 +47,10 @@ pub fn parse_arg_gamemode(consumed_args: &ConsumedArgs) -> Result<GameMode, Inva
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
return match GameMode::from_str(s) {
|
match GameMode::from_str(s) {
|
||||||
Err(_) | Ok(GameMode::Undefined) => Err(InvalidConsumptionError(Some(s.into()))),
|
Err(_) | Ok(GameMode::Undefined) => Err(InvalidConsumptionError(Some(s.into()))),
|
||||||
Ok(gamemode) => Ok(gamemode)
|
Ok(gamemode) => Ok(gamemode)
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ const ARG_COMMAND: &str = "command";
|
|||||||
fn consume_arg_command(_src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
fn consume_arg_command(_src: &CommandSender, args: &mut RawArgs) -> Option<String> {
|
||||||
let s = args.pop()?;
|
let s = args.pop()?;
|
||||||
|
|
||||||
let dispatcher = DISPATCHER;
|
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);
|
||||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
|
||||||
|
|
||||||
if dispatcher.commands.contains_key(s) { Some(s.into()) }
|
if dispatcher.commands.contains_key(s) { Some(s.into()) }
|
||||||
else { None }
|
else { None }
|
||||||
@@ -36,8 +35,7 @@ fn parse_arg_command<'a>(consumed_args: &'a ConsumedArgs, dispatcher: &'a Comman
|
|||||||
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
||||||
CommandTree::new(DESCRIPTION).with_child(
|
CommandTree::new(DESCRIPTION).with_child(
|
||||||
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
|
argument(ARG_COMMAND, consume_arg_command).execute(&|sender, args| {
|
||||||
let dispatcher = DISPATCHER;
|
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);
|
||||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
|
||||||
|
|
||||||
let (name, tree) = parse_arg_command(args, dispatcher)?;
|
let (name, tree) = parse_arg_command(args, dispatcher)?;
|
||||||
|
|
||||||
@@ -50,8 +48,7 @@ pub(crate) fn init_command_tree<'a>() -> CommandTree<'a> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
).execute(&|sender, _args| {
|
).execute(&|sender, _args| {
|
||||||
let dispatcher = DISPATCHER;
|
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);
|
||||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
|
||||||
|
|
||||||
for (name, tree) in &dispatcher.commands {
|
for (name, tree) in &dispatcher.commands {
|
||||||
sender.send_message(
|
sender.send_message(
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ impl<'a> CommandSender<'a> {
|
|||||||
///
|
///
|
||||||
/// Central point from which commands are dispatched. Should always be initialized using
|
/// Central point from which commands are dispatched. Should always be initialized using
|
||||||
/// [dispatcher_init].
|
/// [dispatcher_init].
|
||||||
const DISPATCHER: OnceLock<CommandDispatcher> = OnceLock::new();
|
static DISPATCHER: OnceLock<CommandDispatcher> = OnceLock::new();
|
||||||
|
|
||||||
/// create [CommandDispatcher] instance for [DISPATCHER]
|
/// create [CommandDispatcher] instance for [DISPATCHER]
|
||||||
fn dispatcher_init<'a>() -> CommandDispatcher<'a> {
|
fn dispatcher_init<'a>() -> CommandDispatcher<'a> {
|
||||||
@@ -84,8 +84,7 @@ fn dispatcher_init<'a>() -> CommandDispatcher<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_command(sender: &mut CommandSender, cmd: &str) {
|
pub fn handle_command(sender: &mut CommandSender, cmd: &str) {
|
||||||
let dispatcher = DISPATCHER;
|
let dispatcher = DISPATCHER.get_or_init(dispatcher_init);
|
||||||
let dispatcher = dispatcher.get_or_init(dispatcher_init);
|
|
||||||
|
|
||||||
if let Err(err) = dispatcher.dispatch(sender, cmd) {
|
if let Err(err) = dispatcher.dispatch(sender, cmd) {
|
||||||
sender.send_message(
|
sender.send_message(
|
||||||
|
|||||||
@@ -2,15 +2,13 @@ use std::collections::{HashMap, VecDeque};
|
|||||||
|
|
||||||
use crate::commands::CommandSender;
|
use crate::commands::CommandSender;
|
||||||
use crate::commands::dispatcher::InvalidTreeError;
|
use crate::commands::dispatcher::InvalidTreeError;
|
||||||
use crate::commands::tree_builder::{argument, NonLeafNodeBuilder};
|
/// see [crate::commands::tree_builder::argument]
|
||||||
|
|
||||||
/// see [argument]
|
|
||||||
pub(crate) type RawArgs<'a> = Vec<&'a str>;
|
pub(crate) type RawArgs<'a> = Vec<&'a str>;
|
||||||
|
|
||||||
/// see [argument] and [CommandTree::execute]/[NonLeafNodeBuilder::execute]
|
/// see [crate::commands::tree_builder::argument] and [CommandTree::execute]/[crate::commands::tree_builder::NonLeafNodeBuilder::execute]
|
||||||
pub(crate) type ConsumedArgs<'a> = HashMap<&'a str, String>;
|
pub(crate) type ConsumedArgs<'a> = HashMap<&'a str, String>;
|
||||||
|
|
||||||
/// see [argument]
|
/// see [crate::commands::tree_builder::argument]
|
||||||
pub(crate) type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;
|
pub(crate) type ArgumentConsumer<'a> = fn(&CommandSender, &mut RawArgs) -> Option<String>;
|
||||||
|
|
||||||
pub(crate) struct Node<'a> {
|
pub(crate) struct Node<'a> {
|
||||||
@@ -20,7 +18,7 @@ pub(crate) struct Node<'a> {
|
|||||||
|
|
||||||
pub(crate) enum NodeType<'a> {
|
pub(crate) enum NodeType<'a> {
|
||||||
ExecuteLeaf {
|
ExecuteLeaf {
|
||||||
run: &'a dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError>,
|
run: &'a (dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError> + Sync),
|
||||||
},
|
},
|
||||||
Literal {
|
Literal {
|
||||||
string: &'a str,
|
string: &'a str,
|
||||||
@@ -30,7 +28,7 @@ pub(crate) enum NodeType<'a> {
|
|||||||
consumer: ArgumentConsumer<'a>,
|
consumer: ArgumentConsumer<'a>,
|
||||||
},
|
},
|
||||||
Require {
|
Require {
|
||||||
predicate: &'a dyn Fn(&CommandSender) -> bool,
|
predicate: &'a (dyn Fn(&CommandSender) -> bool + Sync),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ impl <'a> CommandTree<'a> {
|
|||||||
/// desired type.
|
/// desired type.
|
||||||
///
|
///
|
||||||
/// Also see [NonLeafNodeBuilder::execute].
|
/// Also see [NonLeafNodeBuilder::execute].
|
||||||
pub fn execute(mut self, run: &'a dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError>) -> Self {
|
pub fn execute(mut self, run: &'a (dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError> + Sync)) -> Self {
|
||||||
let node = Node {
|
let node = Node {
|
||||||
node_type: NodeType::ExecuteLeaf {
|
node_type: NodeType::ExecuteLeaf {
|
||||||
run,
|
run,
|
||||||
@@ -103,7 +103,7 @@ impl <'a>NonLeafNodeBuilder<'a> {
|
|||||||
/// desired type.
|
/// desired type.
|
||||||
///
|
///
|
||||||
/// Also see [CommandTree::execute].
|
/// Also see [CommandTree::execute].
|
||||||
pub fn execute(mut self, run: &'a dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError>) -> Self {
|
pub fn execute(mut self, run: &'a (dyn Fn(&mut CommandSender, &ConsumedArgs) -> Result<(), InvalidTreeError> + Sync)) -> Self {
|
||||||
self.leaf_nodes.push(LeafNodeBuilder {
|
self.leaf_nodes.push(LeafNodeBuilder {
|
||||||
node_type: NodeType::ExecuteLeaf {
|
node_type: NodeType::ExecuteLeaf {
|
||||||
run
|
run
|
||||||
@@ -146,7 +146,7 @@ pub fn argument<'a>(name: &'a str, consumer: ArgumentConsumer) -> NonLeafNodeBui
|
|||||||
|
|
||||||
/// ```predicate``` should return ```false``` if requirement for reaching following [Node]s is not
|
/// ```predicate``` should return ```false``` if requirement for reaching following [Node]s is not
|
||||||
/// met.
|
/// met.
|
||||||
pub fn require(predicate: &dyn Fn(&CommandSender) -> bool) -> NonLeafNodeBuilder {
|
pub fn require(predicate: &(dyn Fn(&CommandSender) -> bool + Sync)) -> NonLeafNodeBuilder {
|
||||||
NonLeafNodeBuilder {
|
NonLeafNodeBuilder {
|
||||||
node_type: NodeType::Require {
|
node_type: NodeType::Require {
|
||||||
predicate
|
predicate
|
||||||
|
|||||||
Reference in new issue
Block a user