#[derive(Copy, Drop, PartialEq)]
struct Position {
file: u8, // Column a-h (0-7)
rank: u8, // Row 1-8 (0-7)
}
impl TupleTryIntoPosition of TryInto {
fn try_into(self: (u8, u8)) -> Option {
let (file_char, rank) = self;
// Validate rank is between 1 and 8
if rank 8 {
return None;
}
// Validate and convert file character (a-h) to number (0-7)
if file_char 'h' {
return None;
}
let file = file_char - 'a';
Some(Position {
file,
rank: rank - 1 // Convert 1-8 (chess notation) to 0-7 (internal index)
})
}
}
// Valid positions
let e4 = ('e', 4).try_into();
assert!(e4 == Some(Position { file: 4, rank: 3 }));
// Invalid positions
let invalid_file = ('x', 4).try_into();
let invalid_rank = ('a', 9).try_into();
assert!(invalid_file == None);
assert!(invalid_rank == None);