Set up a second layer to the SGF parser #226

Merged
savanni merged 4 commits from kifu/game-repr into main 2024-03-22 03:14:04 +00:00
3 changed files with 60 additions and 23 deletions
Showing only changes of commit 1d959117aa - Show all commits

19
sgf/src/bin/read_sgf.rs Normal file
View File

@ -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);
}
}
}

View File

@ -53,31 +53,31 @@ pub struct Player {
/// whatever). /// whatever).
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Game { 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. // TODO: board size is not necessary in all games. Hive has no defined board size.
board_size: Size, pub board_size: Size,
black_player: Player, pub black_player: Player,
white_player: Player, pub white_player: Player,
app: Option<String>, pub app: Option<String>,
annotator: Option<String>, pub annotator: Option<String>,
copyright: Option<String>, pub copyright: Option<String>,
dates: Vec<Date>, pub dates: Vec<Date>,
event: Option<String>, pub event: Option<String>,
game_name: Option<String>, pub game_name: Option<String>,
extra_info: Option<String>, pub extra_info: Option<String>,
opening_info: Option<String>, pub opening_info: Option<String>,
location: Option<String>, pub location: Option<String>,
result: Option<GameResult>, pub result: Option<GameResult>,
round: Option<String>, pub round: Option<String>,
rules: Option<String>, pub rules: Option<String>,
source: Option<String>, pub source: Option<String>,
time_limit: Option<Duration>, pub time_limit: Option<Duration>,
overtime: Option<String>, pub overtime: Option<String>,
transcriber: Option<String>, pub transcriber: Option<String>,
children: Vec<GameNode>, pub children: Vec<GameNode>,
} }
impl Game { impl Game {

View File

@ -3,11 +3,10 @@ mod game;
mod parser; mod parser;
mod types; mod types;
use std::{fs::File, io::Read};
pub use date::Date; pub use date::Date;
pub use game::Game; pub use game::Game;
use game::Player;
pub use parser::parse_collection; pub use parser::parse_collection;
use parser::Size;
use thiserror::Error; use thiserror::Error;
pub use types::*; pub use types::*;
@ -59,6 +58,24 @@ impl From<nom::error::Error<&str>> for ParseError {
} }
} }
pub fn parse_sgf(input: &str) -> Result<Vec<Result<Game, game::GameError>>, Error> {
let (_, games) = parse_collection::<nom::error::VerboseError<&str>>(&input)?;
let games = games.into_iter()
.map(|game| Game::try_from(&game))
.collect::<Vec<Result<Game, game::GameError>>>();
Ok(games)
}
pub fn parse_sgf_file(path: &std::path::Path) -> Result<Vec<Result<Game, game::GameError>>, 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<Vec<Game>, Error> { pub fn parse_sgf(_input: &str) -> Result<Vec<Game>, Error> {
Ok(vec![Game::new( Ok(vec![Game::new(
GameType::Go, GameType::Go,
@ -78,3 +95,4 @@ pub fn parse_sgf(_input: &str) -> Result<Vec<Game>, Error> {
}, },
)]) )])
} }
*/