diff --git a/board/src/square.rs b/board/src/square.rs index d380b29..0ad9d09 100644 --- a/board/src/square.rs +++ b/board/src/square.rs @@ -237,6 +237,23 @@ impl FromStr for Square { } } +macro_rules! try_from_integer { + ($int_type:ident) => { + impl TryFrom<$int_type> for Square { + type Error = SquareOutOfBoundsError; + + fn try_from(value: $int_type) -> Result { + Square::try_index(value as usize).ok_or(SquareOutOfBoundsError) + } + } + }; +} + +try_from_integer!(u8); +try_from_integer!(u16); +try_from_integer!(u32); +try_from_integer!(u64); + impl fmt::Display for Square { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(