[explorer] Track result of command with a CommandResult type, and overall state with a State type

This commit is contained in:
Eryn Wells 2024-01-24 17:16:33 -08:00
parent d27c455ce4
commit a73355c769

View file

@ -5,10 +5,23 @@ use clap::{Arg, Command};
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::DefaultEditor; use rustyline::DefaultEditor;
#[derive(Eq, PartialEq)] struct CommandResult {
enum ShouldContinue { should_continue: bool,
Yes, should_print_position: bool,
No, }
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 { fn command_line() -> Command {
@ -41,16 +54,19 @@ fn command_line() -> Command {
.subcommand(Command::new("quit").about("Quit the program")) .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 args = shlex::split(line).ok_or("error: Invalid quoting")?;
let matches = command_line() let matches = command_line()
.try_get_matches_from(args) .try_get_matches_from(args)
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
let mut result = CommandResult::default();
match matches.subcommand() { match matches.subcommand() {
Some(("print", _matches)) => {} Some(("print", _matches)) => {}
Some(("quit", _matches)) => { Some(("quit", _matches)) => {
return Ok(ShouldContinue::No); result.should_continue = false;
result.should_print_position = false;
} }
Some(("place", _matches)) => { Some(("place", _matches)) => {
let piece_arg = _matches.get_one::<String>("piece").unwrap(); 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"), None => unreachable!("Subcommand required"),
} }
Ok(ShouldContinue::Yes) Ok(result)
} }
fn main() -> Result<(), String> { fn main() -> Result<(), String> {
let mut editor = DefaultEditor::new().map_err(|err| format!("Error: {}", err.to_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 { loop {
println!("{}", &pos); if should_print_position {
println!("{}", &state.position);
println!("{} to move.", state.position.player_to_move());
}
let readline = editor.readline("? "); let readline = editor.readline("? ");
match readline { match readline {
@ -90,9 +116,10 @@ fn main() -> Result<(), String> {
continue; continue;
} }
match respond(line, &mut pos) { match respond(line, &mut state) {
Ok(should_continue) => { Ok(result) => {
if should_continue == ShouldContinue::No { should_print_position = result.should_print_position;
if !result.should_continue {
break; break;
} }
} }