5 stable releases

1.0.4 Jan 20, 2024
1.0.1 Jan 19, 2024

#1339 in Parser implementations

23 downloads per month

MIT license

38KB
821 lines

SGF-TOOL

Build & Test Latest Version Rust Documentation Crates.io Crates.io

SGF file format parser and builder.

Reference: https://red-bean.com/sgf/sgf4.html

Examples

use std::borrow::Cow;
use sgf_tool::*;

fn main() -> Result<(), SgfToolError> {
    // Parse sgf
    let source = "(;FF[4];C[root];SZ[19];B[aa];W[ab])";
    let base = parse(&source)?;

    for token in base.tokens.iter() {
        println!("Token: {:?}", token);
    }

    // Or

    assert_eq!(base.get(TokenType::FileFormat), Some(Cow::Owned(Token::FileFormat(4))).as_ref());

    /* Rebuild sgf and validage */
    let mut buffer = String::new();
    base.build(&mut buffer)?;
    assert_eq!(buffer, source);

    // Build sgf
    let mut base = sgf_tool::Base::default();
    base.add_token(Token::Application("sgf-tool"));
    base.add_token(Token::BlackMove(Move::Move(Point("ab"))));
    base.add_token(Token::WhiteMove(Move::Move(Point("bc"))));
    base.add_token(Token::BlackMove(Move::Pass));

    assert_eq!("(;AP[sgf-tool];B[ab];W[bc];B[])", &build(base)?);
    Ok(())
}

Dependencies

~2.3–3MB
~63K SLoC