push commands

This commit is contained in:
Baptiste Fouques 2023-01-20 17:17:58 +01:00
parent 4aa7ee9f2e
commit 5dc2457a15
2 changed files with 14 additions and 9 deletions

View File

@ -140,31 +140,34 @@ impl<Fi: Read, Fo: Write> UciEngine<Fi, Fo> {
/// ///
/// if `EngineCommand::Id`: update name or authorship of the engine /// if `EngineCommand::Id`: update name or authorship of the engine
/// if `EngineCommand::UciOk`: end engine initialization phase /// if `EngineCommand::UciOk`: end engine initialization phase
pub fn exec(&mut self, command: &str){ pub fn exec(&mut self, command: &str) -> std::result::Result<EngineCommand, &'static str> {
match parse (&mut command.to_string()) { let uci_command = parse (&mut command.to_string());
match uci_command.clone() {
Ok(EngineCommand::Id{id}) => self.update(&id), Ok(EngineCommand::Id{id}) => self.update(&id),
Ok(EngineCommand::UciOk) => self.uciok(), Ok(EngineCommand::UciOk) => self.uciok(),
Ok(EngineCommand::Opt { options: _ }) => {}, Ok(EngineCommand::Opt { options: _ }) => {},
Ok(_) => {unimplemented!("command not implemented")}, Ok(_) => {unimplemented!("command not implemented")},
Err(_) => warn!("Not a command"), Err(_) => warn!("Not a command"),
} };
uci_command
} }
/// Retrieve a line from the uci engine input stream and parse it /// Retrieve a line from the uci engine input stream and parse it
pub fn pull(&mut self) { pub fn pull(&mut self) -> Option<EngineCommand> {
if let Some(source) = &mut self.source { if let Some(source) = &mut self.source {
let mut command = String::new(); let mut command = String::new();
match source.read_line(&mut command) { match source.read_line(&mut command) {
Ok(0) => self.terminate("Chess engine closed connection."), Ok(0) => {self.terminate("Chess engine closed connection."); None},
Ok(_) => { Ok(_) => {
info!("← {}", command); 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 { } else {
info!("❌ No connected uci engine"); info!("❌ No connected uci engine");
None
} }
} }

View File

@ -6,6 +6,7 @@ use chess::{ChessMove, Board, Action};
mod uci_command{} mod uci_command{}
#[derive(Clone)]
pub struct Id {name: Option<String>, author: Option<String>} pub struct Id {name: Option<String>, author: Option<String>}
impl Id { impl Id {
pub fn new() -> Id { pub fn new() -> Id {
@ -30,11 +31,12 @@ impl Id {
self.author.clone() self.author.clone()
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Info {} pub struct Info {}
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Opt{} pub struct Opt{}
#[derive(Clone)]
pub enum EngineCommand { pub enum EngineCommand {
Id{id: Id}, Id{id: Id},
UciOk, UciOk,