Skip to content

Commit

Permalink
[abbadingo] added chessdefines module with pieces and colours definin…
Browse files Browse the repository at this point in the history
…itions
  • Loading branch information
fpiantini committed Oct 27, 2021
1 parent ec8ac4b commit c22c915
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 5 deletions.
95 changes: 95 additions & 0 deletions abbadingo/src/chessdefines.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//! Base definitions of items used in chess board related structures
//!
//! In this module there are for example definition for Chess pieces, and Army colours
use crate::error::AbbaDingoError;
use std::convert::TryFrom;

// ********************************************************************************
// ********************************************************************************
// ENUMs, STRUCTs, DEFINEs
// ********************************************************************************
// ********************************************************************************

/// The colour of a chess Army.
///
/// Army colour can be `White` or `Black`.
#[derive(Debug, Clone, Copy, FromPrimitive, PartialOrd, PartialEq, Eq)]
pub enum ArmyColour {
White,
Black,
}

/// The chess pieces.
///
/// The enum with the traditional chess pieces, from King to Pawn.
#[derive(Debug, Clone, Copy, FromPrimitive, PartialOrd, PartialEq, Eq)]
pub enum ChessPiece {
King,
Queen,
Bishop,
Knight,
Rook,
Pawn,
}

// ********************************************************************************
// ********************************************************************************
// FUNCTIONS / METHODS / TRAITS
// ********************************************************************************
// ********************************************************************************

/// Converts a [ChessPiece] into its String representation ("King", "Queen", ...,"pawn").
///
/// # Example
/// ```
/// # use abbadingo::chessdefines::*;
/// assert_eq!(Into::<String>::into(ChessPiece::Queen), "Queen");
/// assert_eq!(Into::<String>::into(ChessPiece::Pawn), "pawn");
/// ```
impl Into<String> for ChessPiece {
fn into(self) -> String {
format!(
"{}",
match self {
ChessPiece::King => "King",
ChessPiece::Queen => "Queen",
ChessPiece::Bishop => "Bishop",
ChessPiece::Knight => "Knight",
ChessPiece::Rook => "Rook",
ChessPiece::Pawn => "pawn",
}
)
//format!("{}", (self as u8 + 97) as char)
}
}

/// Tentatively convert a &str with a piece in chess notation format
/// to the corresponding [ChessPiece].
///
/// For valid values ("K", "Q", ...) this function returns the
/// corresponding Ok(ChessPiece), otherwise Err(AbbaDingoError::IllegalConversionToChessPiece)
/// is returned.
///
/// # Example
/// ```
/// # use std::convert::TryFrom;
/// # use abbadingo::chessdefines::*;
/// # use abbadingo::error::AbbaDingoError;
/// assert_eq!(ChessPiece::try_from("K"), Ok(ChessPiece::King));
/// assert_eq!(ChessPiece::try_from("p"), Err(AbbaDingoError::IllegalConversionToChessPiece));
/// ```
///
impl TryFrom<&str> for ChessPiece {
type Error = AbbaDingoError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"K" => Ok(ChessPiece::King),
"Q" => Ok(ChessPiece::Queen),
"B" => Ok(ChessPiece::Bishop),
"N" => Ok(ChessPiece::Knight),
"R" => Ok(ChessPiece::Rook),
_ => Err(AbbaDingoError::IllegalConversionToChessPiece),
}
}
}
13 changes: 8 additions & 5 deletions abbadingo/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ use thiserror::Error;
#[derive(Error, Debug, PartialEq)]
/// Enumerates all the possible errors returned by this library
pub enum AbbaDingoError {
/// Invalid operation on File requested.
/// Invalid operation on [File](crate::bbdefines::File) requested.
#[error("Invalid operation on File")]
InvalidOperationOnFile,
/// Invalid operation on Rank requested.
/// Invalid operation on [Rank](crate::bbdefines::Rank) requested.
#[error("Invalid operation on Rank")]
InvalidOperationOnRank,
/// Illegal conversion to File.
/// Illegal conversion to [File](crate::bbdefines::File).
#[error("Illegal conversion to File")]
IllegalConversionToFile,
/// Illegal conversion to Rank.
/// Illegal conversion to [Rank](crate::bbdefines::Rank).
#[error("Illegal conversion to Rank")]
IllegalConversionToRank,
/// Illegal conversion to Cell.
/// Illegal conversion to [Cell](crate::bbdefines::Cell).
#[error("Illegal conversion to Cell")]
IllegalConversionToCell,
/// Illegal conversion to [ChessPiece](crate::chessdefines::ChessPiece).
#[error("Illegal conversion to ChessPiece")]
IllegalConversionToChessPiece,
}
1 change: 1 addition & 0 deletions abbadingo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ extern crate num_derive;

pub mod bbdefines;
pub mod bitboard;
pub mod chessdefines;
pub mod error;
pub mod hexboard;

0 comments on commit c22c915

Please sign in to comment.