ruci 2.1.0

A UCI (Universal Chess Interface) crate.
Documentation
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
extern crate alloc;

use alloc::borrow::Cow;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use alloc::boxed::Box;
use core::fmt::{Display, Formatter, Write};
use shakmaty::Color;
use shakmaty::uci::UciMove;
use crate::{parsing, uci_moves, OptionReplaceIf};
use crate::engine::pointers::InfoParameterPointer;
use crate::dev_macros::{from_str_parts, impl_message};
use super::{pointers, traits};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// <https://backscattering.de/chess/uci/#engine-info-depth>
pub struct Depth {
    /// <https://backscattering.de/chess/uci/#engine-info-depth>
    pub depth: usize,
    /// <https://backscattering.de/chess/uci/#engine-info-seldepth>
    pub seldepth: Option<usize>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ScoreBound {
    /// <https://backscattering.de/chess/uci/#engine-info-score-lowerbound>
    LowerBound,
    /// <https://backscattering.de/chess/uci/#engine-info-score-upperbound>
    UpperBound,
}

/// Player advantage/disadvantage from the engine's perspective.
/// 
/// <https://backscattering.de/chess/uci/#engine-info-score>
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Score {
    /// <https://backscattering.de/chess/uci/#engine-info-score-centipawns>
    Centipawns(isize),
    /// <https://backscattering.de/chess/uci/#engine-info-score-mate>
    MateIn(isize),
}

impl Score {
    /// Visit [`ScoreStandardized`] to see what this does.
    #[allow(clippy::arithmetic_side_effects)]
    pub const fn standardized(self, turn: Color) -> ScoreStandardized {
        match (turn, self) {
            (Color::White, Self::Centipawns(centipawns)) => ScoreStandardized(Self::Centipawns(centipawns)),
            (Color::Black, Self::Centipawns(centipawns)) => ScoreStandardized(Self::Centipawns(-centipawns)),
            (Color::White, Self::MateIn(mate_in)) => ScoreStandardized(Self::MateIn(mate_in)),
            (Color::Black, Self::MateIn(mate_in)) => ScoreStandardized(Self::MateIn(-mate_in)),
        }
    }
}

/// This struct represents a "standardized" score (read below).
///
/// Engines (should) return a [`Score`] dependent on whose turn it is to move.
/// [`ScoreStandardized`] is a score *independent* of whose turn it is to move.
/// So, `x` means that *white* has an advantage of `x`, `-x` means that *black* has an advantage of `x`.
///
/// Get this by calling [`Score::standardized`].
/// Note that you need to specify whose turn it is to move.
///
/// **⚠️** While the UCI protocol does say engines should score from their point of view,
/// you should verify this is the case with the engine you're using.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ScoreStandardized(Score);

impl ScoreStandardized {
    #[allow(clippy::arithmetic_side_effects)]
    #[doc(hidden)]
    #[deprecated(since = "0.8.1", note = "use Score::standardized instead")]
    pub const fn from_score(score: Score, turn: Color) -> Self {
        score.standardized(turn)
    }

    pub const fn score(self) -> Score {
        self.0
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// <https://backscattering.de/chess/uci/#engine-info-score>
pub struct ScoreWithBound {
    pub kind: Score,
    /// <https://backscattering.de/chess/uci/#engine-info-score-lowerbound>
    /// <https://backscattering.de/chess/uci/#engine-info-score-upperbound>
    pub bound: Option<ScoreBound>,
}

impl ScoreWithBound {
    fn from_str(s: &str) -> Option<Self> {
        fn isize_at_plus1_position(split: &[&str], position: Option<usize>) -> Option<isize> {
            position.and_then(|position| {
                if position == usize::MAX {
                    None
                } else {
                    // CLIPPY: Potential overflow handled in this if statement
                    #[allow(clippy::arithmetic_side_effects)]
                    split
                        .get(position + 1)
                        .and_then(|s| s.parse().ok())
                }
            })
        }

        let split = s.split(' ').collect::<Vec<_>>();
        let centipawns_position = split.iter().position(|&part| part == "cp");
        let mate_in_position = split.iter().position(|&part| part == "mate");
        let is_lowerbound = split.contains(&"lowerbound");
        let is_upperbound = split.contains(&"upperbound");
        let centipawns = isize_at_plus1_position(&split, centipawns_position);
        let mate_in = isize_at_plus1_position(&split, mate_in_position);
        let kind = centipawns.map_or(
            mate_in.map(Score::MateIn),
            |centipawns| Some(Score::Centipawns(centipawns))

        )?;

        Some(Self {
            kind,
            bound: if is_lowerbound && is_upperbound {
                None
            } else if is_lowerbound {
                Some(ScoreBound::LowerBound)
            } else if is_upperbound {
                Some(ScoreBound::UpperBound)
            } else {
                None
            },
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// <https://backscattering.de/chess/uci/#engine-info-refutation>
pub struct Refutation<'a> {
    pub refuted_move: UciMove,
    pub refutation: Cow<'a, [UciMove]>,
}

impl Refutation<'static> {
    fn from_str(s: &str) -> Option<Self> {
        let mut moves = uci_moves::from_str(s);

        if moves.is_empty() {
            return None;
        }

        let first = moves.remove(0);

        Some(Self {
            refuted_move: first,
            refutation: Cow::Owned(moves),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// <https://backscattering.de/chess/uci/#engine-info-currline>
pub struct CurrLine<'a> {
    pub used_cpu: Option<usize>,
    pub line: Cow<'a, [UciMove]>,
}

impl CurrLine<'static> {
    fn from_str(s: &str) -> Option<Self> {
        // TODO: This will fail if there's more whitespace in between.
        // Fix in future
        let (used_cpu, line) = s.split_once(' ')?;
        let Ok(used_cpu) = used_cpu.parse() else {
            return None;
        };

        Some(Self {
            used_cpu: Some(used_cpu),
            line: Cow::Owned(uci_moves::from_str(line)),
        })
    }
}

/// Information about the engine's calculation.
///
/// Sent (probably multiple times) after [`Go`](crate::gui::Go).
///
/// <https://backscattering.de/chess/uci/#engine-info>
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Info<'a> {
    /// <https://backscattering.de/chess/uci/#engine-info-depth>
    pub depth: Option<Depth>,
    /// <https://backscattering.de/chess/uci/#engine-info-time>
    pub time: Option<usize>,
    /// <https://backscattering.de/chess/uci/#engine-info-nodes>
    pub nodes: Option<usize>,
    /// Primary variation.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-pv>
    pub pv: Cow<'a, [UciMove]>,
    /// Multi primary variation.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-multipv>
    pub multi_pv: Option<usize>,
    /// <https://backscattering.de/chess/uci/#engine-info-score>
    pub score: Option<ScoreWithBound>,
    /// Current move.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-currmove>
    pub curr_move: Option<UciMove>,
    /// Current move number.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-currmovenumber>
    pub curr_move_number: Option<usize>,
    /// <https://backscattering.de/chess/uci/#engine-info-hashfull>
    pub hash_full: Option<usize>,
    /// Nodes per second.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-nps>
    pub nps: Option<usize>,
    /// Tablebase hits.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-tbhits>
    pub tb_hits: Option<usize>,
    /// Shredderbase hits.
    ///
    /// <https://backscattering.de/chess/uci/#engine-info-sbhits>
    pub sb_hits: Option<usize>,
    /// <https://backscattering.de/chess/uci/#engine-info-cpuload>
    pub cpu_load: Option<usize>,
    /// <https://backscattering.de/chess/uci/#engine-info-string>
    pub string: Option<Cow<'a, str>>,
    /// <https://backscattering.de/chess/uci/#engine-info-refutation>
    pub refutation: Option<Refutation<'a>>,
    /// <https://backscattering.de/chess/uci/#engine-info-currline>
    pub curr_line: Option<CurrLine<'a>>,
}

impl<'a> From<Info<'a>> for crate::Message<'a> {
    fn from(value: Info<'a>) -> Self {
        Self::Engine(crate::engine::Message::Info(Box::new(value)))
    }
}

impl<'a> From<Info<'a>> for crate::engine::Message<'a> {
    fn from(value: Info<'a>) -> Self {
        Self::Info(Box::new(value))
    }
}

impl_message!(Info<'_>);
from_str_parts!(impl Info<'_> for parts<'p> -> Self {
    let mut this = Self::default();
    // Need to handle depth like this in case the seldepth argument comes before the depth argument.
    let mut depth = None::<usize>;
    let mut seldepth = None::<usize>;
    let mut pv = Vec::new();
    let mut string = String::new();

    let parameter_fn = |parameter, next_parameter: Option<InfoParameterPointer>, value: &str, parts: core::str::Split<'p, char>| {
        match parameter {
            InfoParameterPointer::Depth => depth.replace_if(value.parse().ok()),
            InfoParameterPointer::SelDepth => seldepth.replace_if(value.parse().ok()),
            InfoParameterPointer::Time => this.time.replace_if(value.parse().ok()),
            InfoParameterPointer::Nodes => this.nodes.replace_if(value.parse().ok()),
            InfoParameterPointer::PV => {
                let parsed = uci_moves::from_str(value);

                if !parsed.is_empty() {
                    pv = parsed;
                }
            },
            InfoParameterPointer::MultiPV => this.multi_pv.replace_if(value.parse().ok()),
            InfoParameterPointer::Score => this.score.replace_if(ScoreWithBound::from_str(value)),
            InfoParameterPointer::CurrMove => this.curr_move.replace_if(value.parse().ok()),
            InfoParameterPointer::CurrMoveNumber => this.curr_move_number.replace_if(value.parse().ok()),
            InfoParameterPointer::HashFull => this.hash_full.replace_if(value.parse().ok()),
            InfoParameterPointer::Nps => this.nps.replace_if(value.parse().ok()),
            InfoParameterPointer::TbHits => this.tb_hits.replace_if(value.parse().ok()),
            InfoParameterPointer::SbHits => this.sb_hits.replace_if(value.parse().ok()),
            InfoParameterPointer::CpuLoad => this.cpu_load.replace_if(value.parse().ok()),
            InfoParameterPointer::String => {
                string = value.to_string();
                let size_hint = parts.size_hint();
                string.reserve(size_hint.1.unwrap_or(size_hint.0).saturating_mul(20));

                if let Some(next_parameter) = next_parameter {
                    string.push_str(next_parameter.as_str_spaced());
                }

                let mut first = true;

                for part in parts {
                    if first {
                        first = false;
                    } else {
                        string.push(' ');
                    }

                    string.push_str(part);
                }

                return None;
            },
            InfoParameterPointer::Refutation => this.refutation.replace_if(Refutation::from_str(value)),
            InfoParameterPointer::CurrLine => this.curr_line.replace_if(CurrLine::from_str(value)),
        }

        Some(parts)
    };

    let mut value = String::with_capacity(200);
    parsing::apply_parameters(parts, &mut value, parameter_fn);

    this.pv = Cow::Owned(pv);

    if let Some(depth) = depth {
        this.depth = Some(Depth {
            depth,
            seldepth
        });
    }

    if !string.is_empty() {
        this.string = Some(Cow::Owned(string));
    }

    this
});

impl Display for Info<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_str("info")?;

        if let Some(depth) = &self.depth {
            write!(f, " depth {}", depth.depth)?;

            if let Some(selective_search_depth) = depth.seldepth {
                write!(f, " seldepth {selective_search_depth}")?;
            }
        }

        if let Some(time) = self.time {
            write!(f, " time {time}")?;
        }

        if let Some(nodes) = self.nodes {
            write!(f, " nodes {nodes}")?;
        }

        if !self.pv.is_empty() {
            f.write_str(" pv ")?;
            uci_moves::fmt(&self.pv, f)?;
        }

        if let Some(multi_primary_variation) = self.multi_pv {
            write!(f, " multipv {multi_primary_variation}")?;
        }

        if let Some(score) = &self.score {
            f.write_str(" score")?;

            match score.kind {
                Score::Centipawns(centipawns) => write!(f, " cp {centipawns}")?,
                Score::MateIn(mate_in) => write!(f, " mate {mate_in}")?,
            }

            match score.bound {
                Some(ScoreBound::UpperBound) => {
                    f.write_str(" upperbound")?;
                }
                Some(ScoreBound::LowerBound) => {
                    f.write_str(" lowerbound")?;
                }
                None => {}
            }
        }

        if let Some(current_move) = &self.curr_move {
            write!(f, " currmove {current_move}")?;
        }

        if let Some(current_move_number) = self.curr_move_number {
            write!(f, " currmovenumber {current_move_number}")?;
        }

        if let Some(hash_full) = self.hash_full {
            write!(f, " hashfull {hash_full}")?;
        }

        if let Some(nodes_per_second) = self.nps {
            write!(f, " nps {nodes_per_second}")?;
        }

        if let Some(table_base_hits) = self.tb_hits {
            write!(f, " tbhits {table_base_hits}")?;
        }

        if let Some(shredder_base_hits) = self.sb_hits {
            write!(f, " sbhits {shredder_base_hits}")?;
        }

        if let Some(cpu_load) = self.cpu_load {
            write!(f, " cpuload {cpu_load}")?;
        }

        if let Some(refutation) = &self.refutation {
            write!(f, " refutation {} ", refutation.refuted_move)?;
            uci_moves::fmt(&refutation.refutation, f)?;
        }

        if let Some(current_line) = &self.curr_line {
            f.write_str(" currline")?;

            if let Some(used_cpu) = current_line.used_cpu {
                f.write_char(' ')?;
                f.write_str(&used_cpu.to_string())?;
            }
            
            if !current_line.line.is_empty() {
                f.write_char(' ')?;
                uci_moves::fmt(&current_line.line, f)?;
            }
        }

        if let Some(string) = &self.string {
            write!(f, " string {string}")?;
        }

        Ok(())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use alloc::borrow::Cow;
    use alloc::string::ToString;
    use super::Info;
    use shakmaty::uci::UciMove;
    use pretty_assertions::assert_eq;
    use shakmaty::Color;
    use crate::engine::{CurrLine, Depth, Refutation, Score, ScoreBound, ScoreWithBound};
    use crate::dev_macros::{assert_from_str_message, assert_message_to_from_str, assert_message_to_str};

    #[test]
    fn score_kind_standardize() {
        assert_eq!(
            Score::Centipawns(-20).standardized(Color::White).score(),
            Score::Centipawns(-20)
        );
        assert_eq!(
            Score::Centipawns(-15).standardized(Color::Black).score(),
            Score::Centipawns(15)
        );
        assert_eq!(
            Score::Centipawns(10).standardized(Color::White).score(),
            Score::Centipawns(10)
        );
        assert_eq!(
            Score::Centipawns(5).standardized(Color::Black).score(),
            Score::Centipawns(-5)
        );
    }

    #[test]
    fn to_from_str() {
        let pv: &[UciMove] = &[UciMove::from_ascii(b"e2e4").unwrap(), UciMove::from_ascii(b"c7c5").unwrap()];
        let refutation = [UciMove::from_ascii(b"d7d5").unwrap(), UciMove::from_ascii(b"f1g2").unwrap()];
        let curr_line = [UciMove::from_ascii(b"e2e4").unwrap(), UciMove::from_ascii(b"c7c5").unwrap()];
        
        let m = Info {
            depth: Some(Depth {
                depth: 20,
                seldepth: Some(31)
            }),
            time: Some(12),
            nodes: Some(4),
            pv: Cow::Borrowed(pv),
            multi_pv: Some(1),
            score: Some(ScoreWithBound {
                kind: Score::Centipawns(22),
                bound: Some(ScoreBound::LowerBound),
            }),
            curr_move: Some(UciMove::from_ascii(b"e2e4").unwrap()),
            curr_move_number: None,
            hash_full: None,
            nps: None,
            tb_hits: Some(2),
            sb_hits: None,
            cpu_load: None,
            string: Some(Cow::Borrowed("blabla")),
            refutation: Some(Refutation {
                refuted_move: UciMove::from_ascii(b"g2g4").unwrap(),
                refutation: Cow::Borrowed(&refutation),
            }),
            curr_line: Some(CurrLine {
                used_cpu: Some(1),
                line: Cow::Borrowed(&curr_line),
            }),
        };
        
        assert_message_to_from_str!(engine m, "info depth 20 seldepth 31 time 12 nodes 4 pv e2e4 c7c5 multipv 1 score cp 22 lowerbound currmove e2e4 tbhits 2 refutation g2g4 d7d5 f1g2 currline 1 e2e4 c7c5 string blabla");
    }

    #[test]
    fn to_from_str_bad_parameters() {
        let refutation = [UciMove::from_ascii(b"d7d5").unwrap(), UciMove::from_ascii(b"f1g2").unwrap()];
        let curr_line = [UciMove::from_ascii(b"e2e4").unwrap(), UciMove::from_ascii(b"c7c5").unwrap()];
        
        let m = Info {
            depth: Some(Depth {
                depth: 20,
                seldepth: Some(31)
            }),
            time: Some(12),
            nodes: Some(4),
            pv: Cow::Borrowed(&[]),
            multi_pv: Some(1),
            score: Some(ScoreWithBound {
                kind: Score::Centipawns(22),
                bound: Some(ScoreBound::LowerBound),
            }),
            curr_move: Some(UciMove::from_ascii(b"e2e4").unwrap()),
            curr_move_number: None,
            hash_full: None,
            nps: None,
            tb_hits: Some(4),
            sb_hits: None,
            cpu_load: None,
            string: Some(Cow::Borrowed("blabla")),
            refutation: Some(Refutation {
                refuted_move: UciMove::from_ascii(b"g2g4").unwrap(),
                refutation: Cow::Borrowed(&refutation),
            }),
            curr_line: Some(CurrLine {
                used_cpu: Some(1),
                line: Cow::Borrowed(&curr_line),
            }),
        };

        assert_from_str_message!(
            engine
            "info depth BAD depth 20 seldepth 31 time 12 depth also bad nodes 4 multipv 1 score cp 22 lowerbound currmove e2e4 tbhits 2 refutation g2g4 d7d5 f1g2 currline 1 e2e4 c7c5 tbhits 4 string blabla",
            Ok(m.clone())
        );
        assert_message_to_str!(
            engine
            m,
            "info depth 20 seldepth 31 time 12 nodes 4 multipv 1 score cp 22 lowerbound currmove e2e4 tbhits 4 refutation g2g4 d7d5 f1g2 currline 1 e2e4 c7c5 string blabla"
        );
    }

    #[test]
    fn to_from_str_string() {
        let m = Info {
            depth: Some(Depth {
                depth: 13,
                seldepth: None
            }),
            string: Some(Cow::Borrowed("the parameters in this string should be ignored! depth 20 (psych) time 2 nodes 4 score cp 22 lowerbound")),
            ..Default::default()
        };
        
        assert_message_to_from_str!(
            engine
            m,
            "info depth 13 string the parameters in this string should be ignored! depth 20 (psych) time 2 nodes 4 score cp 22 lowerbound"
        );
    }
}