Struct sgf_parse::SgfNode[][src]

pub struct SgfNode<Prop: SgfProp> {
    pub properties: Vec<Prop>,
    pub children: Vec<Self>,
    pub is_root: bool,
}
Expand description

A node in an SGF Game Tree.

Any succesfully constructed node will be serializable, but may or may not be valid. All game-specific information is encoded in the Prop type. Use go::Prop for go games, and unknown_game::Prop for all other games.

Fields

properties: Vec<Prop>children: Vec<Self>is_root: bool

Implementations

Returns a new node.

Examples

use sgf_parse::{SgfNode, SgfProp};
use sgf_parse::go::Prop;

let children = vec![
    SgfNode::<Prop>::new(
        vec![Prop::new("B".to_string(), vec!["dd".to_string()])],
        vec![],
        false,
    ),
];
let node = SgfNode::new(vec![Prop::SZ((19, 19))], children, true);

Returns the property with the provided identifier for the node (if present).

Examples

use sgf_parse::go::{parse, Prop};

let node = parse("(;SZ[13:13];B[de])").unwrap().into_iter().next().unwrap();
let board_size = match node.get_property("SZ") {
    Some(Prop::SZ(size)) => size.clone(),
    None => (19, 19),
    _ => unreachable!(),
};

Returns an iterator over the children of this node.

Examples

use sgf_parse::go::parse;

let node = parse("(;SZ[19](;B[de])(;B[dd]HO[2]))").unwrap().into_iter().next().unwrap();
for child in node.children() {
    if let Some(prop) = child.get_property("HO") {
       println!("Found a hotspot!")
    }
}

Returns an iterator over the properties of this node.

Examples

use sgf_parse::go::{parse, Move, Prop};

let node = parse("(;B[de]C[A comment])").unwrap().into_iter().next().unwrap();
for prop in node.properties() {
    match prop {
        Prop::B(mv) => match mv {
            Move::Move(p) => println!("B Move at {}, {}", p.x, p.y),
            Move::Pass => println!("B Pass"),
        }
        Prop::W(mv) => match mv {
            Move::Move(p) => println!("W Move at {}, {}", p.x, p.y),
            Move::Pass => println!("W Pass"),
        }
        _ => {},
    }
}

Returns the serialized SGF for this SgfNode as a complete GameTree.

Examples

use sgf_parse::go::parse;

let sgf = "(;SZ[13:13];B[de])";
let node = parse(sgf).unwrap().into_iter().next().unwrap();
assert_eq!(node.serialize(), sgf);

Returns Ok if the node’s properties are valid according to the SGF FF[4] spec.

Errors

Returns an error if the node has invalid properties.

Examples

use sgf_parse::InvalidNodeError;
use sgf_parse::go::parse;

let node = parse("(;B[de]C[A comment]C[Another])").unwrap().into_iter().next().unwrap();
let result = node.validate();
assert!(matches!(result, Err(InvalidNodeError::RepeatedIdentifier(_))));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.