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
use crate::{GameNode, SgfError, SgfErrorKind, SgfToken};

/// A game tree, containing it's nodes and possible variations following the last node
#[derive(Debug, Clone, PartialEq)]
pub struct GameTree {
    pub nodes: Vec<GameNode>,
    pub variations: Vec<GameTree>,
}

impl Default for GameTree {
    /// Creates an empty GameTree
    fn default() -> Self {
        GameTree {
            nodes: vec![],
            variations: vec![],
        }
    }
}

impl GameTree {
    /// Counts number of nodes in the longest variation
    pub fn count_max_nodes(&self) -> usize {
        let count = self.nodes.len();
        let variation_count = self
            .variations
            .iter()
            .map(|v| v.count_max_nodes())
            .max()
            .unwrap_or(0);

        count + variation_count
    }

    /// Gets a vector of all nodes that contain a `SgfToken::Unknown` token
    ///
    /// ```rust
    /// use sgf_parser::*;
    ///
    /// let tree: GameTree = parse("(;B[dc];W[ef]TMP[foobar](;B[aa])(;B[cc];W[ee]))").unwrap();
    ///
    /// let unknown_nodes = tree.get_unknown_nodes();
    /// unknown_nodes.iter().for_each(|node| {
    ///     let unknown_tokens = node.get_unknown_tokens();
    ///     assert_eq!(unknown_tokens.len(), 1);
    ///     if let SgfToken::Unknown((identifier, value)) = unknown_tokens[0] {
    ///         assert_eq!(identifier, "TMP");
    ///         assert_eq!(value, "foobar");
    ///     }
    /// });
    ///
    /// ```
    pub fn get_unknown_nodes(&self) -> Vec<&GameNode> {
        let mut unknowns = self
            .nodes
            .iter()
            .filter(|node| {
                node.tokens
                    .iter()
                    .any(|t| matches!(t, SgfToken::Unknown(_)))
            })
            .collect::<Vec<_>>();
        self.variations.iter().for_each(|variation| {
            let tmp = variation.get_unknown_nodes();
            unknowns.extend(tmp);
        });
        unknowns
    }

    /// Gets a vector of all nodes that contain a `SgfToken::Invalid` token
    ///
    /// ```rust
    /// use sgf_parser::*;
    ///
    /// let tree: GameTree = parse("(;B[dc];W[foobar];B[aa])(;B[cc];W[ee]))").unwrap();
    ///
    /// let invalid_nodes = tree.get_invalid_nodes();
    /// invalid_nodes.iter().for_each(|node| {
    ///     let invalid_tokens = node.get_invalid_tokens();
    ///     if let SgfToken::Invalid((identifier, value)) = invalid_tokens[0] {
    ///         assert_eq!(identifier, "W");
    ///         assert_eq!(value, "foobar");
    ///     }
    /// });
    ///
    /// ```
    pub fn get_invalid_nodes(&self) -> Vec<&GameNode> {
        let mut invalids = self
            .nodes
            .iter()
            .filter(|node| {
                node.tokens
                    .iter()
                    .any(|t| matches!(t, SgfToken::Invalid(_)))
            })
            .collect::<Vec<_>>();
        self.variations.iter().for_each(|variation| {
            let tmp = variation.get_invalid_nodes();
            invalids.extend(tmp);
        });
        invalids
    }

    /// Checks if this GameTree has any variations
    pub fn has_variations(&self) -> bool {
        !self.variations.is_empty()
    }

    /// Counts number of variations in the GameTree
    pub fn count_variations(&self) -> usize {
        self.variations.len()
    }

    /// Get max length of a variation
    ///
    /// ```rust
    /// use sgf_parser::*;
    ///
    /// let tree: GameTree = parse("(;B[dc];W[ef](;B[aa])(;B[cc];W[ee]))").unwrap();
    ///
    /// assert_eq!(tree.get_varation_length(0).unwrap(), 1);
    /// assert_eq!(tree.get_varation_length(1).unwrap(), 2);
    /// ```
    pub fn get_varation_length(&self, variation: usize) -> Result<usize, SgfError> {
        if let Some(variation) = self.variations.get(variation) {
            Ok(variation.count_max_nodes())
        } else {
            Err(SgfErrorKind::VariationNotFound.into())
        }
    }

    /// Gets an iterator for the GameTree
    ///
    /// ```rust
    /// use sgf_parser::*;
    ///
    /// let tree: GameTree = parse("(;B[dc];W[ef](;B[aa])(;B[cc];W[ee]))").unwrap();
    ///
    /// let mut iter = tree.iter();
    ///
    /// assert_eq!(iter.count_variations(), 2);
    /// assert!(iter.pick_variation(1).is_ok());
    ///
    /// let mut count = 0;
    /// iter.for_each(|node| {
    ///     assert!(!node.tokens.is_empty());
    ///     count += 1;
    /// });
    ///
    /// assert_eq!(count, tree.count_max_nodes());
    /// ```
    pub fn iter(&self) -> GameTreeIterator<'_> {
        GameTreeIterator::new(self)
    }

    /// Checks if the tree is valid. `self` is assumed to be a root tree, so it can contain
    /// root tokens in it's first node.
    ///
    /// The only way to invalidate a GameTree is to have a root token in a non-root node.
    /// The provided `parse` function will not generate invalid a `GameTree`, but manual creation
    /// or modification of a game tree can create an invalid state
    ///
    /// `SgfToken::Invalid` and `SgfToken::Unknown` does not invalidate the tree
    ///
    /// ```rust
    /// use sgf_parser::*;
    ///
    /// let valid_tree: GameTree = parse("(;SZ[19]B[dc];W[ef](;B[aa])(;B[cc];W[ee]))").unwrap();
    /// assert!(valid_tree.is_valid());
    ///
    /// let mut invalid_tree: GameTree = valid_tree.clone();
    /// invalid_tree.variations[1].nodes[0].tokens.push(SgfToken::from_pair("SZ", "19"));
    ///
    /// assert!(!invalid_tree.is_valid());
    /// ```
    pub fn is_valid(&self) -> bool {
        let validate_nodes = |nodes: &[GameNode]| {
            nodes
                .iter()
                .all(|node| node.tokens.iter().all(|t| !t.is_root_token()))
        };
        if !validate_nodes(&self.nodes[1..]) {
            return false;
        }

        let mut trees: Vec<&GameTree> = vec![];
        trees.extend(self.variations.iter());
        while let Some(tree) = trees.pop() {
            if !validate_nodes(&tree.nodes) {
                return false;
            }
            trees.extend(tree.variations.iter());
        }
        true
    }
}

impl Into<String> for &GameTree {
    fn into(self) -> String {
        let nodes = self
            .nodes
            .iter()
            .map(|n| -> String { n.into() })
            .collect::<String>();
        let variations = self
            .variations
            .iter()
            .map(|n| -> String { n.into() })
            .collect::<String>();
        format!("({}{})", nodes, variations)
    }
}

impl Into<String> for GameTree {
    fn into(self) -> String {
        (&self).into()
    }
}

pub struct GameTreeIterator<'a> {
    tree: &'a GameTree,
    index: usize,
    variation: usize,
}

impl<'a> GameTreeIterator<'a> {
    fn new(game_tree: &'a GameTree) -> Self {
        GameTreeIterator {
            tree: game_tree,
            index: 0,
            variation: 0,
        }
    }

    /// Checks if the current `GameTree` has any variations
    pub fn has_variations(&self) -> bool {
        self.tree.has_variations()
    }

    /// Counts number of variations in the current `GameTree`
    pub fn count_variations(&self) -> usize {
        self.tree.count_variations()
    }

    /// Picks a varation in the current `GameTree` to continue with, once the nodes haves been exhausted
    pub fn pick_variation(&mut self, variation: usize) -> Result<usize, SgfError> {
        if variation < self.tree.variations.len() {
            self.variation = variation;
            Ok(self.variation)
        } else {
            Err(SgfErrorKind::VariationNotFound.into())
        }
    }
}

impl<'a> Iterator for GameTreeIterator<'a> {
    type Item = &'a GameNode;

    fn next(&mut self) -> Option<&'a GameNode> {
        match self.tree.nodes.get(self.index) {
            Some(node) => {
                self.index += 1;
                Some(&node)
            }
            None => {
                if !self.tree.variations.is_empty() {
                    self.tree = &self.tree.variations[self.variation];
                    self.index = 0;
                    self.variation = 0;
                    self.next()
                } else {
                    None
                }
            }
        }
    }
}