diff --git a/src/commands/parser.rs b/src/commands/parser.rs index ad4070f..84cf416 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -5,14 +5,14 @@ use crate::dice::parser::parse_element_expression; use crate::parser::eat_whitespace; // Parse a roll expression. -fn parse_roll(input: &str) -> IResult<&str, RollCommand> { +fn parse_roll(input: &str) -> IResult<&str, Box> { let (input, _) = eat_whitespace(input)?; let (input, expression) = parse_element_expression(input)?; - Ok((input, RollCommand(expression))) + Ok((input, Box::new(RollCommand(expression)))) } -// 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. +/// 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(original_input: &str) -> IResult<&str, Option>> { let (input, _) = eat_whitespace(original_input)?; named!(command(&str) -> (&str, &str), tuple!(complete!(tag!("!")), complete!(take_while!(char::is_alphabetic)))); @@ -21,14 +21,9 @@ pub fn parse_command(original_input: &str) -> IResult<&str, Option (input, result), Err(_e) => return Ok((original_input, None)), }; - let (input, command) = match command { - "r" | "roll" => { - let (input, command) = parse_roll(input)?; - let command: Box = Box::new(command); - (input, command) - } + match command { + "r" | "roll" => parse_roll(input).map(|(input, command)| (input, Some(command))), // No recognized command, ignore this. - _ => return Ok((original_input, None)), - }; - Ok((input, Some(command))) + _ => Ok((original_input, None)), + } }