Crates.io | sgf-tool |
lib.rs | sgf-tool |
version | 1.0.4 |
source | src |
created_at | 2024-01-19 21:46:56.603575 |
updated_at | 2024-01-20 14:46:16.021749 |
description | SGF Tool |
homepage | |
repository | https://github.com/erhanbaris/sgf-tool |
max_upload_size | |
id | 1105767 |
size | 38,277 |
SGF file format parser and builder.
Reference: https://red-bean.com/sgf/sgf4.html
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(())
}