From 8803b83ddb2972e5e1e25f5ee7d9a732611b94f7 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Mon, 31 Aug 2020 20:16:43 +0000 Subject: [PATCH] Remove useless trim function and unnecessary uses of eat_whitespace. --- src/commands.rs | 11 ++++------- src/commands/parser.rs | 20 ++++++++------------ src/parser.rs | 21 --------------------- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 9880469..337423c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,7 +1,6 @@ use crate::cofd::dice::DicePool; use crate::dice::ElementExpression; use crate::help::HelpTopic; -use crate::parser::trim; use crate::roll::Roll; pub mod parser; @@ -84,18 +83,16 @@ impl Command for HelpCommand { /// Parse a command string into a dynamic command execution trait /// object. Returns an error if a command was recognized but not /// parsed correctly. Returns None if no command was recognized. -pub fn parse_command<'a>(s: &'a str) -> Result>, String> { +pub fn parse_command(s: &str) -> Result>, String> { match parser::parse_command(s) { Ok((input, command)) => match (input, &command) { //This clause prevents bot from spamming messages to itself //after executing a previous command. ("", Some(_)) | (_, None) => Ok(command), - //Any unconsumed input (except whitespace) is considered a parsing error. - (extra, _) => match trim(extra).as_str() { - "" => Ok(command), - _ => Err(format!("{}: malformed dice expression", s)), - }, + //Any unconsumed input (whitespace should already be + // stripped) is considered a parsing error. + _ => Err(format!("{}: malformed expression", s)), }, Err(err) => Err(err.to_string()), } diff --git a/src/commands/parser.rs b/src/commands/parser.rs index 27fce63..2a9d381 100644 --- a/src/commands/parser.rs +++ b/src/commands/parser.rs @@ -2,18 +2,15 @@ use crate::cofd::parser::{create_chance_die, parse_dice_pool}; use crate::commands::{Command, HelpCommand, PoolRollCommand, RollCommand}; use crate::dice::parser::parse_element_expression; use crate::help::parse_help_topic; -use crate::parser::{eat_whitespace, trim}; use nom::{bytes::complete::tag, character::complete::alpha1, IResult}; // Parse a roll expression. fn parse_roll(input: &str) -> IResult<&str, Box> { - let (input, _) = eat_whitespace(input)?; let (input, expression) = parse_element_expression(input)?; Ok((input, Box::new(RollCommand(expression)))) } fn parse_pool_roll(input: &str) -> IResult<&str, Box> { - let (input, _) = eat_whitespace(input)?; let (input, pool) = parse_dice_pool(input)?; Ok((input, Box::new(PoolRollCommand(pool)))) } @@ -24,20 +21,20 @@ fn chance_die() -> IResult<&'static str, Box> { } fn help(topic: &str) -> IResult<&str, Box> { - let (topic, _) = eat_whitespace(topic)?; - let topic = parse_help_topic(&trim(topic)); + let topic = parse_help_topic(topic); Ok(("", Box::new(HelpCommand(topic)))) } /// 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). +/// Whitespace at the start and end of the command input is removed. fn split_command(input: &str) -> IResult<&str, &str> { - let (input, _) = eat_whitespace(input)?; + //let (input, _) = eat_whitespace(input)?; + let input = input.trim_start(); let (input, _) = tag("!")(input)?; - let (command_input, command) = alpha1(input)?; - let (command_input, _) = eat_whitespace(command_input)?; - //TODO strip witespace from end of command input + let (mut command_input, command) = alpha1(input)?; + command_input = command_input.trim(); Ok((command_input, command)) } @@ -81,16 +78,15 @@ mod tests { fn whitespace_at_end_test() { //TODO currently it does not chop whitespace off the end. assert_eq!( - ("1d4 ", "roll"), + ("1d4", "roll"), split_command("!roll 1d4 ").expect("got parsing error") ); } #[test] fn whitespace_on_both_ends_test() { - //TODO currently it does not chop whitespace off the end. assert_eq!( - ("1d4 ", "roll"), + ("1d4", "roll"), split_command(" !roll 1d4 ").expect("got parsing error") ); } diff --git a/src/parser.rs b/src/parser.rs index 7f1e90d..43ec9a8 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -9,24 +9,3 @@ pub fn eat_whitespace(input: &str) -> IResult<&str, &str> { let (input, whitespace) = take_while(is_whitespace)(input)?; Ok((input, whitespace)) } - -/// Remove the whitespace on the ends of the string. -pub fn trim(input: &str) -> String { - //2 allocations, how fun - String::from(input).trim().to_owned() -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn basic_trim_test() { - assert_eq!(String::from("blah"), trim(" blah ")); - } - - #[test] - fn trim_only_removes_ends_test() { - assert_eq!(String::from("b l a h"), trim(" b l a h ")); - } -}