From 1d959117aab953b6c3b18eac98dfc47ca2489eaf Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Thu, 21 Mar 2024 23:12:30 -0400 Subject: [PATCH] Write a little app to demonstrate reading an an SGF file --- sgf/src/bin/read_sgf.rs | 19 +++++++++++++++++++ sgf/src/game.rs | 42 ++++++++++++++++++++--------------------- sgf/src/lib.rs | 22 +++++++++++++++++++-- 3 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 sgf/src/bin/read_sgf.rs diff --git a/sgf/src/bin/read_sgf.rs b/sgf/src/bin/read_sgf.rs new file mode 100644 index 0000000..af3c5e9 --- /dev/null +++ b/sgf/src/bin/read_sgf.rs @@ -0,0 +1,19 @@ +use sgf::parse_sgf_file; +use std::path::PathBuf; +use std::env; + +fn main() { + let mut args = env::args(); + + let _ = args.next(); + let file = PathBuf::from(args.next().unwrap()); + + println!("{:?}", file); + + let games = parse_sgf_file(&file).unwrap(); + for sgf in games { + if let Ok(sgf) = sgf { + println!("{:?}", sgf.white_player); + } + } +} diff --git a/sgf/src/game.rs b/sgf/src/game.rs index 1b6a0cb..e1d967b 100644 --- a/sgf/src/game.rs +++ b/sgf/src/game.rs @@ -53,31 +53,31 @@ pub struct Player { /// whatever). #[derive(Clone, Debug, PartialEq)] pub struct Game { - game_type: GameType, + pub game_type: GameType, // TODO: board size is not necessary in all games. Hive has no defined board size. - board_size: Size, - black_player: Player, - white_player: Player, + pub board_size: Size, + pub black_player: Player, + pub white_player: Player, - app: Option, - annotator: Option, - copyright: Option, - dates: Vec, - event: Option, - game_name: Option, - extra_info: Option, - opening_info: Option, - location: Option, - result: Option, - round: Option, - rules: Option, - source: Option, - time_limit: Option, - overtime: Option, - transcriber: Option, + pub app: Option, + pub annotator: Option, + pub copyright: Option, + pub dates: Vec, + pub event: Option, + pub game_name: Option, + pub extra_info: Option, + pub opening_info: Option, + pub location: Option, + pub result: Option, + pub round: Option, + pub rules: Option, + pub source: Option, + pub time_limit: Option, + pub overtime: Option, + pub transcriber: Option, - children: Vec, + pub children: Vec, } impl Game { diff --git a/sgf/src/lib.rs b/sgf/src/lib.rs index 9b88bf2..c37cad2 100644 --- a/sgf/src/lib.rs +++ b/sgf/src/lib.rs @@ -3,11 +3,10 @@ mod game; mod parser; mod types; +use std::{fs::File, io::Read}; pub use date::Date; pub use game::Game; -use game::Player; pub use parser::parse_collection; -use parser::Size; use thiserror::Error; pub use types::*; @@ -59,6 +58,24 @@ impl From> for ParseError { } } +pub fn parse_sgf(input: &str) -> Result>, Error> { + let (_, games) = parse_collection::>(&input)?; + let games = games.into_iter() + .map(|game| Game::try_from(&game)) + .collect::>>(); + + Ok(games) +} + +pub fn parse_sgf_file(path: &std::path::Path) -> Result>, Error> { + let mut file = File::open(path).unwrap(); + let mut text = String::new(); + let _ = file.read_to_string(&mut text); + + parse_sgf(&text) +} + +/* pub fn parse_sgf(_input: &str) -> Result, Error> { Ok(vec![Game::new( GameType::Go, @@ -78,3 +95,4 @@ pub fn parse_sgf(_input: &str) -> Result, Error> { }, )]) } +*/