1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::time::Duration;

use super::parser::EngineCommandParser;
use crate::error::Error;

/// Represents a kind of "option" command value.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum OptionKind {
    Check {
        default: Option<bool>,
    },
    Spin {
        default: Option<i32>,
        min: Option<i32>,
        max: Option<i32>,
    },
    Combo {
        default: Option<String>,
        vars: Vec<String>,
    },
    Button {
        default: Option<String>,
    },
    String {
        default: Option<String>,
    },
    Filename {
        default: Option<String>,
    },
}

/// Represents parameters of "option" command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct OptionParams {
    pub name: String,
    pub value: OptionKind,
}

/// Represents a kind of "score" parameter value in "info" command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum ScoreKind {
    CpExact,
    CpLowerbound,
    CpUpperbound,
    MateExact,
    MateSignOnly,
    MateLowerbound,
    MateUpperbound,
}

/// Represents parameters of "info" command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum InfoParams {
    CurrMove(String),
    Depth(i32, Option<i32>),
    HashFull(i32),
    MultiPv(i32),
    Nodes(i32),
    Nps(i32),
    Pv(Vec<String>),
    Score(i32, ScoreKind),
    Text(String),
    Time(Duration),
}

/// Represents parameters of "checkmate" command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum CheckmateParams {
    Mate(Vec<String>),
    NoMate,
    NotImplemented,
    Timeout,
}

/// Represents parameters of "bestmove" command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum BestMoveParams {
    MakeMove(String, Option<String>),
    Resign,
    Win,
}

/// Represents parameters of "id" command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum IdParams {
    Name(String),
    Author(String),
}

/// Represents a USI command sent from the engine.
///
/// # Examples
///
/// ```
/// use usi::{EngineCommand, BestMoveParams};
///
/// 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!(),
/// }
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum EngineCommand {
    Id(IdParams),
    BestMove(BestMoveParams),
    Checkmate(CheckmateParams),
    Info(Vec<InfoParams>),
    Option(OptionParams),
    ReadyOk,
    UsiOk,
    Unknown,
}

impl EngineCommand {
    /// Parses a USI command string into a new instance of `EngineCommand`.
    pub fn parse(cmd: &str) -> Result<EngineCommand, Error> {
        let parser = EngineCommandParser::new(cmd);
        parser.parse()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse() {
        let ok_cases = [
            "id name Lesserkai",
            "id author Program Writer",
            "bestmove 7g7f",
            "bestmove 8h2b+ ponder 3a2b",
            "bestmove resign",
            "bestmove win",
            "checkmate nomate",
            "checkmate notimplemented",
            "checkmate timeout",
            "checkmate G*8f 9f9g 8f8g 9g9h 8g8h",
            "info time 1141 depth 3 seldepth 5 nodes 135125 score cp -1521 pv 3a3b L*4h 4c4d",
            "info nodes 120000 nps 116391 multipv 1 currmove 1 hashfull 104",
            "info string 7g7f (70%)",
            "info score cp 100 lowerbound",
            "info score cp 100 upperbound",
            "info score mate +",
            "info score mate -",
            "info score mate 5",
            "info score mate -5",
            "info score mate 5 lowerbound",
            "info score mate 5 upperbound",
            "option name UseBook type check default true",
            "option name Selectivity type spin default 2 min 0 max 4",
            "option name Style type combo default Normal var Solid var Normal var Risky",
            "option name ResetLearning type button",
            "option name BookFile type string default public.bin",
            "option name LearningFile type filename default <empty>",
            "readyok",
            "usiok",
            "unknown command",
        ];

        let ng_cases = [
            "",
            "checkmate",
            "id foo bar",
            "info depth foo",
            "info depth 1 seldepth foo",
            "info multipv foo",
            "info score foo 1",
            "info foo bar",
            "option foo bar baz",
            "option name foo bar",
        ];

        for (i, c) in ok_cases.iter().enumerate() {
            assert!(EngineCommand::parse(c).is_ok(), "failed at #{}", i);
        }

        for (i, c) in ng_cases.iter().enumerate() {
            assert!(EngineCommand::parse(c).is_err(), "failed at #{}", i);
        }
    }
}