From b9808a1862d35ebce33114c222eca06a9bb92f75 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Wed, 26 Jul 2023 19:33:50 -0400 Subject: [PATCH] Introduce the Game data structure --- Makefile | 3 +++ sgf/Cargo.lock | 22 +++++++++++----------- sgf/src/go.rs | 9 ++------- sgf/src/lib.rs | 27 +++++++++++++++++++++++++-- sgf/src/tree.rs | 1 + 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 44f3aa7..7706641 100644 --- a/Makefile +++ b/Makefile @@ -65,5 +65,8 @@ kifu-pwa/dev: kifu-pwa/server: pushd kifu/pwa && make server +sgf/test: + pushd sgf && make test + sgf/test-oneshot: pushd sgf && make test-oneshot diff --git a/sgf/Cargo.lock b/sgf/Cargo.lock index bfdf38e..699220b 100644 --- a/sgf/Cargo.lock +++ b/sgf/Cargo.lock @@ -63,17 +63,6 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" -[[package]] -name = "go-sgf" -version = "0.1.0" -dependencies = [ - "chrono", - "nom", - "serde", - "thiserror", - "typeshare", -] - [[package]] name = "iana-time-zone" version = "0.1.57" @@ -216,6 +205,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sgf" +version = "0.1.0" +dependencies = [ + "chrono", + "nom", + "serde", + "thiserror", + "typeshare", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/sgf/src/go.rs b/sgf/src/go.rs index 79fab10..0597fbb 100644 --- a/sgf/src/go.rs +++ b/sgf/src/go.rs @@ -71,17 +71,12 @@ use crate::{ date::{self, parse_date_field, Date}, tree::{parse_collection, ParseSizeError, Size}, + Error, }; use serde::{Deserialize, Serialize}; use typeshare::typeshare; -#[derive(Debug)] -pub enum Error<'a> { - InvalidField, - InvalidBoardSize, - Incomplete, - InvalidSgf(nom::error::VerboseError<&'a str>), -} +pub struct Game {} impl<'a> From>> for Error<'a> { fn from(err: nom::Err>) -> Self { diff --git a/sgf/src/lib.rs b/sgf/src/lib.rs index db6f9a3..485267f 100644 --- a/sgf/src/lib.rs +++ b/sgf/src/lib.rs @@ -2,13 +2,20 @@ mod date; pub use date::Date; mod go; -pub use go::{parse_sgf, GameTree, GameType, Rank}; +pub use go::{GameTree, GameType, Rank}; mod tree; +use tree::parse_collection; use thiserror::Error; -pub enum Warning {} +#[derive(Debug)] +pub enum Error<'a> { + InvalidField, + InvalidBoardSize, + Incomplete, + InvalidSgf(nom::error::VerboseError<&'a str>), +} #[derive(Debug, PartialEq, Error)] pub enum ParseError { @@ -25,6 +32,22 @@ impl From> for ParseError { } } +pub enum Game { + Go(go::Game), + Unsupported(tree::Tree), +} + +pub fn parse_sgf<'a>(input: &'a str) -> Result, Error<'a>> { + let (_, trees) = parse_collection::>(input)?; + Ok(trees + .into_iter() + .map(|t| match t.sequence[0].find_prop("GM") { + Some(prop) if prop.values == vec!["1".to_owned()] => Game::Go(go::Game {}), + _ => Game::Unsupported(t), + }) + .collect::>()) +} + /* impl From<(&str, VerboseErrorKind)> for diff --git a/sgf/src/tree.rs b/sgf/src/tree.rs index 7c2681d..317be80 100644 --- a/sgf/src/tree.rs +++ b/sgf/src/tree.rs @@ -1,3 +1,4 @@ +use crate::Error; use nom::{ branch::alt, bytes::complete::{escaped_transform, tag},