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-08-28 22:02:41 +00:00
|
|
|
use crate::help::parse_help_topic;
|
2020-08-31 23:33:46 +00:00
|
|
|
use nom::bytes::streaming::tag;
|
|
|
|
use nom::error::ErrorKind as NomErrorKind;
|
|
|
|
use nom::Err as NomErr;
|
|
|
|
use nom::{character::complete::alpha1, IResult};
|
2020-04-21 03:15:13 +00:00
|
|
|
|
|
|
|
// Parse a roll expression.
|
2020-04-21 06:15:18 +00:00
|
|
|
fn parse_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
|
2020-04-21 03:15:13 +00:00
|
|
|
let (input, expression) = parse_element_expression(input)?;
|
2020-04-21 06:15:18 +00:00
|
|
|
Ok((input, Box::new(RollCommand(expression))))
|
2020-04-21 03:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 21:49:22 +00:00
|
|
|
fn parse_pool_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
|
|
|
|
let (input, pool) = parse_dice_pool(input)?;
|
|
|
|
Ok((input, Box::new(PoolRollCommand(pool))))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chance_die() -> IResult<&'static str, Box<dyn Command>> {
|
|
|
|
let (input, pool) = create_chance_die()?;
|
|
|
|
Ok((input, Box::new(PoolRollCommand(pool))))
|
|
|
|
}
|
|
|
|
|
2020-08-28 22:02:41 +00:00
|
|
|
fn help(topic: &str) -> IResult<&str, Box<dyn Command>> {
|
2020-08-31 20:16:43 +00:00
|
|
|
let topic = parse_help_topic(topic);
|
2020-08-28 22:02:41 +00:00
|
|
|
Ok(("", Box::new(HelpCommand(topic))))
|
|
|
|
}
|
|
|
|
|
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-08-31 00:07:56 +00:00
|
|
|
fn split_command(input: &str) -> IResult<&str, &str> {
|
2020-08-31 20:16:43 +00:00
|
|
|
let input = input.trim_start();
|
2020-08-31 00:07:56 +00:00
|
|
|
let (input, _) = tag("!")(input)?;
|
2020-08-31 23:33:46 +00:00
|
|
|
|
2020-08-31 20:16:43 +00:00
|
|
|
let (mut command_input, command) = alpha1(input)?;
|
|
|
|
command_input = command_input.trim();
|
2020-08-31 00:07:56 +00:00
|
|
|
Ok((command_input, command))
|
|
|
|
}
|
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-08-31 00:07:56 +00:00
|
|
|
pub fn parse_command(input: &str) -> IResult<&str, Option<Box<dyn Command>>> {
|
2020-08-31 23:33:46 +00:00
|
|
|
match split_command(input) {
|
|
|
|
Ok((cmd_input, cmd)) => match cmd {
|
|
|
|
"r" | "roll" => parse_roll(cmd_input).map(|(input, command)| (input, Some(command))),
|
|
|
|
"rp" | "pool" => {
|
|
|
|
parse_pool_roll(cmd_input).map(|(input, command)| (input, Some(command)))
|
|
|
|
}
|
|
|
|
"chance" => chance_die().map(|(input, command)| (input, Some(command))),
|
|
|
|
"help" => help(cmd_input).map(|(input, command)| (input, Some(command))),
|
|
|
|
// No recognized command, ignore this.
|
|
|
|
_ => Ok((input, None)),
|
|
|
|
},
|
|
|
|
|
|
|
|
//TODO better way to do this?
|
|
|
|
//If the input is not a command, or the message is incomplete
|
|
|
|
//(empty), we declare this to be a non-command, and don't do
|
|
|
|
//anything else with it.
|
|
|
|
Err(NomErr::Error((_, NomErrorKind::Tag))) | Err(NomErr::Incomplete(_)) => Ok(("", None)),
|
|
|
|
|
|
|
|
//All other errors passed up.
|
|
|
|
Err(e) => Err(e),
|
2020-08-31 00:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-08-31 23:33:46 +00:00
|
|
|
#[test]
|
|
|
|
fn non_command_test() {
|
|
|
|
let result = parse_command("not a command");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert!(result.unwrap().1.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_message_test() {
|
|
|
|
let result = parse_command("");
|
|
|
|
assert!(result.is_ok());
|
|
|
|
assert!(result.unwrap().1.is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn just_exclamation_point_test() {
|
|
|
|
let result = parse_command("!");
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
|
2020-08-31 00:07:56 +00:00
|
|
|
#[test]
|
|
|
|
fn basic_command_test() {
|
|
|
|
assert_eq!(
|
|
|
|
("1d4", "roll"),
|
|
|
|
split_command("!roll 1d4").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace_at_start_test() {
|
|
|
|
assert_eq!(
|
|
|
|
("1d4", "roll"),
|
|
|
|
split_command(" !roll 1d4").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn whitespace_at_end_test() {
|
|
|
|
assert_eq!(
|
2020-08-31 20:16:43 +00:00
|
|
|
("1d4", "roll"),
|
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-08-31 20:16:43 +00:00
|
|
|
("1d4", "roll"),
|
2020-08-31 00:07:56 +00:00
|
|
|
split_command(" !roll 1d4 ").expect("got parsing error")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn single_command_test() {
|
|
|
|
assert_eq!(
|
|
|
|
("", "roll"),
|
|
|
|
split_command("!roll").expect("got parsing error")
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
("", "thisdoesnotexist"),
|
|
|
|
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
|
|
|
}
|