#![allow(dead_code)] /// Manage UCI messages and link into chess component mod uci_command; use log::warn; use std::io::*; use crate::uci_command::*; pub struct UciEngine { source: Fi, destination: Fo, uciok: bool, id: Id, } impl UciEngine { pub fn new(source: Fi, destination: Fo) -> UciEngine { UciEngine::{ source, destination, id: Id::new(), uciok: false} } pub fn name(&self) -> Option { self.id.name() } pub fn author(&self) -> Option { self.id.author() } pub fn update(&mut self, id: &Id){ if self.is_uciok() {warn!("Engine info should not be updated now (uciok)");} self.id.update(id); } pub fn update_from_str(&mut self, name: Option<&str>, author: Option<&str>){ if self.is_uciok() {warn!("Engine info should not be updated now (uciok)");} self.id.update_from_str(name, author); } pub fn uciok(&mut self){ self.uciok = true; } pub fn is_uciok(&self) -> bool { self.uciok } /// Parse UCI commands /// /// * id /// * name *x* /// /// this must be sent after receiving the "uci" command to identify the engine, /// e.g. "id name Shredder X.Y\n" /// * author *x* /// /// this must be sent after receiving the "uci" command to identify the engine, /// e.g. "id author Stefan MK\n" /// /// ```rust /// use chess_uci::*; /// use std::io; /// let mut engine = UciEngine::new(io::empty(), io::sink()); /// /// engine.exec("id name Baptiste"); /// assert_eq!(engine.name().expect(""), "Baptiste"); /// /// engine.exec("id author Monique Jouve"); /// assert_eq!(engine.author().expect(""), "Monique Jouve"); /// /// ``` /// /// * uciok /// /// Must be sent after the id and optional options to tell the GUI that the engine /// has sent all infos and is ready in uci mode. /// /// ```rust /// use chess_uci::*; /// use std::io; /// let mut engine = UciEngine::new(io::empty(), io::sink()); /// assert_eq!(engine.is_uciok(), false); /// /// engine.exec("id name Baptiste"); /// assert_eq!(engine.name().expect(""), "Baptiste"); /// assert_eq!(engine.is_uciok(), false); /// /// engine.exec("id author Monique Jouve"); /// assert_eq!(engine.author().expect(""), "Monique Jouve"); /// assert_eq!(engine.is_uciok(), false); /// /// engine.exec(" uciok"); /// assert_eq!(engine.is_uciok(), true); /// ``` pub fn exec(&mut self, command: &str){ match parse (&mut command.to_string()) { Ok(EngineCommand::Id{id}) => self.update(&id), Ok(EngineCommand::UciOk) => self.uciok(), Ok(_) => {unimplemented!("command not implemented")}, Err(_) => warn!("Not a command"), } } pub fn next(&mut self) { exec(self.source.read_line()); } }