Make games serializable through Serde

This commit is contained in:
Savanni D'Gerinel 2024-03-21 23:23:47 -04:00
parent fe082773e3
commit 89a289a1ae
3 changed files with 14 additions and 12 deletions

View File

@ -2,6 +2,7 @@ use crate::{
parser::{self, Annotation, Evaluation, Move, SetupInstr, Size, UnknownProperty},
Color, Date, GameResult, GameType,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, time::Duration};
use uuid::Uuid;
@ -36,7 +37,7 @@ pub enum GameNodeError {
ConflictingPosition,
}
#[derive(Clone, Debug, PartialEq, Default)]
#[derive(Clone, Debug, PartialEq, Default, Deserialize, Serialize)]
pub struct Player {
pub name: Option<String>,
pub rank: Option<String>,
@ -51,7 +52,7 @@ pub struct Player {
/// syntax issues, the result of the Game is to have a fully-understood game. However, this doesn't
/// (yet?) go quite to the level of apply the game type (i.e., this is Go, Chess, Yinsh, or
/// whatever).
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Game {
pub game_type: GameType,
@ -216,7 +217,7 @@ impl TryFrom<&parser::Tree> for Game {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum GameNode {
MoveNode(MoveNode),
SetupNode(SetupNode),
@ -317,7 +318,7 @@ impl TryFrom<&parser::Node> for GameNode {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct MoveNode {
id: Uuid,
color: Color,
@ -426,7 +427,7 @@ impl TryFrom<&parser::Node> for MoveNode {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct SetupNode {
id: Uuid,

View File

@ -9,6 +9,7 @@ use nom::{
multi::{many0, many1, separated_list1},
IResult, Parser,
};
use serde::{Deserialize, Serialize};
use std::{num::ParseIntError, time::Duration};
impl From<ParseSizeError> for Error {
@ -29,7 +30,7 @@ impl From<ParseIntError> for ParseSizeError {
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum Annotation {
BadMove,
DoubtfulMove,
@ -37,7 +38,7 @@ pub enum Annotation {
Tesuji,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum Evaluation {
Even,
GoodForBlack,
@ -147,7 +148,7 @@ impl ToString for GameType {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Size {
pub width: i32,
pub height: i32,
@ -200,7 +201,7 @@ pub struct Node {
pub next: Vec<Node>,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum SetupInstr {
Piece((Color, String)),
Clear(String),
@ -288,7 +289,7 @@ impl ToString for Node {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum Move {
Move(String),
Pass,

View File

@ -3,7 +3,7 @@ use thiserror::Error;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum GameType {
Go,
Othello,
@ -96,7 +96,7 @@ impl From<nom::error::Error<&str>> for ParseError {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub enum Color {
Black,
White,