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
use std::io;
use failure::{Backtrace, Fail};
use shakmaty::Material;
use crate::types::Metric;
pub type SyzygyResult<T> = Result<T, SyzygyError>;
pub type ProbeResult<T> = Result<T, ProbeError>;
#[derive(Debug, Fail)]
pub enum SyzygyError {
#[fail(display = "syzygy tables do not contain positions with castling rights")]
Castling,
#[fail(display = "too many pieces")]
TooManyPieces,
#[fail(display = "required {} table not found: {}", metric, material)]
MissingTable {
metric: Metric,
material: Material
},
#[fail(display = "failed to probe {} table {}: {}", metric, material, error)]
ProbeFailed {
metric: Metric,
material: Material,
#[fail(cause)]
error: ProbeError,
},
}
#[derive(Debug, Fail)]
pub enum ProbeError {
#[fail(display = "i/o error reading table file: {}", error)]
Read {
#[fail(cause)]
error: io::Error,
},
#[fail(display = "invalid magic header bytes: {:x?}", magic)]
Magic {
magic: [u8; 4],
},
#[fail(display = "corrupted table")]
CorruptedTable(Backtrace),
}
pub trait ProbeResultExt<T> {
fn ctx(self, metric: Metric, material: &Material) -> SyzygyResult<T>;
}
impl<T> ProbeResultExt<T> for ProbeResult<T> {
fn ctx(self, metric: Metric, material: &Material) -> SyzygyResult<T> {
self.map_err(|error| SyzygyError::ProbeFailed {
metric,
material: material.normalized(),
error,
})
}
}
impl From<io::Error> for ProbeError {
fn from(error: io::Error) -> ProbeError {
match error.kind() {
io::ErrorKind::UnexpectedEof => ProbeError::CorruptedTable(Backtrace::new()),
_ => ProbeError::Read { error },
}
}
}
macro_rules! throw {
() => {
return Err(crate::errors::ProbeError::CorruptedTable(::failure::Backtrace::new()))
}
}
macro_rules! u {
($e:expr) => {
match $e {
Some(ok) => ok,
None => throw!(),
}
};
}
macro_rules! ensure {
($cond:expr) => {
if !$cond {
throw!();
}
};
}