Write a little app to demonstrate reading an an SGF file
This commit is contained in:
parent
a5990a2a30
commit
1d959117aa
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<String>,
|
||||
annotator: Option<String>,
|
||||
copyright: Option<String>,
|
||||
dates: Vec<Date>,
|
||||
event: Option<String>,
|
||||
game_name: Option<String>,
|
||||
extra_info: Option<String>,
|
||||
opening_info: Option<String>,
|
||||
location: Option<String>,
|
||||
result: Option<GameResult>,
|
||||
round: Option<String>,
|
||||
rules: Option<String>,
|
||||
source: Option<String>,
|
||||
time_limit: Option<Duration>,
|
||||
overtime: Option<String>,
|
||||
transcriber: Option<String>,
|
||||
pub app: Option<String>,
|
||||
pub annotator: Option<String>,
|
||||
pub copyright: Option<String>,
|
||||
pub dates: Vec<Date>,
|
||||
pub event: Option<String>,
|
||||
pub game_name: Option<String>,
|
||||
pub extra_info: Option<String>,
|
||||
pub opening_info: Option<String>,
|
||||
pub location: Option<String>,
|
||||
pub result: Option<GameResult>,
|
||||
pub round: Option<String>,
|
||||
pub rules: Option<String>,
|
||||
pub source: Option<String>,
|
||||
pub time_limit: Option<Duration>,
|
||||
pub overtime: Option<String>,
|
||||
pub transcriber: Option<String>,
|
||||
|
||||
children: Vec<GameNode>,
|
||||
pub children: Vec<GameNode>,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
|
|
|
@ -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<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> {
|
||||
Ok(vec![Game::new(
|
||||
GameType::Go,
|
||||
|
@ -78,3 +95,4 @@ pub fn parse_sgf(_input: &str) -> Result<Vec<Game>, Error> {
|
|||
},
|
||||
)])
|
||||
}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue