aboutsummaryrefslogtreecommitdiff
path: root/src/square.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/square.rs')
-rw-r--r--src/square.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/square.rs b/src/square.rs
index efa682c..e518a92 100644
--- a/src/square.rs
+++ b/src/square.rs
@@ -1,3 +1,5 @@
+use std::str::Chars;
+
use crate::bitboard::Bitboard;
/// Aliases to board square indexes
@@ -49,6 +51,36 @@ impl Square {
pub fn east_one(&self) -> Self {
Self::from(*self as u8 + 1)
}
+
+ pub fn from_notation(chars: &mut Chars) -> Result<Self, String> {
+ let file = match chars.next() {
+ Some(ch) => {
+ match ch {
+ 'a' => 0,
+ 'b' => 1,
+ 'c' => 2,
+ 'd' => 3,
+ 'e' => 4,
+ 'f' => 5,
+ 'g' => 6,
+ 'h' => 7,
+ _ => return Err(String::from("Incorrect file!"))
+ }
+ },
+ None => return Err(String::from("Missing file"))
+ };
+ let rank = match chars.next() {
+ Some(ch) => {
+ match ch.to_digit(10) {
+ Some(digit) => digit - 1,
+ None => return Err(String::from("Incorrect rank"))
+ }
+ }
+ None => return Err(String::from("Missing rank"))
+ };
+
+ Ok(Self::from_coords(rank as u8, file))
+ }
}