usi/
lib.rs

1//! A module for working with USI protocol in a type-safe way.
2//!
3//! USI protocol defines commands sent from either GUIs or engines.
4//! Detail about USI protocol can be found at <http://www.geocities.jp/shogidokoro/usi.html>.
5//!
6//! # Data types representing commands defined in USI protocol.
7//!
8//! `GuiCommand` and `EngineCommand` represents input/output commands defined in the protocol.
9//!
10//! # Examples
11//!
12//! ```
13//! use std::time::Duration;
14//! use usi::{GuiCommand, ThinkParams, EngineCommand, BestMoveParams};
15//!
16//! // GuiCommand can be converted into the USI compliant string.
17//! let params = ThinkParams::new().btime(Duration::from_secs(1)).wtime(Duration::from_secs(2));
18//! let cmd = GuiCommand::Go(params);
19//! assert_eq!("go btime 1000 wtime 2000", cmd.to_string());
20//!
21//! // EngineCommand can be parsed from the command string sent from the USI engine.
22//! let cmd = EngineCommand::parse("bestmove 7g7f ponder 8c8d").unwrap();
23//! match cmd {
24//!     EngineCommand::BestMove(BestMoveParams::MakeMove(ref m, Some(ref pm))) => {
25//!         assert_eq!("7g7f", *m);
26//!         assert_eq!("8c8d", *pm);
27//!     },
28//!     _ => unreachable!(),
29//! }
30//! ```
31//!
32//! # Working with a USI engine process
33//!
34//! `UsiEngineHandler` can be used to spawn the USI engine process.
35//! You can send `GuiCommand`s and receive `EngineCommand`.
36//!
37//! # Examples
38//! ```no_run
39//! use usi::{BestMoveParams, Error, EngineCommand, GuiCommand, UsiEngineHandler};
40//!
41//! let mut handler = UsiEngineHandler::spawn("/path/to/usi_engine", "/path/to/working_dir").unwrap();
42//!
43//! // Get the USI engine information.
44//! let info = handler.get_info().unwrap();
45//! assert_eq!("engine name", info.name());
46//!
47//! // Set options.
48//! handler.send_command(&GuiCommand::SetOption("USI_Ponder".to_string(), Some("true".to_string()))).unwrap();
49//! handler.prepare().unwrap();
50//! handler.send_command(&GuiCommand::UsiNewGame).unwrap();
51//!
52//! // Start listening to the engine output.
53//! // You can pass the closure which will be called
54//! //   everytime new command is received from the engine.
55//! handler.listen(move |output| -> Result<(), Error> {
56//!     match output.response() {
57//!         Some(EngineCommand::BestMove(BestMoveParams::MakeMove(
58//!                      ref best_move_sfen,
59//!                      ref ponder_move,
60//!                 ))) => {
61//!                     assert_eq!("5g5f", best_move_sfen);
62//!                 }
63//!         _ => {}
64//!     }
65//!     Ok(())
66//! }).unwrap();
67//! handler.send_command(&GuiCommand::Usi).unwrap();
68//! ```
69mod error;
70mod process;
71mod protocol;
72
73pub use self::error::*;
74pub use self::process::*;
75pub use self::protocol::*;