Introduce the Game data structure

This commit is contained in:
Savanni D'Gerinel 2023-07-26 19:33:50 -04:00
parent c51fced23a
commit b9808a1862
5 changed files with 42 additions and 20 deletions

View File

@ -65,5 +65,8 @@ kifu-pwa/dev:
kifu-pwa/server: kifu-pwa/server:
pushd kifu/pwa && make server pushd kifu/pwa && make server
sgf/test:
pushd sgf && make test
sgf/test-oneshot: sgf/test-oneshot:
pushd sgf && make test-oneshot pushd sgf && make test-oneshot

22
sgf/Cargo.lock generated
View File

@ -63,17 +63,6 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "go-sgf"
version = "0.1.0"
dependencies = [
"chrono",
"nom",
"serde",
"thiserror",
"typeshare",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.57" version = "0.1.57"
@ -216,6 +205,17 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "sgf"
version = "0.1.0"
dependencies = [
"chrono",
"nom",
"serde",
"thiserror",
"typeshare",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.109" version = "1.0.109"

View File

@ -71,17 +71,12 @@
use crate::{ use crate::{
date::{self, parse_date_field, Date}, date::{self, parse_date_field, Date},
tree::{parse_collection, ParseSizeError, Size}, tree::{parse_collection, ParseSizeError, Size},
Error,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use typeshare::typeshare; use typeshare::typeshare;
#[derive(Debug)] pub struct Game {}
pub enum Error<'a> {
InvalidField,
InvalidBoardSize,
Incomplete,
InvalidSgf(nom::error::VerboseError<&'a str>),
}
impl<'a> From<nom::Err<nom::error::VerboseError<&'a str>>> for Error<'a> { impl<'a> From<nom::Err<nom::error::VerboseError<&'a str>>> for Error<'a> {
fn from(err: nom::Err<nom::error::VerboseError<&'a str>>) -> Self { fn from(err: nom::Err<nom::error::VerboseError<&'a str>>) -> Self {

View File

@ -2,13 +2,20 @@ mod date;
pub use date::Date; pub use date::Date;
mod go; mod go;
pub use go::{parse_sgf, GameTree, GameType, Rank}; pub use go::{GameTree, GameType, Rank};
mod tree; mod tree;
use tree::parse_collection;
use thiserror::Error; 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)] #[derive(Debug, PartialEq, Error)]
pub enum ParseError { pub enum ParseError {
@ -25,6 +32,22 @@ impl From<nom::error::Error<&str>> for ParseError {
} }
} }
pub enum Game {
Go(go::Game),
Unsupported(tree::Tree),
}
pub fn parse_sgf<'a>(input: &'a str) -> Result<Vec<Game>, Error<'a>> {
let (_, trees) = parse_collection::<nom::error::VerboseError<&'a str>>(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::<Vec<Game>>())
}
/* /*
impl From<(&str, VerboseErrorKind)> for impl From<(&str, VerboseErrorKind)> for

View File

@ -1,3 +1,4 @@
use crate::Error;
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::{escaped_transform, tag}, bytes::complete::{escaped_transform, tag},