[explorer] Track result of command with a CommandResult type, and overall state with a State type
This commit is contained in:
parent
d27c455ce4
commit
a73355c769
1 changed files with 39 additions and 12 deletions
|
@ -5,10 +5,23 @@ use clap::{Arg, Command};
|
|||
use rustyline::error::ReadlineError;
|
||||
use rustyline::DefaultEditor;
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
enum ShouldContinue {
|
||||
Yes,
|
||||
No,
|
||||
struct CommandResult {
|
||||
should_continue: bool,
|
||||
should_print_position: bool,
|
||||
}
|
||||
|
||||
impl Default for CommandResult {
|
||||
fn default() -> Self {
|
||||
CommandResult {
|
||||
should_continue: true,
|
||||
should_print_position: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct State {
|
||||
builder: PositionBuilder,
|
||||
position: Position,
|
||||
}
|
||||
|
||||
fn command_line() -> Command {
|
||||
|
@ -41,16 +54,19 @@ fn command_line() -> Command {
|
|||
.subcommand(Command::new("quit").about("Quit the program"))
|
||||
}
|
||||
|
||||
fn respond(line: &str, pos: &mut Position) -> Result<ShouldContinue, String> {
|
||||
fn respond(line: &str, state: &mut State) -> Result<CommandResult, String> {
|
||||
let args = shlex::split(line).ok_or("error: Invalid quoting")?;
|
||||
let matches = command_line()
|
||||
.try_get_matches_from(args)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let mut result = CommandResult::default();
|
||||
|
||||
match matches.subcommand() {
|
||||
Some(("print", _matches)) => {}
|
||||
Some(("quit", _matches)) => {
|
||||
return Ok(ShouldContinue::No);
|
||||
result.should_continue = false;
|
||||
result.should_print_position = false;
|
||||
}
|
||||
Some(("place", _matches)) => {
|
||||
let piece_arg = _matches.get_one::<String>("piece").unwrap();
|
||||
|
@ -71,16 +87,26 @@ fn respond(line: &str, pos: &mut Position) -> Result<ShouldContinue, String> {
|
|||
None => unreachable!("Subcommand required"),
|
||||
}
|
||||
|
||||
Ok(ShouldContinue::Yes)
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let mut editor = DefaultEditor::new().map_err(|err| format!("Error: {}", err.to_string()))?;
|
||||
|
||||
let mut pos = Position::empty();
|
||||
let starting_position = Position::starting();
|
||||
let builder = PositionBuilder::from_position(&starting_position);
|
||||
let mut state = State {
|
||||
builder,
|
||||
position: starting_position,
|
||||
};
|
||||
|
||||
let mut should_print_position = true;
|
||||
|
||||
loop {
|
||||
println!("{}", &pos);
|
||||
if should_print_position {
|
||||
println!("{}", &state.position);
|
||||
println!("{} to move.", state.position.player_to_move());
|
||||
}
|
||||
|
||||
let readline = editor.readline("? ");
|
||||
match readline {
|
||||
|
@ -90,9 +116,10 @@ fn main() -> Result<(), String> {
|
|||
continue;
|
||||
}
|
||||
|
||||
match respond(line, &mut pos) {
|
||||
Ok(should_continue) => {
|
||||
if should_continue == ShouldContinue::No {
|
||||
match respond(line, &mut state) {
|
||||
Ok(result) => {
|
||||
should_print_position = result.should_print_position;
|
||||
if !result.should_continue {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue