Set up a per-game interpretation layer atop the SGF, and bind all games together in a Game data structure #49

Merged
savanni merged 6 commits from feature/sgf-interpretation-layer into main 2023-07-27 22:20:24 +00:00
5 changed files with 42 additions and 20 deletions
Showing only changes of commit b9808a1862 - Show all commits

View File

@ -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

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"
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"

View File

@ -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<nom::Err<nom::error::VerboseError<&'a str>>> for Error<'a> {
fn from(err: nom::Err<nom::error::VerboseError<&'a str>>) -> Self {

View File

@ -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<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

View File

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