[][src]Struct sgf_parser::GameTree

pub struct GameTree {
    pub nodes: Vec<GameNode>,
    pub variations: Vec<GameTree>,
}

A game tree, containing it's nodes and possible variations following the last node

Fields

nodes: Vec<GameNode>variations: Vec<GameTree>

Implementations

impl GameTree[src]

pub fn count_max_nodes(&self) -> usize[src]

Counts number of nodes in the longest variation

pub fn get_unknown_nodes(&self) -> Vec<&GameNode>[src]

Gets a vector of all nodes that contain a SgfToken::Unknown token

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_invalid_nodes(&self) -> Vec<&GameNode>[src]

Gets a vector of all nodes that contain a SgfToken::Invalid token

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 has_variations(&self) -> bool[src]

Checks if this GameTree has any variations

pub fn count_variations(&self) -> usize[src]

Counts number of variations in the GameTree

pub fn get_varation_length(&self, variation: usize) -> Result<usize, SgfError>[src]

Get max length of a variation

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 iter(&self) -> GameTreeIterator<'_>[src]

Gets an iterator for the GameTree

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 is_valid(&self) -> bool[src]

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

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());

Trait Implementations

impl Clone for GameTree[src]

impl Debug for GameTree[src]

impl Default for GameTree[src]

pub fn default() -> Self[src]

Creates an empty GameTree

impl Into<String> for &GameTree[src]

impl Into<String> for GameTree[src]

impl PartialEq<GameTree> for GameTree[src]

impl StructuralPartialEq for GameTree[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.