Skip to content

Commit d4d5919

Browse files
committed
HAHAHAHA MACRO POWERRRRR
1 parent 2bf6bf8 commit d4d5919

File tree

5 files changed

+50
-258
lines changed

5 files changed

+50
-258
lines changed

games/src/ataxx/move.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::str::FromStr;
1616

1717
use thiserror::Error;
1818

19-
use crate::ataxx::{Square, SquareParseError};
20-
use crate::interface::{MoveType, RepresentableType};
19+
use crate::ataxx::Square;
20+
use crate::interface::{MoveType, RepresentableType, TypeParseError};
2121

2222
/// Move represents an Ataxx move which can be played on the Board.
2323
#[derive(Copy, Clone, PartialEq, Eq, Default)]
@@ -153,7 +153,7 @@ pub enum MoveParseError {
153153
#[error("length of move string should be 2 or 4, not {0}")]
154154
BadLength(usize),
155155
#[error("bad source square string \"{0}\"")]
156-
BadSquare(#[from] SquareParseError),
156+
BadSquare(#[from] TypeParseError),
157157
}
158158

159159
impl FromStr for Move {

games/src/ataxx/piece.rs

+2-92
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ use std::fmt;
1515
use std::ops;
1616
use std::str::FromStr;
1717

18-
use thiserror::Error;
19-
2018
use crate::interface::ColoredPieceType;
2119
use crate::interface::RepresentableType;
2220
use crate::representable_type;
2321

2422
representable_type!(
2523
/// Color represents all the possible colors that an ataxx piece can have,
2624
/// specifically, Black and White.
27-
enum Color: u8 { Black White }
25+
enum Color: u8 { Black "x", White "o", }
2826
);
2927

3028
impl ops::Not for Color {
@@ -37,52 +35,9 @@ impl ops::Not for Color {
3735
}
3836
}
3937

40-
#[derive(Error, Debug)]
41-
pub enum ColorParseError {
42-
#[error("color identifier string has more than 1 character")]
43-
StringTooLong,
44-
#[error("unknown color identifier '{0}'")]
45-
StringFormatInvalid(String),
46-
}
47-
48-
impl fmt::Display for Color {
49-
/// Implements displaying the Color in a human-readable form. [`Color::Black`]
50-
/// is formatted as `x` and [`Color::White`] is formatted as `o`.
51-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52-
write!(
53-
f,
54-
"{}",
55-
match *self {
56-
Self::Black => "x",
57-
Self::White => "o",
58-
}
59-
)
60-
}
61-
}
62-
63-
impl FromStr for Color {
64-
type Err = ColorParseError;
65-
66-
/// from_str converts the given human-readable string into its corresponding
67-
/// [`Color`]. `x`, `X`, `b`, `B` are parsed as [`Color::Black`] and `o`, `O`,
68-
/// `w`, `W` are parsed as [`Color::White`]. Best practice is to use `x` and `o`
69-
/// respectively for Black and White.
70-
fn from_str(s: &str) -> Result<Self, Self::Err> {
71-
if s.len() != 1 {
72-
return Err(ColorParseError::StringTooLong);
73-
}
74-
75-
match s {
76-
"x" | "X" | "b" | "B" => Ok(Color::Black),
77-
"o" | "O" | "w" | "W" => Ok(Color::White),
78-
_ => Err(ColorParseError::StringFormatInvalid(s.to_string())),
79-
}
80-
}
81-
}
82-
8338
representable_type!(
8439
/// Piece represents all the possible ataxx pieces.
85-
enum ColoredPiece: u8 { Black White Block }
40+
enum ColoredPiece: u8 { Black "x", White "o", Block "■", }
8641
);
8742

8843
impl ColoredPieceType for ColoredPiece {
@@ -105,48 +60,3 @@ impl ColoredPieceType for ColoredPiece {
10560
}
10661
}
10762
}
108-
109-
#[derive(Error, Debug)]
110-
pub enum PieceParseError {
111-
#[error("piece identifier string has more than 1 character")]
112-
StringTooLong,
113-
#[error("unknown piece identifier '{0}'")]
114-
StringFormatInvalid(String),
115-
}
116-
117-
impl FromStr for ColoredPiece {
118-
type Err = ColorParseError;
119-
120-
/// from_str converts the given human-readable string into its corresponding
121-
/// [`Color`]. `x`, `X`, `b`, `B` are parsed as [`Color::Black`] and `o`, `O`,
122-
/// `w`, `W` are parsed as [`Color::White`]. Best practice is to use `x` and `o`
123-
/// respectively for Black and White.
124-
fn from_str(s: &str) -> Result<Self, Self::Err> {
125-
if s.len() != 1 {
126-
return Err(ColorParseError::StringTooLong);
127-
}
128-
129-
match s {
130-
"x" | "X" | "b" | "B" => Ok(ColoredPiece::Black),
131-
"o" | "O" | "w" | "W" => Ok(ColoredPiece::White),
132-
_ => Err(ColorParseError::StringFormatInvalid(s.to_string())),
133-
}
134-
}
135-
}
136-
137-
impl fmt::Display for ColoredPiece {
138-
/// Implements displaying the Piece in a human-readable form.
139-
/// [`ColoredPiece::Black`] is formatted as `x` and [`ColoredPiece::White`]
140-
/// is formatted as `o`.
141-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142-
write!(
143-
f,
144-
"{}",
145-
match *self {
146-
Self::Black => "x",
147-
Self::White => "o",
148-
Self::Block => "■",
149-
}
150-
)
151-
}
152-
}

games/src/ataxx/position.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ use std::str::FromStr;
1919
use strum::IntoEnumIterator;
2020

2121
use crate::interface::PositionType;
22+
use crate::interface::TypeParseError;
2223
use crate::interface::{BitBoardType, RepresentableType, SquareType};
2324

2425
use thiserror::Error;
2526

2627
#[rustfmt::skip]
2728
use crate::ataxx::{
2829
BitBoard, ColoredPiece, File, Hash, Move,
29-
Rank, Square,
30-
ColorParseError, Color
30+
Rank, Square, Color
3131
};
3232
use crate::interface::MoveStore;
3333

@@ -297,7 +297,7 @@ pub enum PositionParseError {
297297
TooManyRanks,
298298

299299
#[error("parsing side to move: {0}")]
300-
BadSideToMove(#[from] ColorParseError),
300+
BadSideToMove(#[from] TypeParseError),
301301
#[error("parsing half-move clock: {0}")]
302302
BadHalfMoveClock(#[from] ParseIntError),
303303
}

games/src/ataxx/square.rs

+5-159
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
use std::fmt;
1515
use std::str::FromStr;
1616

17-
use thiserror::Error;
18-
1917
use crate::interface::{representable_type, RepresentableType, SquareType};
2018

2119
representable_type!(
2220
/// Square represents all the squares present on an Ataxx Board.
2321
/// The index of each Square is equal to `rank-index * 8 + file-index`.
24-
enum Square: u8 {
22+
super enum Square: u8 {
2523
A1 B1 C1 D1 E1 F1 G1
2624
A2 B2 C2 D2 E2 F2 G2
2725
A3 B3 C3 D3 E3 F3 G3
@@ -37,168 +35,16 @@ impl SquareType for Square {
3735
type Rank = Rank;
3836
}
3937

40-
/// SquareParseError represents the various errors that can
41-
/// be encountered while parsing a given string into a Square.
42-
#[derive(Error, Debug)]
43-
pub enum SquareParseError {
44-
#[error("wrong square string size")]
45-
WrongStringSize,
46-
#[error("{0}")]
47-
FileParseError(#[from] FileParseError),
48-
#[error("{0}")]
49-
RankParseError(#[from] RankParseError),
50-
}
51-
52-
impl FromStr for Square {
53-
type Err = SquareParseError;
54-
55-
/// from_str converts a square given in the format `<file><rank>` into
56-
/// a Square. For the formats of `<file>` and `<rank>` see the documentation
57-
/// for [`File::FromStr`](File::from_str) and [`Rank::FromStr`](Rank::from_str).
58-
/// It is effectively the inverse operation of Display.
59-
/// ```
60-
/// use ataxx::*;
61-
/// use std::str::FromStr;
62-
///
63-
/// assert_eq!(Square::from_str("a1").unwrap(), Square::A1);
64-
/// ```
65-
fn from_str(s: &str) -> Result<Self, Self::Err> {
66-
if s.len() != 2 {
67-
return Err(SquareParseError::WrongStringSize);
68-
}
69-
70-
let file = File::from_str(&s[..1])?; // Parse the File specification.
71-
let rank = Rank::from_str(&s[1..])?; // Parse the Rank specification.
72-
73-
Ok(SquareType::new(file, rank))
74-
}
75-
}
76-
77-
impl fmt::Display for Square {
78-
/// Display formats the given Square in the format `<file><rank>`. For how
79-
/// `<file>` and `<rank>` are formatted, see the documentation for
80-
/// `File::Display` and `Rank::Display` trait implementations.
81-
/// ```
82-
/// use ataxx::*;
83-
///
84-
/// assert_eq!(Square::A1.to_string(), "a1");
85-
/// ```
86-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
87-
write!(f, "{}{}", self.file(), self.rank())
88-
}
89-
}
90-
9138
representable_type!(
9239
/// File represents a file on the Ataxx Board. Each vertical column of Squares
9340
/// on an Ataxx Board is known as a File. There are 7 of them in total.
94-
enum File: u8 { A B C D E F G }
41+
super enum File: u8 { A B C D E F G }
9542
);
9643

97-
/// FileParseError represents the various errors that can
98-
/// be encountered while parsing a given string into a File.
99-
#[derive(Error, Debug)]
100-
pub enum FileParseError {
101-
#[error("wrong string size for file identifier")]
102-
WrongStringSize,
103-
#[error("invalid file identifier string")]
104-
InvalidFileString,
105-
}
106-
107-
impl FromStr for File {
108-
type Err = FileParseError;
109-
110-
/// from_str converts the given string representation of a File into its
111-
/// corresponding File value. String representations are lowercase alphabets
112-
/// from a to g which represent Files from [`File::A`] to [`File::G`].
113-
/// ```
114-
/// use ataxx::*;
115-
/// use std::str::FromStr;
116-
///
117-
/// assert_eq!(File::from_str("a").unwrap(), File::A);
118-
/// ```
119-
fn from_str(s: &str) -> Result<Self, Self::Err> {
120-
if s.len() != 1 {
121-
return Err(FileParseError::WrongStringSize);
122-
}
123-
124-
let ident = s.chars().next().unwrap() as u8;
125-
126-
// File identifier should be one of a..h.
127-
if !(b'a'..=b'h').contains(&ident) {
128-
return Err(FileParseError::InvalidFileString);
129-
}
130-
131-
Ok(unsafe { File::unsafe_from(ident - b'a') })
132-
}
133-
}
134-
135-
impl fmt::Display for File {
136-
/// Display formats the given File into a string. Specifically,
137-
/// it formats the File into a lowercase letter representing that File.
138-
/// ```
139-
/// use ataxx::*;
140-
///
141-
/// assert_eq!(File::A.to_string(), "a");
142-
/// ```
143-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
144-
write!(f, "{}", (b'a' + *self as u8) as char)
145-
}
146-
}
147-
14844
representable_type!(
14945
/// Rank represents a rank on the Ataxx Board. Each horizontal row of Squares
15046
/// on an Ataxx Board is known as a Rank. There are 7 of them in total.
151-
enum Rank: u8 { First Second Third Fourth Fifth Sixth Seventh }
152-
);
153-
154-
/// RankParseError represents the various errors that can
155-
/// be encountered while parsing a given string into a Rank.
156-
#[derive(Error, Debug)]
157-
pub enum RankParseError {
158-
#[error("wrong string size for rank identifier")]
159-
WrongStringSize,
160-
#[error("invalid rank identifier string")]
161-
InvalidRankString,
162-
}
163-
164-
impl FromStr for Rank {
165-
type Err = RankParseError;
166-
167-
/// from_str converts the given string representation of a Rank into its
168-
/// corresponding Rank value. String representations are single digit long
169-
/// decimal digits from 1 to 7 which represent the Ranks from
170-
/// [`Rank::First`] to [`Rank::Seventh`] respectively.
171-
/// ```
172-
/// use ataxx::*;
173-
/// use std::str::FromStr;
174-
///
175-
/// assert_eq!(Rank::from_str("1").unwrap(), Rank::First);
176-
/// ```
177-
fn from_str(s: &str) -> Result<Self, Self::Err> {
178-
if s.len() != 1 {
179-
return Err(RankParseError::WrongStringSize);
180-
}
181-
182-
let ident = s.chars().next().unwrap() as u8;
183-
184-
// Rank identifier should be one of 1..8.
185-
if !(b'1'..=b'8').contains(&ident) {
186-
return Err(RankParseError::InvalidRankString);
187-
}
188-
189-
Ok(unsafe { Rank::unsafe_from(ident - b'1') })
47+
enum Rank: u8 {
48+
First "1", Second "2", Third "3", Fourth "4", Fifth "5", Sixth "6", Seventh "7",
19049
}
191-
}
192-
193-
impl fmt::Display for Rank {
194-
/// Display formats the given Rank into a string. Specifically, it formats
195-
/// the Rank into a numerical digit from 1-7 representing that Rank.
196-
/// ```
197-
/// use ataxx::*;
198-
///
199-
/// assert_eq!(Rank::First.to_string(), "1");
200-
/// ```
201-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
202-
write!(f, "{}", *self as usize + 1)
203-
}
204-
}
50+
);

0 commit comments

Comments
 (0)