Crates.io | uci-parser |
lib.rs | uci-parser |
version | 0.2.0 |
source | src |
created_at | 2024-10-03 19:17:08.858454 |
updated_at | 2024-10-17 19:54:33.313887 |
description | Universal Chess Interface parser |
homepage | https://github.com/dannyhammer/uci-parser |
repository | https://github.com/dannyhammer/uci-parser |
max_upload_size | |
id | 1395598 |
size | 102,919 |
This crate contains types and functions for communicating with chess engines and chess GUIs through the Universal Chess Interface (UCI) protocol.
The primary function of this crate is to provide well-typed representations for every message/command described in the UCI protocol, along with an easy-to-use API for converting between these well-typed representations and strings.
The simplest use case is parsing commands like uci
, which is as easy as it sounds:
use uci_parser::UciCommand;
let cmd = UciCommand::new("uci").unwrap();
assert_eq!(cmd, UciCommand::Uci);
Commands implement FromStr
, so you can call .parse()
:
use uci_parser::UciCommand;
// Arbitrary whitespace is handled appropriately
let cmd = "setoption name \n Threads value \t 16".parse::<UciCommand>();
assert!(cmd.is_ok());
assert_eq!(
cmd.unwrap(),
UciCommand::SetOption {
name: "Threads".to_string(),
value: Some("16".to_string())
}
);
Commands that have many optional arguments, like go
, implement Default
so they can be parsed cleanly:
use std::time::Duration;
use uci_parser::{UciCommand, UciSearchOptions};
let cmd = "go movetime 42".parse::<UciCommand>();
assert!(cmd.is_ok());
assert_eq!(
cmd.unwrap(),
UciCommand::Go(UciSearchOptions {
// Times are provided as Durations
movetime: Some(Duration::from_millis(42)),
..Default::default()
})
);
How edge cases should be handled is a delicate subject and the correct answer depends on the needs of your engine. Rather than enforce my own opinion on handling those edge cases, I've marked them as crate features.
parse-go-perft
: Adds support for parsing perft <depth>
as an argument to the go
command.
parse-bench
: Adds support for parsing the string bench
into UciCommand::Bench
.
bench
are the same as the arguments to go
, since both commands involve running searches.parse-position-kiwipete
: Adds support to parse kiwipete
as a special argument to position
(similar to startpos
).
position kiwipete
will be equivalent to parsing position fen r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
, so the fen
field of Position
will be Some(<kiwipete fen>)
.validate-promotion-moves
: Restricts the grammar when parsing moves in UCI notation to ensure that promotions are valid:
[a-h][1-8][a-h][1-8][pnbrqk]
, which will parse e2e4p
successfully, even though it doesn't "make sense" in-game. With this feature enabled, the grammar restricts to: [a-h][1-8][a-h][1-8] | [a-h]7[a-h]8[qnrb] | [a-h]2[a-h]1[qnrb]
. This means that only moves from the penultimate ranks to the final ranks will be parsed as promotions, and the only valid pieces for a promotion are a Queen, Knight, Rook, or Bishop.clamp-negatives
: Clamps negative numbers to 0
when parsing.
go wtime -80
. This is normally not a problem,go movetime -42
, this crate will parse that as a Duration
of 0
milliseconds. It is up to your engine to determine how to respond to these situations.err-on-unused-input
: Causes the parser to fail if the input text was not fully consumed during parsing.
joho debug on
parses to debug on
). Unknown tokens encountered while parsing a specific command will generate errors (debug joho on
fails). Unknown tokens after a command are, by default, ignored (debug on joho
parses to debug on
). If this feature is enabled, the parser will fail if all tokens were not consumed during parsing (debug on joho
will fail).types
: Exposes several well-typed representations of UCI components, such as moves.
position startpos moves e2e4
will yield a list of String
s for all parsed moves
, leaving you to have to re-parse them in your engine later. If this feature is enabled, any String
that is parsed as a move will be converted to [UciMove
], which contains types representing the files, ranks, squares, and pieces involved in each move.types
] module for more information.