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::cofd::dice::DicePool;
|
||||||
use crate::dice::ElementExpression;
|
use crate::dice::ElementExpression;
|
||||||
use crate::help::HelpTopic;
|
use crate::help::HelpTopic;
|
||||||
use crate::parser::trim;
|
|
||||||
use crate::roll::Roll;
|
use crate::roll::Roll;
|
||||||
|
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
|
@ -84,18 +83,16 @@ impl Command for HelpCommand {
|
||||||
/// Parse a command string into a dynamic command execution trait
|
/// Parse a command string into a dynamic command execution trait
|
||||||
/// object. Returns an error if a command was recognized but not
|
/// object. Returns an error if a command was recognized but not
|
||||||
/// parsed correctly. Returns None if no command was recognized.
|
/// 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) {
|
match parser::parse_command(s) {
|
||||||
Ok((input, command)) => match (input, &command) {
|
Ok((input, command)) => match (input, &command) {
|
||||||
//This clause prevents bot from spamming messages to itself
|
//This clause prevents bot from spamming messages to itself
|
||||||
//after executing a previous command.
|
//after executing a previous command.
|
||||||
("", Some(_)) | (_, None) => Ok(command),
|
("", Some(_)) | (_, None) => Ok(command),
|
||||||
|
|
||||||
//Any unconsumed input (except whitespace) is considered a parsing error.
|
//Any unconsumed input (whitespace should already be
|
||||||
(extra, _) => match trim(extra).as_str() {
|
// stripped) is considered a parsing error.
|
||||||
"" => Ok(command),
|
_ => Err(format!("{}: malformed expression", s)),
|
||||||
_ => Err(format!("{}: malformed dice expression", s)),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
Err(err) => Err(err.to_string()),
|
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::commands::{Command, HelpCommand, PoolRollCommand, RollCommand};
|
||||||
use crate::dice::parser::parse_element_expression;
|
use crate::dice::parser::parse_element_expression;
|
||||||
use crate::help::parse_help_topic;
|
use crate::help::parse_help_topic;
|
||||||
use crate::parser::{eat_whitespace, trim};
|
|
||||||
use nom::{bytes::complete::tag, character::complete::alpha1, IResult};
|
use nom::{bytes::complete::tag, character::complete::alpha1, IResult};
|
||||||
|
|
||||||
// Parse a roll expression.
|
// Parse a roll expression.
|
||||||
fn parse_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
|
fn parse_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
|
||||||
let (input, _) = eat_whitespace(input)?;
|
|
||||||
let (input, expression) = parse_element_expression(input)?;
|
let (input, expression) = parse_element_expression(input)?;
|
||||||
Ok((input, Box::new(RollCommand(expression))))
|
Ok((input, Box::new(RollCommand(expression))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pool_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
|
fn parse_pool_roll(input: &str) -> IResult<&str, Box<dyn Command>> {
|
||||||
let (input, _) = eat_whitespace(input)?;
|
|
||||||
let (input, pool) = parse_dice_pool(input)?;
|
let (input, pool) = parse_dice_pool(input)?;
|
||||||
Ok((input, Box::new(PoolRollCommand(pool))))
|
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>> {
|
fn help(topic: &str) -> IResult<&str, Box<dyn Command>> {
|
||||||
let (topic, _) = eat_whitespace(topic)?;
|
let topic = parse_help_topic(topic);
|
||||||
let topic = parse_help_topic(&trim(topic));
|
|
||||||
Ok(("", Box::new(HelpCommand(topic))))
|
Ok(("", Box::new(HelpCommand(topic))))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Split an input string into its constituent command and "everything
|
/// Split an input string into its constituent command and "everything
|
||||||
/// else" parts. Extracts the command separately from its input (i.e.
|
/// else" parts. Extracts the command separately from its input (i.e.
|
||||||
/// rest of the line) and returns a tuple of (command_input, command).
|
/// 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> {
|
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 (input, _) = tag("!")(input)?;
|
||||||
let (command_input, command) = alpha1(input)?;
|
let (mut command_input, command) = alpha1(input)?;
|
||||||
let (command_input, _) = eat_whitespace(command_input)?;
|
command_input = command_input.trim();
|
||||||
//TODO strip witespace from end of command input
|
|
||||||
Ok((command_input, command))
|
Ok((command_input, command))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,16 +78,15 @@ mod tests {
|
||||||
fn whitespace_at_end_test() {
|
fn whitespace_at_end_test() {
|
||||||
//TODO currently it does not chop whitespace off the end.
|
//TODO currently it does not chop whitespace off the end.
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
("1d4 ", "roll"),
|
("1d4", "roll"),
|
||||||
split_command("!roll 1d4 ").expect("got parsing error")
|
split_command("!roll 1d4 ").expect("got parsing error")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn whitespace_on_both_ends_test() {
|
fn whitespace_on_both_ends_test() {
|
||||||
//TODO currently it does not chop whitespace off the end.
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
("1d4 ", "roll"),
|
("1d4", "roll"),
|
||||||
split_command(" !roll 1d4 ").expect("got parsing error")
|
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)?;
|
let (input, whitespace) = take_while(is_whitespace)(input)?;
|
||||||
Ok((input, whitespace))
|
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