This commit is contained in:
Taylor C. Richberger 2020-04-21 00:15:18 -06:00
parent 1a34a390d4
commit 4e72498181
1 changed files with 8 additions and 13 deletions

View File

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