Crate usi

source · []
Expand description

A module for working with USI protocol in a type-safe way.

USI protocol defines commands sent from either GUIs or engines. Detail about USI protocol can be found at

Data types representing commands defined in USI protocol.

GuiCommand and EngineCommand represents input/output commands defined in the protocol.


use std::time::Duration;
use usi::{GuiCommand, ThinkParams, EngineCommand, BestMoveParams};

// GuiCommand can be converted into the USI compliant string.
let params = ThinkParams::new().btime(Duration::from_secs(1)).wtime(Duration::from_secs(2));
let cmd = GuiCommand::Go(params);
assert_eq!("go btime 1000 wtime 2000", cmd.to_string());

// EngineCommand can be parsed from the command string sent from the USI engine.
let cmd = EngineCommand::parse("bestmove 7g7f ponder 8c8d").unwrap();
match cmd {
    EngineCommand::BestMove(BestMoveParams::MakeMove(ref m, Some(ref pm))) => {
        assert_eq!("7g7f", *m);
        assert_eq!("8c8d", *pm);
    _ => unreachable!(),

Working with a USI engine process

UsiEngineHandler can be used to spawn the USI engine process. You can send GuiCommands and receive EngineCommand.


use usi::{BestMoveParams, Error, EngineCommand, GuiCommand, UsiEngineHandler};

let mut handler = UsiEngineHandler::spawn("/path/to/usi_engine", "/path/to/working_dir").unwrap();

// Get the USI engine information.
let info = handler.get_info().unwrap();
assert_eq!("engine name",;

// Set options.
handler.send_command(&GuiCommand::SetOption("USI_Ponder".to_string(), Some("true".to_string()))).unwrap();

// Start listening to the engine output.
// You can pass the closure which will be called
//   everytime new command is received from the engine.
handler.listen(move |output| -> Result<(), Error> {
    match output.response() {
                     ref best_move_sfen,
                     ref ponder_move,
                ))) => {
                    assert_eq!("5g5f", best_move_sfen);
        _ => {}


EngineCommandReader<R> produces a structured output from a reader.

Represents a metadata returned from a USI engine.

A struct to represent each output produced from a USI engine process.

GuiCommandWriter<W> converts GuiCommands and writes strings into the writer.

Represents parameters of “option” command.

Represents parameters of “go” command.

UsiEngineHandler provides a type-safe interface to the USI engine process.


Represents parameters of “bestmove” command.

Represents parameters of “checkmate” command.

Represents a USI command sent from the engine.

Represents parameters of “gameover” command.

Represents a USI command sent from the GUI.

Represents parameters of “id” command.

Represents parameters of “info” command.

Represents a kind of “option” command value.

Represents a kind of “score” parameter value in “info” command.