Struct SgfNode

Source
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>

Source

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

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!(),
};
Source

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!")
    }
}
Source

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"),
        }
        _ => {},
    }
}
Source

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

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

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

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

Trait Implementations§

Source§

impl<Prop: Clone + SgfProp> Clone for SgfNode<Prop>

Source§

fn clone(&self) -> SgfNode<Prop>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Prop: Debug + SgfProp> Debug for SgfNode<Prop>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Prop: SgfProp> Default for SgfNode<Prop>

Source§

fn default() -> Self

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

impl<Prop: SgfProp> Display for SgfNode<Prop>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<SgfNode<Prop>> for GameTree

Source§

fn from(sgf_node: SgfNode<Prop>) -> Self

Converts to this type from the input type.
Source§

impl From<SgfNode<Prop>> for GameTree

Source§

fn from(sgf_node: SgfNode<Prop>) -> Self

Converts to this type from the input type.
Source§

impl<Prop: PartialEq + SgfProp> PartialEq for SgfNode<Prop>

Source§

fn eq(&self, other: &SgfNode<Prop>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Prop: SgfProp> StructuralPartialEq for SgfNode<Prop>

Auto Trait Implementations§

§

impl<Prop> Freeze for SgfNode<Prop>

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.