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§
Source§impl<Prop: SgfProp> SgfNode<Prop>
impl<Prop: SgfProp> SgfNode<Prop>
Sourcepub fn new(properties: Vec<Prop>, children: Vec<Self>, is_root: bool) -> Self
pub fn new(properties: Vec<Prop>, children: Vec<Self>, is_root: bool) -> Self
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);
Sourcepub fn get_property(&self, identifier: &str) -> Option<&Prop>
pub fn get_property(&self, identifier: &str) -> Option<&Prop>
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!(),
};
Sourcepub fn children(&self) -> impl Iterator<Item = &Self>
pub fn children(&self) -> impl Iterator<Item = &Self>
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!")
}
}
Sourcepub fn properties(&self) -> impl Iterator<Item = &Prop>
pub fn properties(&self) -> impl Iterator<Item = &Prop>
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"),
}
_ => {},
}
}
Sourcepub fn serialize(&self) -> String
pub fn serialize(&self) -> String
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);
Sourcepub fn validate(&self) -> Result<(), InvalidNodeError>
pub fn validate(&self) -> Result<(), InvalidNodeError>
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(_))));
Sourcepub fn main_variation(&self) -> impl Iterator<Item = &Self>
pub fn main_variation(&self) -> impl Iterator<Item = &Self>
Returns an iterator over the nodes of the main variation.
This is a convenience method for iterating through the first child of each node until the main line ends.
§Examples
use crate::sgf_parse::SgfProp;
use sgf_parse::go::{parse, Prop};
let sgf = "(;B[ee];W[ce](;B[ge](;W[gd])(;W[gf]))(;B[ce]))";
let node = &parse(sgf).unwrap()[0];
let moves: Vec<Prop> = node
.main_variation()
.map(|n| {
n.get_property("B")
.or_else(|| n.get_property("W"))
.unwrap()
.clone()
})
.collect();
let expected = vec![
Prop::new("B".to_string(), vec!["ee".to_string()]),
Prop::new("W".to_string(), vec!["ce".to_string()]),
Prop::new("B".to_string(), vec!["ge".to_string()]),
Prop::new("W".to_string(), vec!["gd".to_string()]),
];
assert_eq!(moves, expected);
Sourcepub fn get_move(&self) -> Option<&Prop>
pub fn get_move(&self) -> Option<&Prop>
Returns the move property (if present) on the node.
§Examples
use crate::sgf_parse::SgfProp;
use sgf_parse::go::{parse, Prop};
let sgf = "(;GM[1]B[tt]C[Comment])";
let node = &parse(sgf).unwrap()[0];
let mv = node.get_move();
assert_eq!(mv, Some(&Prop::new("B".to_string(), vec!["tt".to_string()])));