2021-05-14 22:07:16 +00:00
|
|
|
/**
|
|
|
|
* In addition to the terms of the AGPL, portions of this file are
|
|
|
|
* governed by the terms of the MIT license, from the original
|
|
|
|
* axfive-matrix-dicebot project.
|
|
|
|
*/
|
2020-10-15 16:52:08 +00:00
|
|
|
use crate::commands::{
|
2020-10-31 12:40:44 +00:00
|
|
|
basic_rolling::RollCommand,
|
|
|
|
cofd::PoolRollCommand,
|
2020-10-31 14:03:18 +00:00
|
|
|
cthulhu::{CthAdvanceRoll, CthRoll},
|
2021-05-26 15:28:59 +00:00
|
|
|
management::{CheckCommand, LinkCommand, RegisterCommand, UnlinkCommand, UnregisterCommand},
|
2020-10-31 12:40:44 +00:00
|
|
|
misc::HelpCommand,
|
|
|
|
variables::{
|
|
|
|
DeleteVariableCommand, GetAllVariablesCommand, GetVariableCommand, SetVariableCommand,
|
|
|
|
},
|
|
|
|
Command,
|
2020-10-15 16:52:08 +00:00
|
|
|
};
|
2020-10-04 21:32:50 +00:00
|
|
|
use crate::error::BotError;
|
|
|
|
use combine::parser::char::{char, letter, space};
|
|
|
|
use combine::{any, many1, optional, Parser};
|
2021-05-25 23:54:06 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-10-31 13:52:46 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2020-10-31 21:03:17 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Error)]
|
2020-10-31 13:52:46 +00:00
|
|
|
pub enum CommandParsingError {
|
2020-10-31 21:03:17 +00:00
|
|
|
#[error("unrecognized command: {0}")]
|
|
|
|
UnrecognizedCommand(String),
|
|
|
|
|
2020-10-31 13:52:46 +00:00
|
|
|
#[error("parser error: {0}")]
|
|
|
|
InternalParseError(#[from] combine::error::StringStreamError),
|
|
|
|
}
|
2020-04-21 03:15:13 +00:00
|
|
|
|
2020-08-31 00:07:56 +00:00
|
|
|
/// Split an input string into its constituent command and "everything
|
|
|
|
/// else" parts. Extracts the command separately from its input (i.e.
|
|
|
|
/// rest of the line) and returns a tuple of (command_input, command).
|
2020-08-31 20:16:43 +00:00
|
|
|
/// Whitespace at the start and end of the command input is removed.
|
2020-10-31 13:52:46 +00:00
|
|
|
fn split_command(input: &str) -> Result<(String, String), CommandParsingError> {
|
2020-10-04 21:32:50 +00:00
|
|
|
let input = input.trim();
|
|
|
|
|
|
|
|
let exclamation = char('!');
|
|
|
|
let word = many1(letter()).map(|value: String| value);
|
|
|
|
let at_least_one_space = many1(space().silent()).map(|value: String| value);
|
|
|
|
let cmd_input = optional(at_least_one_space.and(many1(any()).map(|value: String| value)));
|
|
|
|
|
|
|
|
let mut parser = exclamation.and(word).and(cmd_input);
|
|
|
|
|
|
|
|
//TODO make less wacky, possibly by mapping it into a struct and
|
|
|
|
// making use of skip. This super-wacky tuple is:
|
|
|
|
// (parsed_input, rest)
|
|
|
|
//Where parsed_input is:
|
|
|
|
// (!command, option<arguments>)
|
|
|
|
//Where !command is:
|
|
|
|
// ('!', command)
|
|
|
|
//Were option<arguments> is:
|
|
|
|
// Option tuple of (whitespace, arguments)
|
|
|
|
let (command, command_input) = match parser.parse(input)? {
|
|
|
|
(((_, command), Some((_, command_input))), _) => (command, command_input),
|
|
|
|
(((_, command), None), _) => (command, "".to_string()),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((command, command_input))
|
2020-08-31 00:07:56 +00:00
|
|
|
}
|
2020-08-21 21:49:22 +00:00
|
|
|
|
2021-05-25 23:54:06 +00:00
|
|
|
/// Atempt to convert text input to a Boxed command type. Shortens
|
|
|
|
/// boilerplate.
|
|
|
|
macro_rules! convert_to {
|
|
|
|
($type:ident, $input: expr) => {
|
|
|
|
$type::try_from($input.as_str()).map(Into::into)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-31 00:07:56 +00:00
|
|
|
/// Potentially parse a command expression. If we recognize the
|
|
|
|
/// command, an error should be raised if the command is misparsed. If
|
2020-11-12 21:05:14 +00:00
|
|
|
/// we don't recognize the command, return an error.
|
2020-10-31 21:03:17 +00:00
|
|
|
pub fn parse_command(input: &str) -> Result<Box<dyn Command>, BotError> {
|
2020-08-31 23:33:46 +00:00
|
|
|
match split_command(input) {
|
2021-05-21 22:40:03 +00:00
|
|
|
Ok((cmd, cmd_input)) => match cmd.to_lowercase().as_ref() {
|
2021-05-25 23:54:06 +00:00
|
|
|
// "variables" => GetAllVariablesCommand::try_from(input).map(Into::into),
|
|
|
|
"variables" => convert_to!(GetAllVariablesCommand, cmd_input),
|
|
|
|
"get" => convert_to!(GetVariableCommand, cmd_input),
|
|
|
|
"set" => convert_to!(SetVariableCommand, cmd_input),
|
|
|
|
"del" => convert_to!(DeleteVariableCommand, cmd_input),
|
|
|
|
"r" | "roll" => convert_to!(RollCommand, cmd_input),
|
|
|
|
"rp" | "pool" => convert_to!(PoolRollCommand, cmd_input),
|
|
|
|
"chance" => PoolRollCommand::chance_die().map(Into::into),
|
|
|
|
"cthroll" => convert_to!(CthRoll, cmd_input),
|
|
|
|
"cthadv" | "ctharoll" => convert_to!(CthAdvanceRoll, cmd_input),
|
|
|
|
"help" => convert_to!(HelpCommand, cmd_input),
|
|
|
|
"register" => convert_to!(RegisterCommand, cmd_input),
|
2021-05-26 15:28:59 +00:00
|
|
|
"link" => convert_to!(LinkCommand, cmd_input),
|
|
|
|
"unlink" => convert_to!(UnlinkCommand, cmd_input),
|
2021-05-25 23:54:06 +00:00
|
|
|
"check" => convert_to!(CheckCommand, cmd_input),
|
|
|
|
"unregister" => convert_to!(UnregisterCommand, cmd_input),
|
2020-10-31 21:03:17 +00:00
|
|
|
_ => Err(CommandParsingError::UnrecognizedCommand(cmd).into()),
|
2020-08-31 23:33:46 +00:00
|
|
|
},
|
|
|
|
//All other errors passed up.
|
2020-10-31 13:52:46 +00:00
|
|
|
Err(e) => Err(e.into()),
|
2020-08-31 00:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-10-04 21:32:50 +00:00
|
|
|
//TODO these errors don't seem to implement the right traits to do
|
|
|
|
//eq checks or even unwrap_err!
|
|
|
|
|
2020-08-31 23:33:46 +00:00
|
|
|
#[test]
|
|
|
|
fn non_command_test() {
|
|
|
|
let result = parse_command("not a command");
|
2020-10-04 21:32:50 +00:00
|
|
|
assert!(result.is_err());
|
2020-08-31 23:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_message_test() {
|
|
|
|
let result = parse_command("");
|
2020-10-04 21:32:50 +00:00
|
|
|
assert!(result.is_err());
|
2020-08-31 23:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-09-01 08:17:59 +00:00
|
|
|
fn just_exclamation_mark_test() {
|
2020-08-31 23:33:46 +00:00
|
|
|
let result = parse_command("!");
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
2020-11-05 23:03:22 +00:00
|
|
|
#[test]
|
|
|
|
fn newline_test() {
|
|
|
|
assert!(parse_command("\n!roll 1d4").is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace_and_newline_test() {
|
|
|
|
assert!(parse_command(" \n!roll 1d4").is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn newline_and_whitespace_test() {
|
|
|
|
assert!(parse_command("\n !cthroll 50").is_ok());
|
|
|
|
}
|
|
|
|
|
2020-09-01 08:17:59 +00:00
|
|
|
#[test]
|
|
|
|
fn word_with_exclamation_mark_test() {
|
|
|
|
let result1 = parse_command("hello !notacommand");
|
2020-10-04 21:32:50 +00:00
|
|
|
assert!(result1.is_err());
|
2020-09-01 08:17:59 +00:00
|
|
|
|
|
|
|
let result2 = parse_command("hello!");
|
2020-10-04 21:32:50 +00:00
|
|
|
assert!(result2.is_err());
|
2020-09-01 08:17:59 +00:00
|
|
|
|
|
|
|
let result3 = parse_command("hello!notacommand");
|
2020-10-04 21:32:50 +00:00
|
|
|
assert!(result3.is_err());
|
2020-09-01 08:17:59 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 00:07:56 +00:00
|
|
|
#[test]
|
|
|
|
fn basic_command_test() {
|
|
|
|
assert_eq!(
|
2020-10-04 21:32:50 +00:00
|
|
|
("roll".to_string(), "1d4".to_string()),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command("!roll 1d4").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace_at_start_test() {
|
|
|
|
assert_eq!(
|
2020-10-04 21:32:50 +00:00
|
|
|
("roll".to_string(), "1d4".to_string()),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command(" !roll 1d4").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace_at_end_test() {
|
|
|
|
assert_eq!(
|
2020-10-04 21:32:50 +00:00
|
|
|
("roll".to_string(), "1d4".to_string()),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command("!roll 1d4 ").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace_on_both_ends_test() {
|
|
|
|
assert_eq!(
|
2020-10-04 21:32:50 +00:00
|
|
|
("roll".to_string(), "1d4".to_string()),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command(" !roll 1d4 ").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_command_test() {
|
|
|
|
assert_eq!(
|
2020-10-04 21:32:50 +00:00
|
|
|
("roll".to_string(), "".to_string()),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command("!roll").expect("got parsing error")
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2020-10-04 21:32:50 +00:00
|
|
|
("thisdoesnotexist".to_string(), "".to_string()),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command("!thisdoesnotexist").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bad_command_test() {
|
|
|
|
assert!(split_command("roll 1d4").is_err());
|
|
|
|
assert!(split_command("roll").is_err());
|
2020-04-21 06:15:18 +00:00
|
|
|
}
|
2020-11-12 21:05:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chance_die_is_not_malformed() {
|
|
|
|
assert!(parse_command("!chance").is_ok());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_malformed_expression_test() {
|
|
|
|
assert!(parse_command("!roll 1d20asdlfkj").is_err());
|
|
|
|
assert!(parse_command("!roll 1d20asdlfkj ").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_dice_pool_malformed_expression_test() {
|
|
|
|
assert!(parse_command("!pool 8abc").is_err());
|
|
|
|
assert!(parse_command("!pool 8abc ").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pool_whitespace_test() {
|
|
|
|
parse_command("!pool ns3:8 ").expect("was error");
|
|
|
|
parse_command(" !pool ns3:8").expect("was error");
|
|
|
|
parse_command(" !pool ns3:8 ").expect("was error");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn help_whitespace_test() {
|
|
|
|
parse_command("!help stuff ").expect("was error");
|
|
|
|
parse_command(" !help stuff").expect("was error");
|
|
|
|
parse_command(" !help stuff ").expect("was error");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn roll_whitespace_test() {
|
|
|
|
parse_command("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
parse_command("!roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
parse_command(" !roll 1d4 + 5d6 -3 ").expect("was error");
|
|
|
|
}
|
2021-05-21 22:40:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn case_insensitive_test() {
|
|
|
|
parse_command("!CTHROLL 40").expect("command parsing is not case sensitive.");
|
|
|
|
}
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|