From a73355c7694298642c271966e6017ebeaa364143 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 17:16:33 -0800 Subject: [PATCH] [explorer] Track result of command with a CommandResult type, and overall state with a State type --- explorer/src/main.rs | 51 +++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/explorer/src/main.rs b/explorer/src/main.rs index e8fca2f..fedd11c 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -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 { +fn respond(line: &str, state: &mut State) -> Result { 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::("piece").unwrap(); @@ -71,16 +87,26 @@ fn respond(line: &str, pos: &mut Position) -> Result { 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; } }