From 81b8b2c0cced0c55f480d0020104848e3c5364cb Mon Sep 17 00:00:00 2001 From: "Taylor C. Richberger" Date: Tue, 21 Apr 2020 00:22:31 -0600 Subject: [PATCH] simplify the code, and invert Result and Option --- src/bin/dicebot-cmd.rs | 6 +++--- src/commands.rs | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/bin/dicebot-cmd.rs b/src/bin/dicebot-cmd.rs index 57a87cd..7388e1f 100644 --- a/src/bin/dicebot-cmd.rs +++ b/src/bin/dicebot-cmd.rs @@ -3,9 +3,9 @@ use axfive_matrix_dicebot::commands::parse_command; fn main() -> Result<(), String> { let command = std::env::args().skip(1).collect::>().join(" "); let command = match parse_command(&command) { - Some(Ok(command)) => command, - Some(Err(e)) => return Err(format!("Error parsing command: {}", e)), - None => return Err("Command not recognized".into()), + Ok(Some(command)) => command, + Ok(None) => return Err("Command not recognized".into()), + Err(e) => return Err(format!("Error parsing command: {}", e)), }; println!("{}", command.execute().plain()); Ok(()) diff --git a/src/commands.rs b/src/commands.rs index 739bd17..e83cf0d 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -36,11 +36,10 @@ impl Command for RollCommand { } } -pub fn parse_command(s: &str) -> Option, String>> { +pub fn parse_command(s: &str) -> Result>, String> { // Ignore trailing input, if any. match parser::parse_command(s) { - Ok((_, Some(command))) => Some(Ok(command)), - Ok((_, None)) => None, - Err(err) => Some(Err(err.to_string())), + Ok((_, result)) => Ok(result), + Err(err) => Err(err.to_string()), } }