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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use anyhow::Result;
use std::{collections::HashMap, fmt::Display, str::FromStr};
use thiserror::Error;

/// Supported UCI commands
#[derive(PartialEq, Debug)]
pub enum UCI {
    /// Sent after the 'uci' command
    UciOk,

    /// Sent after the 'isready' command
    ReadyOk,

    /// Engine sending info to GUI
    Info {
        cp: Option<isize>,
        mate: Option<isize>,
        depth: Option<isize>,
        seldepth: Option<isize>,
        nodes: Option<isize>,
        time: Option<isize>,
        multipv: Option<isize>,
        pv: Option<Vec<String>>,
    },

    /// Options can be set to modify the engine behaviour
    Option { name: String, opt_type: OptionType },
}

/// Possible types for Engine Options
#[derive(PartialEq, Debug, Clone)]
pub enum OptionType {
    Check {
        default: bool,
    },
    Spin {
        default: isize,
        min: isize,
        max: isize,
    },
    Combo {
        default: String,
        options: Vec<String>,
    },
    Button,
    String {
        default: String,
    },
}

impl OptionType {
    fn new(opt_type: String, line: String) -> Result<Self> {
        Ok(match opt_type.as_str() {
            "check" => OptionType::new_check(line)?,
            "spin" => OptionType::new_spin(line)?,
            "combo" => OptionType::new_combo(line)?,
            "button" => OptionType::new_button()?,
            "string" => OptionType::new_string(line)?,
            _ => return Err(UCIError::ParseError.into()),
        })
    }

    fn new_check(line: String) -> Result<Self> {
        let words = vec!["default"];
        let values = parse_line_values(line, words)?;
        Ok(OptionType::Check {
            default: values["default"].unwrap(),
        })
    }

    fn new_spin(line: String) -> Result<Self> {
        let words = vec!["default", "min", "max"];
        let values = parse_line_values(line, words)?;
        Ok(OptionType::Spin {
            default: values["default"].unwrap(),
            min: values["min"].unwrap(),
            max: values["max"].unwrap(),
        })
    }

    fn new_combo(line: String) -> Result<Self> {
        let words = vec!["default"];
        let values = parse_line_values(line.clone(), words)?;
        let line: Vec<&str> = line.split_whitespace().collect();
        let mut options = Vec::new();
        // TODO: Check if combo options can have spaces, in which case this will give incorrect results
        for ix in 0..line.len() {
            if line[ix] == "var" {
                options.push(line[ix + 1].to_string());
            }
        }
        Ok(OptionType::Combo {
            default: values["default"].clone().unwrap(),
            options: options,
        })
    }

    fn new_button() -> Result<Self> {
        Ok(OptionType::Button)
    }

    fn new_string(line: String) -> Result<Self> {
        let words = vec!["default"];
        let values = parse_line_values(line, words)?;
        Ok(OptionType::String {
            default: values["default"].clone().unwrap(),
        })
    }
}

/// Errors produced from UCI parsing
#[derive(Error, Debug)]
pub enum UCIError {
    /// Error parsing a UCI command
    ParseError,
}

impl Display for UCIError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let data = match self {
            UCIError::ParseError => "error parsing uci command",
        };
        return f.write_str(data);
    }
}

/// Parse an UCI command
pub fn parse_uci(line: String) -> Result<UCI> {
    let line = line.trim().to_string();
    let command = line.split_whitespace().next().unwrap_or("");
    match command {
        "info" => parse_info_line(line),
        "uciok" => Ok(UCI::UciOk),
        "readyok" => Ok(UCI::ReadyOk),
        "option" => parse_option_line(line),
        _ => Err(UCIError::ParseError.into()),
    }
}

/// parse_line_values parses the value following each word in the given line.
fn parse_line_values<T: FromStr + Default>(
    line: String,
    words: Vec<&str>,
) -> Result<HashMap<String, Option<T>>> {
    let line: Vec<&str> = line.split_whitespace().collect();
    let mut values = HashMap::with_capacity(words.len());
    for word in words.iter() {
        let mut i = line.iter();
        let value = match i.position(|x: &&str| x == word) {
            Some(ix) => match line.get(ix + 1) {
                Some(v) => v.parse::<T>().ok(),
                None => Some(T::default()),
            },
            None => None,
        };
        values.insert(word.to_string(), value);
    }
    Ok(values)
}

/// Parse an info line for all supported metadata
fn parse_info_line(line: String) -> Result<UCI> {
    let words = vec![
        "cp", "depth", "nodes", "seldepth", "mate", "time", "multipv",
    ];
    let values = parse_line_values(line.clone(), words)?;
    return Ok(UCI::Info {
        cp: values["cp"],
        mate: values["mate"],
        depth: values["depth"],
        nodes: values["nodes"],
        time: values["time"],
        multipv: values["multipv"],
        seldepth: values["seldepth"],
        pv: parse_pv(line),
    });
}

/// Parse an info line and return all the moves stated after 'pv'
fn parse_pv(line: String) -> Option<Vec<String>> {
    let line: Vec<&str> = line.split_whitespace().collect();
    let mut pv = Vec::new();
    let mut i = line.iter();
    match i.position(|x: &&str| *x == "pv") {
        Some(_) => {}
        None => return None, // early return if no pv is found
    };
    while let Some(word) = i.next() {
        pv.push(word.to_string());
    }
    Some(pv)
}

fn parse_option_line(line: String) -> Result<UCI> {
    // FIXME: handle `name`s with spaces (i.e. `option name Clear Hash type button`)
    let words = vec!["name", "type"];
    let values = parse_line_values(line.clone(), words)?;
    return Ok(UCI::Option {
        name: values["name"].clone().unwrap(),
        opt_type: OptionType::new(values["type"].clone().unwrap(), line)?,
    });
}

#[cfg(test)]
mod test {

    use crate::parse::{parse_info_line, UCI};
    use anyhow::Result;

    macro_rules! test_info_line {
        ($line:expr, $ev:expr) => {
            let ev = parse_info_line($line.to_string())?;
            assert_eq!(ev, $ev);
        };
    }

    #[tokio::test]
    async fn test_parse_info_line() -> Result<()> {
        test_info_line!("info depth 1 seldepth 1 multipv 1 score cp 59 nodes 56 nps 56000 hashfull 0 tbhits 0 time 1", 
            UCI::Info {
                cp: Some(59),
                mate: None,
                depth: Some(1),
                nodes: Some(56),
                seldepth: Some(1),
                multipv: Some(1),
                time: Some(1),
                pv: None,
            }
        );
        test_info_line!("info depth 1 seldepth 1 multipv 1 score cp 59 nodes 56 nps 56000 hashfull 0 tbhits 0 time 1 pv d6f4 e3f4", 
            UCI::Info {
                cp: Some(59),
                mate: None,
                depth: Some(1),
                nodes: Some(56),
                seldepth: Some(1),
                multipv: Some(1),
                time: Some(1),
                pv: Some(vec!["d6f4".to_string(), "e3f4".to_string()]),
            }
        );
        test_info_line!(
            "info depth 2 seldepth 2 multipv 1 score cp -27 nodes 227 nps 227000 hashfull 0 tbhits 0 time 1 pv a8b8 f4d6",
            UCI::Info {
                cp: Some(-27),
                mate: None,
                depth: Some(2),
                nodes: Some(227),
                seldepth: Some(2),
                multipv: Some(1),
                time: Some(1),
                pv: Some(vec!["a8b8".to_string(), "f4d6".to_string()]),
            }
        );
        test_info_line!(
            "info depth 24 seldepth 33 multipv 1 score cp -195 nodes 2499457 nps 642203 hashfull 812 tbhits 0 time 3892 pv d8a5 a4a5 c6a5 f4d6 b7a6 d6c5 f6d7 c5a3 f7f6 e1g1 a8c8 b2b3 e8f7 f1c1 d7b6 f3e1 f5g6 f2f3 h8d8 e3e4 a5c6 e1d3 e6e5 d3c5 d5e4 d2e4 g6e4 c5e4",
            UCI::Info {
                cp: Some(-195),
                mate: None,
                depth: Some(24),
                nodes: Some(2499457),
                seldepth: Some(33),
                multipv: Some(1),
                time: Some(3892),
                pv: Some(vec![
                    "d8a5".to_string(),
                    "a4a5".to_string(),
                    "c6a5".to_string(),
                    "f4d6".to_string(),
                    "b7a6".to_string(),
                    "d6c5".to_string(),
                    "f6d7".to_string(),
                    "c5a3".to_string(),
                    "f7f6".to_string(),
                    "e1g1".to_string(),
                    "a8c8".to_string(),
                    "b2b3".to_string(),
                    "e8f7".to_string(),
                    "f1c1".to_string(),
                    "d7b6".to_string(),
                    "f3e1".to_string(),
                    "f5g6".to_string(),
                    "f2f3".to_string(),
                    "h8d8".to_string(),
                    "e3e4".to_string(),
                    "a5c6".to_string(),
                    "e1d3".to_string(),
                    "e6e5".to_string(),
                    "d3c5".to_string(),
                    "d5e4".to_string(),
                    "d2e4".to_string(),
                    "g6e4".to_string(),
                    "c5e4".to_string(),
                ]),
            }
        );
        Ok(())
    }
}