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: boolImplementations§
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(_))));Trait Implementations§
source§impl<Prop: PartialEq + SgfProp> PartialEq<SgfNode<Prop>> for SgfNode<Prop>
impl<Prop: PartialEq + SgfProp> PartialEq<SgfNode<Prop>> for SgfNode<Prop>
impl<Prop: SgfProp> StructuralPartialEq for SgfNode<Prop>
Auto Trait Implementations§
impl<Prop> RefUnwindSafe for SgfNode<Prop>where Prop: RefUnwindSafe,
impl<Prop> Send for SgfNode<Prop>where Prop: Send,
impl<Prop> Sync for SgfNode<Prop>where Prop: Sync,
impl<Prop> Unpin for SgfNode<Prop>where Prop: Unpin,
impl<Prop> UnwindSafe for SgfNode<Prop>where Prop: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more