Skip to content

Commit

Permalink
Remove unnecessary Option from parse_command return type.
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectMoon committed Oct 31, 2020
1 parent c55926a commit f4417d4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::context::Context;
use crate::error::BotError;
use crate::error::{BotError, BotError::CommandParsingError};
use async_trait::async_trait;
use parser::CommandParsingError::UnrecognizedCommand;
use thiserror::Error;

pub mod basic_rolling;
Expand Down Expand Up @@ -45,8 +46,10 @@ pub trait Command: Send + Sync {
/// parsed correctly. Returns Ok(None) if no command was recognized.
pub fn parse(s: &str) -> Result<Box<dyn Command>, BotError> {
match parser::parse_command(s) {
Ok(Some(command)) => Ok(command),
Ok(None) => Err(BotError::CommandError(CommandError::IgnoredCommand)),
Ok(command) => Ok(command),
Err(CommandParsingError(UnrecognizedCommand(_))) => {
Err(CommandError::IgnoredCommand.into())
}
Err(e) => Err(e),
}
}
Expand Down
31 changes: 16 additions & 15 deletions src/commands/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ use combine::{any, many1, optional, Parser};
use nom::Err as NomErr;
use thiserror::Error;

#[derive(Debug, Clone, Copy, PartialEq, Error)]
#[derive(Debug, Clone, PartialEq, Error)]
pub enum CommandParsingError {
#[error("unrecognized command: {0}")]
UnrecognizedCommand(String),

#[error("parser error: {0}")]
InternalParseError(#[from] combine::error::StringStreamError),
}
Expand Down Expand Up @@ -114,23 +117,21 @@ fn split_command(input: &str) -> Result<(String, String), CommandParsingError> {
/// 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(input: &str) -> Result<Option<Box<dyn Command>>, BotError> {
pub fn parse_command(input: &str) -> Result<Box<dyn Command>, BotError> {
match split_command(input) {
Ok((cmd, cmd_input)) => match cmd.as_ref() {
"variables" => get_all_variables().map(|command| Some(command)),
"get" => parse_get_variable_command(&cmd_input).map(|command| Some(command)),
"set" => parse_set_variable_command(&cmd_input).map(|command| Some(command)),
"del" => parse_delete_variable_command(&cmd_input).map(|command| Some(command)),
"r" | "roll" => parse_roll(&cmd_input).map(|command| Some(command)),
"rp" | "pool" => parse_pool_roll(&cmd_input).map(|command| Some(command)),
"cthroll" | "cthRoll" => parse_cth_roll(&cmd_input).map(|command| Some(command)),
"cthadv" | "cthARoll" => {
parse_cth_advancement_roll(&cmd_input).map(|command| Some(command))
}
"chance" => chance_die().map(|command| Some(command)),
"help" => help(&cmd_input).map(|command| Some(command)),
"variables" => get_all_variables(),
"get" => parse_get_variable_command(&cmd_input),
"set" => parse_set_variable_command(&cmd_input),
"del" => parse_delete_variable_command(&cmd_input),
"r" | "roll" => parse_roll(&cmd_input),
"rp" | "pool" => parse_pool_roll(&cmd_input),
"cthroll" | "cthRoll" => parse_cth_roll(&cmd_input),
"cthadv" | "cthARoll" => parse_cth_advancement_roll(&cmd_input),
"chance" => chance_die(),
"help" => help(&cmd_input),
// No recognized command, ignore this.
_ => Ok(None),
_ => Err(CommandParsingError::UnrecognizedCommand(cmd).into()),
},
//All other errors passed up.
Err(e) => Err(e.into()),
Expand Down

0 comments on commit f4417d4

Please sign in to comment.