format code
This commit is contained in:
1 parent
413f88d145
commit
92e6d264b4
9 files changed
+186
-170
No files matched your search
@@ -1,12 +1,14 @@
|
||||
use std::collections::HashMap;
|
||||
use crate::commands::dispatcher::InvalidTreeError::{
|
||||
InvalidConsumptionError, InvalidRequirementError,
|
||||
};
|
||||
use crate::commands::tree::{CommandTree, ConsumedArgs, NodeType, RawArgs};
|
||||
use crate::commands::CommandSender;
|
||||
use crate::commands::dispatcher::InvalidTreeError::{InvalidConsumptionError, InvalidRequirementError};
|
||||
use crate::commands::tree::{ConsumedArgs, NodeType, RawArgs, CommandTree};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum InvalidTreeError {
|
||||
/// This error means that there was an error while parsing a previously consumed argument.
|
||||
/// That only happens when consumption is wrongly implemented, as it should ensure parsing may
|
||||
/// That only happens when consumption is wrongly implemented, as it should ensure parsing may
|
||||
/// never fail.
|
||||
InvalidConsumptionError(Option<String>),
|
||||
|
||||
@@ -15,15 +17,13 @@ pub(crate) enum InvalidTreeError {
|
||||
}
|
||||
|
||||
pub(crate) struct CommandDispatcher<'a> {
|
||||
pub(crate) commands: HashMap<&'a str, CommandTree<'a>>
|
||||
pub(crate) commands: HashMap<&'a str, CommandTree<'a>>,
|
||||
}
|
||||
|
||||
/// Stores registered [CommandTree]s and dispatches commands to them.
|
||||
impl <'a> CommandDispatcher<'a> {
|
||||
|
||||
impl<'a> CommandDispatcher<'a> {
|
||||
/// Execute a command using its corresponding [CommandTree].
|
||||
pub(crate) fn dispatch(&'a self, src: &mut CommandSender, cmd: &str) -> Result<(), String> {
|
||||
|
||||
let mut parts = cmd.split_ascii_whitespace();
|
||||
let key = parts.next().ok_or("Empty Command")?;
|
||||
let raw_args: Vec<&str> = parts.rev().collect();
|
||||
@@ -35,23 +35,32 @@ impl <'a> CommandDispatcher<'a> {
|
||||
match Self::try_is_fitting_path(src, path, tree, raw_args.clone()) {
|
||||
Err(InvalidConsumptionError(s)) => {
|
||||
println!("Error while parsing command \"{cmd}\": {s:?} was consumed, but couldn't be parsed");
|
||||
return Err("Internal Error (See logs for details)".into())
|
||||
},
|
||||
return Err("Internal Error (See logs for details)".into());
|
||||
}
|
||||
Err(InvalidRequirementError) => {
|
||||
println!("Error while parsing command \"{cmd}\": a requirement that was expected was not met.");
|
||||
return Err("Internal Error (See logs for details)".into())
|
||||
},
|
||||
return Err("Internal Error (See logs for details)".into());
|
||||
}
|
||||
Ok(is_fitting_path) => {
|
||||
if is_fitting_path { return Ok(()) }
|
||||
if is_fitting_path {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Err(format!("Invalid Syntax. Usage:{}", tree.paths_formatted(key)))
|
||||
Err(format!(
|
||||
"Invalid Syntax. Usage:{}",
|
||||
tree.paths_formatted(key)
|
||||
))
|
||||
}
|
||||
|
||||
fn try_is_fitting_path(src: &mut CommandSender, path: Vec<usize>, tree: &CommandTree, mut raw_args: RawArgs) -> Result<bool, InvalidTreeError> {
|
||||
|
||||
fn try_is_fitting_path(
|
||||
src: &mut CommandSender,
|
||||
path: Vec<usize>,
|
||||
tree: &CommandTree,
|
||||
mut raw_args: RawArgs,
|
||||
) -> Result<bool, InvalidTreeError> {
|
||||
let mut parsed_args: ConsumedArgs = HashMap::new();
|
||||
|
||||
for node in path.iter().map(|&i| &tree.nodes[i]) {
|
||||
@@ -69,7 +78,11 @@ impl <'a> CommandDispatcher<'a> {
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
NodeType::Argument { consumer: consume, name, .. } => {
|
||||
NodeType::Argument {
|
||||
consumer: consume,
|
||||
name,
|
||||
..
|
||||
} => {
|
||||
if let Some(consumed) = consume(src, &mut raw_args) {
|
||||
parsed_args.insert(name, consumed);
|
||||
} else {
|
||||
@@ -82,8 +95,8 @@ impl <'a> CommandDispatcher<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user