sgf_parse/lib.rs
1//! Data structures and utilities for parsing [SGF FF\[4\] files](https://www.red-bean.com/sgf/).
2//!
3//! ## Quick Start
4//!
5//! Most common use case - parsing a Go game and iterating through its moves:
6//! ```rust
7//! use sgf_parse::{parse, go::Prop, go::Move};
8//!
9//! let sgf = "(;FF[4]GM[1]B[aa];W[ab])";
10//!
11//! let collection = parse(sgf).unwrap();
12//! let root_node = collection.first().unwrap().as_go_node().unwrap();
13//!
14//! // Iterate through the main variation
15//! for node in root_node.main_variation() {
16//! if let Some(prop) = node.get_move() {
17//! println!("Move: {}", prop);
18//! }
19//! }
20//! ```
21//!
22//! Working with multi-game collections:
23//! ```rust
24//! # use sgf_parse::parse;
25//! let sgf = "(;FF[4]GM[1];B[aa])(;FF[4]GM[1];B[dd])";
26//! let collection = parse(sgf).unwrap();
27//!
28//! for gametree in &collection {
29//! let root_node = gametree.as_go_node().unwrap();
30//! println!("Game has {} nodes", root_node.main_variation().count());
31//! }
32//! ```
33//!
34//! For reading SGFs your starting point will likely be [`go::parse`]. For parsing non-go games
35//! check out the [`parse`](`parse()`) function.
36//!
37//! For writing SGFs check out [`SgfNode::serialize`] for writing single game trees or
38//! [`serialize`](`serialize()`) for writing whole collections.
39
40#[macro_use]
41mod prop_macro;
42
43pub mod go;
44pub mod unknown_game;
45
46mod game_tree;
47mod lexer;
48mod parser;
49mod props;
50mod serialize;
51mod sgf_node;
52
53pub use game_tree::{GameTree, GameType};
54pub use lexer::LexerError;
55pub use parser::{parse, parse_with_options, ParseOptions, SgfParseError};
56pub use props::{Color, Double, PropertyType, SgfProp, SimpleText, Text};
57pub use serialize::serialize;
58pub use sgf_node::{InvalidNodeError, SgfNode};