2020-08-21 21:49:22 +00:00
|
|
|
use crate::cofd::parser::{create_chance_die, parse_dice_pool};
|
2020-08-28 22:02:41 +00:00
|
|
|
use crate::commands::{Command, HelpCommand, PoolRollCommand, RollCommand};
|
2020-04-21 03:15:13 +00:00
|
|
|
use crate::dice::parser::parse_element_expression;
|
2020-10-04 21:32:50 +00:00
|
|
|
use crate::error::BotError;
|
2020-08-28 22:02:41 +00:00
|
|
|
use crate::help::parse_help_topic;
|
2020-10-04 21:32:50 +00:00
|
|
|
use combine::parser::char::{char, letter, space};
|
|
|
|
use combine::{any, many1, optional, Parser};
|
2020-08-31 23:33:46 +00:00
|
|
|
use nom::Err as NomErr;
|
2020-04-21 03:15:13 +00:00
|
|
|
|
|
|
|
// Parse a roll expression.
|
2020-10-04 21:32:50 +00:00
|
|
|
fn parse_roll(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
|
|
let result = parse_element_expression(input);
|
|
|
|
match result {
|
|
|
|
Ok((rest, expression)) if rest.len() == 0 => Ok(Box::new(RollCommand(expression))),
|
|
|
|
//Legacy code boundary translates nom errors into BotErrors.
|
|
|
|
Ok(_) => Err(BotError::NomParserIncomplete),
|
|
|
|
Err(NomErr::Error(e)) => Err(BotError::NomParserError(e.1)),
|
|
|
|
Err(NomErr::Failure(e)) => Err(BotError::NomParserError(e.1)),
|
|
|
|
Err(NomErr::Incomplete(_)) => Err(BotError::NomParserIncomplete),
|
|
|
|
}
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:32:50 +00:00
|
|
|
fn parse_pool_roll(input: &str) -> Result<Box<dyn Command>, BotError> {
|
|
|
|
let pool = parse_dice_pool(input)?;
|
|
|
|
Ok(Box::new(PoolRollCommand(pool)))
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:32:50 +00:00
|
|
|
fn chance_die() -> Result<Box<dyn Command>, BotError> {
|
|
|
|
let pool = create_chance_die()?;
|
|
|
|
Ok(Box::new(PoolRollCommand(pool)))
|
2020-08-21 21:49:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 21:32:50 +00:00
|
|
|
fn help(topic: &str) -> Result<Box<dyn Command>, BotError> {
|
2020-08-31 20:16:43 +00:00
|
|
|
let topic = parse_help_topic(topic);
|
2020-10-04 21:32:50 +00:00
|
|
|
Ok(Box::new(HelpCommand(topic)))
|
2020-08-28 22:02:41 +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-04 21:32:50 +00:00
|
|
|
fn split_command(input: &str) -> Result<(String, String), BotError> {
|
|
|
|
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
|
|
|
|
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-08-31 23:33:46 +00:00
|
|
|
/// we don't recognize the command, ignore it and return None.
|
2020-10-04 21:32:50 +00:00
|
|
|
pub fn parse_command(input: &str) -> Result<Option<Box<dyn Command>>, BotError> {
|
2020-08-31 23:33:46 +00:00
|
|
|
match split_command(input) {
|
2020-10-04 21:32:50 +00:00
|
|
|
Ok((cmd, cmd_input)) => match cmd.as_ref() {
|
|
|
|
"r" | "roll" => parse_roll(&cmd_input).map(|command| Some(command)),
|
|
|
|
"rp" | "pool" => parse_pool_roll(&cmd_input).map(|command| Some(command)),
|
|
|
|
"chance" => chance_die().map(|command| Some(command)),
|
|
|
|
"help" => help(&cmd_input).map(|command| Some(command)),
|
2020-08-31 23:33:46 +00:00
|
|
|
// No recognized command, ignore this.
|
2020-10-04 21:32:50 +00:00
|
|
|
_ => Ok(None),
|
2020-08-31 23:33:46 +00:00
|
|
|
},
|
|
|
|
//All other errors passed up.
|
|
|
|
Err(e) => Err(e),
|
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-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-04-21 03:15:13 +00:00
|
|
|
}
|