From 7e73782a7d51b40a558b74793c1d6c1abdea857d Mon Sep 17 00:00:00 2001 From: Botahamec Date: Thu, 8 Jul 2021 20:32:44 -0400 Subject: here's what i have so far --- model/src/color.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 model/src/color.rs (limited to 'model/src/color.rs') diff --git a/model/src/color.rs b/model/src/color.rs new file mode 100644 index 0000000..3dd2a64 --- /dev/null +++ b/model/src/color.rs @@ -0,0 +1,57 @@ +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; +use std::fmt::Display; + +/// The color of a piece +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub enum PieceColor { + Light, + Dark, +} + +impl Display for PieceColor { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!( + f, + "{}", + match self { + Self::Light => "Light", + Self::Dark => "Dark", + } + ) + } +} + +impl PieceColor { + pub const fn flip(self) -> Self { + // TODO optimize + match self { + PieceColor::Light => PieceColor::Dark, + PieceColor::Dark => PieceColor::Light, + } + } + + pub const fn flip_if(self, statement: bool) -> Self { + if statement { + self.flip() + } else { + self + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn light_display() { + assert_eq!(PieceColor::Light.to_string(), "Light"); + } + + #[test] + fn dark_display() { + assert_eq!(PieceColor::Dark.to_string(), "Dark"); + } +} -- cgit v1.2.3