simplify the code, and invert Result and Option

This commit is contained in:
Taylor C. Richberger 2020-04-21 00:22:31 -06:00
parent 4e72498181
commit 81b8b2c0cc
2 changed files with 6 additions and 7 deletions

View File

@ -3,9 +3,9 @@ use axfive_matrix_dicebot::commands::parse_command;
fn main() -> Result<(), String> {
let command = std::env::args().skip(1).collect::<Vec<String>>().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(())

View File

@ -36,11 +36,10 @@ impl Command for RollCommand {
}
}
pub fn parse_command(s: &str) -> Option<Result<Box<dyn Command>, String>> {
pub fn parse_command(s: &str) -> Result<Option<Box<dyn Command>>, 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()),
}
}