14
14
use std:: fmt;
15
15
use std:: str:: FromStr ;
16
16
17
- use thiserror:: Error ;
18
-
19
17
use crate :: interface:: { representable_type, RepresentableType , SquareType } ;
20
18
21
19
representable_type ! (
22
20
/// Square represents all the squares present on an Ataxx Board.
23
21
/// The index of each Square is equal to `rank-index * 8 + file-index`.
24
- enum Square : u8 {
22
+ super enum Square : u8 {
25
23
A1 B1 C1 D1 E1 F1 G1
26
24
A2 B2 C2 D2 E2 F2 G2
27
25
A3 B3 C3 D3 E3 F3 G3
@@ -37,168 +35,16 @@ impl SquareType for Square {
37
35
type Rank = Rank ;
38
36
}
39
37
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
-
91
38
representable_type ! (
92
39
/// File represents a file on the Ataxx Board. Each vertical column of Squares
93
40
/// 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 }
95
42
) ;
96
43
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
-
148
44
representable_type ! (
149
45
/// Rank represents a rank on the Ataxx Board. Each horizontal row of Squares
150
46
/// 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" ,
190
49
}
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