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 00:07:56 +00:00
|
|
|
use nom::{bytes::complete::tag, 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, _) = eat_whitespace(input)?;
|
|
|
|
let input = input.trim_start();
|
2020-08-31 00:07:56 +00:00
|
|
|
let (input, _) = tag("!")(input)?;
|
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
|
|
|
|
/// we don't recognize the command, ignore it and return none
|
|
|
|
pub fn parse_command(input: &str) -> IResult<&str, Option<Box<dyn Command>>> {
|
|
|
|
let (cmd_input, cmd) = split_command(input)?;
|
2020-08-21 21:49:22 +00:00
|
|
|
|
2020-08-31 00:07:56 +00:00
|
|
|
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))),
|
2020-08-21 21:49:22 +00:00
|
|
|
"chance" => chance_die().map(|(input, command)| (input, Some(command))),
|
2020-08-31 00:07:56 +00:00
|
|
|
"help" => help(cmd_input).map(|(input, command)| (input, Some(command))),
|
2020-04-21 06:07:03 +00:00
|
|
|
// No recognized command, ignore this.
|
2020-08-31 00:07:56 +00:00
|
|
|
_ => Ok((input, None)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[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() {
|
|
|
|
//TODO currently it does not chop whitespace off the end.
|
|
|
|
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
|
|
|
}
|