chessfriend/core/src/macros.rs
Eryn Wells 8b2a3926b3 [core,board] Move board::piece to core
Break up types in core into finer grained modules.
Update all the imports.
2024-01-24 17:08:27 -08:00

32 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)
}
}