diff --git a/chess_uci/src/lib.rs b/chess_uci/src/lib.rs index 46b921b..26a4ce7 100644 --- a/chess_uci/src/lib.rs +++ b/chess_uci/src/lib.rs @@ -140,31 +140,34 @@ impl UciEngine { /// /// if `EngineCommand::Id`: update name or authorship of the engine /// if `EngineCommand::UciOk`: end engine initialization phase - pub fn exec(&mut self, command: &str){ - match parse (&mut command.to_string()) { + pub fn exec(&mut self, command: &str) -> std::result::Result { + let uci_command = parse (&mut command.to_string()); + match uci_command.clone() { Ok(EngineCommand::Id{id}) => self.update(&id), Ok(EngineCommand::UciOk) => self.uciok(), Ok(EngineCommand::Opt { options: _ }) => {}, Ok(_) => {unimplemented!("command not implemented")}, Err(_) => warn!("Not a command"), - } + }; + uci_command } /// Retrieve a line from the uci engine input stream and parse it - pub fn pull(&mut self) { + pub fn pull(&mut self) -> Option { if let Some(source) = &mut self.source { let mut command = String::new(); match source.read_line(&mut command) { - Ok(0) => self.terminate("Chess engine closed connection."), + Ok(0) => {self.terminate("Chess engine closed connection."); None}, Ok(_) => { info!("← {}", command); - self.exec(&command) + self.exec(&command).ok() }, - Err(reason) => warn!("Unable to read from engine: {reason}"), + Err(reason) => {warn!("Unable to read from engine: {reason}"); None}, } } else { info!("❌ No connected uci engine"); + None } } diff --git a/chess_uci/src/uci_command.rs b/chess_uci/src/uci_command.rs index 9e76bf1..d702eed 100644 --- a/chess_uci/src/uci_command.rs +++ b/chess_uci/src/uci_command.rs @@ -6,6 +6,7 @@ use chess::{ChessMove, Board, Action}; mod uci_command{} +#[derive(Clone)] pub struct Id {name: Option, author: Option} impl Id { pub fn new() -> Id { @@ -30,11 +31,12 @@ impl Id { self.author.clone() } } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Info {} -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Opt{} +#[derive(Clone)] pub enum EngineCommand { Id{id: Id}, UciOk,