33 lines
980 B
Rust
33 lines
980 B
Rust
|
// Eryn Wells <eryn@erynwells.me>
|
||
|
|
||
|
#[macro_export]
|
||
|
macro_rules! try_from_string {
|
||
|
($type:ty) => {
|
||
|
try_from_string!($type, &str);
|
||
|
try_from_string!($type, &String);
|
||
|
};
|
||
|
($type:ty, $from_type:ty) => {
|
||
|
impl TryFrom<$from_type> for $type {
|
||
|
type Error = $crate::errors::TryFromStrError;
|
||
|
|
||
|
fn try_from(value: $from_type) -> Result<Self, Self::Error> {
|
||
|
let first_char = value
|
||
|
.chars()
|
||
|
.nth(0)
|
||
|
.ok_or($crate::errors::TryFromStrError)?;
|
||
|
Self::try_from(first_char).map_err(|_| $crate::errors::TryFromStrError)
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#[macro_export]
|
||
|
macro_rules! piece {
|
||
|
($color:ident $shape:ident) => {
|
||
|
$crate::Piece::new($crate::Color::$color, $crate::Shape::$shape)
|
||
|
};
|
||
|
($color:ident $shape:ident on $square:ident) => {
|
||
|
$crate::PlacedPiece::new(piece!($color $shape), $crate::Square::$square)
|
||
|
}
|
||
|
}
|