Remove useless trim function and unnecessary uses of eat_whitespace.
This commit is contained in:
parent
da0819745a
commit
8803b83ddb
|
@ -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<Option<Box<dyn Command + 'a>>, String> {
|
||||
pub fn parse_command(s: &str) -> Result<Option<Box<dyn Command>>, 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()),
|
||||
}
|
||||
|
|
|
@ -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<dyn Command>> {
|
||||
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<dyn Command>> {
|
||||
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<dyn Command>> {
|
|||
}
|
||||
|
||||
fn help(topic: &str) -> IResult<&str, Box<dyn Command>> {
|
||||
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))
|
||||
}
|
||||
|
||||
|
@ -88,7 +85,6 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn whitespace_on_both_ends_test() {
|
||||
//TODO currently it does not chop whitespace off the end.
|
||||
assert_eq!(
|
||||
("1d4", "roll"),
|
||||
split_command(" !roll 1d4 ").expect("got parsing error")
|
||||
|
|
|
@ -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 "));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue