forked from projectmoon/tenebrous-dicebot
simplify the code, and invert Result and Option
This commit is contained in:
parent
4e72498181
commit
81b8b2c0cc
|
@ -3,9 +3,9 @@ use axfive_matrix_dicebot::commands::parse_command;
|
||||||
fn main() -> Result<(), String> {
|
fn main() -> Result<(), String> {
|
||||||
let command = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
let command = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
|
||||||
let command = match parse_command(&command) {
|
let command = match parse_command(&command) {
|
||||||
Some(Ok(command)) => command,
|
Ok(Some(command)) => command,
|
||||||
Some(Err(e)) => return Err(format!("Error parsing command: {}", e)),
|
Ok(None) => return Err("Command not recognized".into()),
|
||||||
None => return Err("Command not recognized".into()),
|
Err(e) => return Err(format!("Error parsing command: {}", e)),
|
||||||
};
|
};
|
||||||
println!("{}", command.execute().plain());
|
println!("{}", command.execute().plain());
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -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.
|
// Ignore trailing input, if any.
|
||||||
match parser::parse_command(s) {
|
match parser::parse_command(s) {
|
||||||
Ok((_, Some(command))) => Some(Ok(command)),
|
Ok((_, result)) => Ok(result),
|
||||||
Ok((_, None)) => None,
|
Err(err) => Err(err.to_string()),
|
||||||
Err(err) => Some(Err(err.to_string())),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue