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
use std::io;
use material::Material;
pub type SyzygyResult<T> = Result<T, SyzygyError>;
#[derive(Debug, Fail)]
pub enum SyzygyError {
#[fail(display = "syzygy tables do not contain positions with castling rights")]
Castling,
#[fail(display = "syzygy tables only contain positions with up to 7 pieces")]
TooManyPieces,
#[fail(display = "required table not found: {}", material)]
MissingTable { material: Material },
#[fail(display = "i/o error when reading a table: {}", error)]
Read { error: io::Error },
#[fail(display = "table file has invalid magic bytes")]
Magic,
#[fail(display = "corrupted table (detected in {} l. {})", file, line)]
CorruptedTable { file: &'static str, line: u32 },
}
impl From<io::Error> for SyzygyError {
fn from(error: io::Error) -> SyzygyError {
match error.kind() {
io::ErrorKind::UnexpectedEof => SyzygyError::CorruptedTable { file: file!(), line: line!() },
_ => SyzygyError::Read { error },
}
}
}
macro_rules! throw {
() => {
return Err(SyzygyError::CorruptedTable {
file: file!(),
line: line!(),
})
}
}
macro_rules! u {
($e:expr) => {
match $e {
Some(ok) => ok,
None => throw!(),
}
}
}
macro_rules! ensure {
($cond:expr) => {
if !$cond {
throw!();
}
}
}