Skip to content

Commit

Permalink
Fix deserializing
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusCDE committed Oct 9, 2020
1 parent d6d1c93 commit 3cdf768
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
pub use crate::{Player, Square};
use anyhow::Result;
pub use pleco::{BitMove, Board, File, Piece, PieceType, Player as PlecoPlayer, Rank, SQ};
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ChessOutcome {
Checkmate { winner: Player },
Stalemate,
Expand Down
4 changes: 1 addition & 3 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl ChessRequest {
}
}
}

//#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum ChessUpdate {
/// Usually the Response to `ChessRequest::CurrentBoard`.
/// But may be sent at other points to synchronize state as well.
Expand Down
21 changes: 16 additions & 5 deletions src/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use serde_string_derive::SerdeDisplayFromStr;
use std::fmt;
use thiserror::Error;

const FILES: &[char] = &['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
const RANKS: &[char] = &['1', '2', '3', '4', '5', '6', '7', '8'];

#[derive(Error, Debug)]
pub enum SquareFormatError {
#[error("The square in not within the acceptable range (found: {found}, expected: between A1 and H8)")]
Expand Down Expand Up @@ -53,11 +56,11 @@ impl Square {
}

pub fn x(&self) -> u8 {
self.file() as u8
self.sq.file() as u8
}

pub fn y(&self) -> u8 {
self.rank() as u8
self.sq.rank() as u8
}
}

Expand All @@ -77,9 +80,6 @@ impl std::str::FromStr for Square {
type Err = SquareFormatError;

fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
const FILES: &[char] = &['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
const RANKS: &[char] = &['1', '2', '3', '4', '5', '6', '7', '8'];

let chars: Vec<_> = s.chars().collect();
if (chars.len() != 2) {
return Err(SquareFormatError::InvalidLength {
Expand Down Expand Up @@ -109,6 +109,17 @@ impl std::str::FromStr for Square {
}
}

impl fmt::Display for Square {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}{}",
FILES[self.x() as usize],
RANKS[self.y() as usize]
)
}
}

impl std::ops::Deref for Square {
type Target = SQ;

Expand Down

0 comments on commit 3cdf768

Please sign in to comment.