axfive_matrix_dicebot/commands/
parser.rs1use nom::{complete, named, tag, take_while, tuple, IResult};
2
3use crate::commands::{Command, RollCommand};
4use crate::dice::parser::parse_element_expression;
5use crate::parser::eat_whitespace;
6
7fn parse_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
9 let (input, _) = eat_whitespace(input)?;
10 let (input, expression) = parse_element_expression(input)?;
11 Ok((input, Box::new(RollCommand(expression))))
12}
13
14pub fn parse_command(original_input: &str) -> IResult<&str, Option<Box<dyn Command>>> {
17 let (input, _) = eat_whitespace(original_input)?;
18 named!(command(&str) -> (&str, &str), tuple!(complete!(tag!("!")), complete!(take_while!(char::is_alphabetic))));
19 let (input, command) = match command(input) {
20 Ok((input, (_, result))) => (input, result),
22 Err(_e) => return Ok((original_input, None)),
23 };
24 match command {
25 "r" | "roll" => parse_roll(input).map(|(input, command)| (input, Some(command))),
26 _ => Ok((original_input, None)),
28 }
29}