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
use std::collections::HashSet;
use super::{PropertyType, SgfParseError, SgfProp};
#[derive(Clone, Debug, PartialEq)]
pub struct SgfNode {
properties: Vec<SgfProp>,
children: Vec<SgfNode>,
is_root: bool,
has_game_info: bool,
}
impl SgfNode {
pub fn new(
properties: Vec<SgfProp>,
children: Vec<Self>,
is_root: bool,
) -> Result<Self, SgfParseError> {
let (has_root_props, has_game_info_props) = validate_node_props(&properties)?;
if has_root_props && !is_root {
return Err(SgfParseError::InvalidNode(
"Root properties in non-root node".to_string(),
));
}
let children_have_game_info = children.iter().any(|child| child.has_game_info);
if has_game_info_props && children_have_game_info {
return Err(SgfParseError::InvalidNode(
"Multiple GameInfo nodes in path.".to_string(),
));
}
Ok(Self {
properties,
children,
is_root,
has_game_info: has_game_info_props || children_have_game_info,
})
}
pub fn get_property(&self, identifier: &str) -> Option<&SgfProp> {
for prop in &self.properties {
if prop.identifier() == identifier {
return Some(prop);
}
}
None
}
pub fn children<'a>(&'a self) -> impl Iterator<Item = &Self> + 'a {
self.children.iter()
}
pub fn properties<'a>(&'a self) -> impl Iterator<Item = &SgfProp> + 'a {
self.properties.iter()
}
pub fn into_builder(self) -> SgfNodeBuilder {
let Self {
properties,
children,
is_root,
has_game_info: _,
} = self;
let children = children
.into_iter()
.map(Self::into_builder)
.collect::<Vec<_>>();
SgfNodeBuilder {
properties,
children,
is_root,
}
}
}
fn validate_node_props(props: &[SgfProp]) -> Result<(bool, bool), SgfParseError> {
let mut identifiers = HashSet::new();
let mut markup_points = HashSet::new();
let mut setup_node = false;
let mut move_node = false;
let mut move_seen = false;
let mut game_info_node = false;
let mut root_node = false;
let mut exclusive_node_annotations = 0;
let mut move_annotation_count = 0;
for prop in props.iter() {
match prop {
SgfProp::B(_) => {
move_seen = true;
if identifiers.contains("W") {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
}
SgfProp::W(_) => {
move_seen = true;
if identifiers.contains("B") {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
}
SgfProp::CR(ps)
| SgfProp::MA(ps)
| SgfProp::SL(ps)
| SgfProp::SQ(ps)
| SgfProp::TR(ps) => {
for p in ps.iter() {
if markup_points.contains(&p) {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
markup_points.insert(p);
}
}
SgfProp::DM(_) | SgfProp::UC(_) | SgfProp::GW(_) | SgfProp::GB(_) => {
exclusive_node_annotations += 1
}
SgfProp::BM(_) | SgfProp::DO | SgfProp::IT | SgfProp::TE(_) => {
move_annotation_count += 1
}
_ => {}
}
match prop.property_type() {
Some(PropertyType::Move) => move_node = true,
Some(PropertyType::Setup) => setup_node = true,
Some(PropertyType::GameInfo) => game_info_node = true,
Some(PropertyType::Root) => root_node = true,
_ => {}
}
let ident = prop.identifier();
if identifiers.contains(&ident) {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
identifiers.insert(prop.identifier());
}
if setup_node && move_node {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
if identifiers.contains("KO") && !(identifiers.contains("B") || identifiers.contains("W")) {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
if move_annotation_count > 1 || (move_annotation_count == 1 && !move_seen) {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
if exclusive_node_annotations > 1 {
return Err(SgfParseError::InvalidNodeProps(props.to_owned()));
}
Ok((root_node, game_info_node))
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SgfNodeBuilder {
pub properties: Vec<SgfProp>,
pub children: Vec<SgfNodeBuilder>,
pub is_root: bool,
}
impl SgfNodeBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn build(self) -> Result<SgfNode, SgfParseError> {
use core::cell::RefCell;
use std::rc::Rc;
let mut node_parts = vec![];
let mut dfs_stack = vec![(self, None)];
while let Some((node, parent_children)) = dfs_stack.pop() {
let Self {
properties,
children,
is_root,
} = node;
let built_children: Rc<RefCell<Vec<SgfNode>>> = Rc::new(RefCell::new(vec![]));
for child in children {
dfs_stack.push((child, Some(built_children.clone())));
}
node_parts.push((properties, built_children, is_root, parent_children));
}
for (properties, children, is_root, parent_children) in node_parts.into_iter().rev() {
let children = Rc::try_unwrap(children)
.expect("All children should already be built")
.into_inner();
let new_node = SgfNode::new(properties, children, is_root)?;
if let Some(parent_children) = parent_children {
parent_children.borrow_mut().push(new_node);
} else {
return Ok(new_node);
}
}
unreachable!("The first node must have no parent")
}
}