use std::fmt::{Debug, Display};
use crate::{Move, PlayerIndex, State};
pub type PlayResult<T, S, M, const P: usize> = Result<T, InvalidMove<S, M, P>>;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct InvalidMove<S, M, const P: usize> {
pub state: S,
pub player: PlayerIndex<P>,
pub the_move: M,
}
impl<S, M, const P: usize> InvalidMove<S, M, P> {
pub fn new(state: S, player: PlayerIndex<P>, the_move: M) -> Self {
InvalidMove {
state,
player,
the_move,
}
}
}
impl<S, M: Move, const P: usize> Display for InvalidMove<S, M, P> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let msg = format!(
"player {} played an invalid move: {:?}",
self.player, self.the_move
);
write!(fmt, "{}", msg)
}
}
impl<S: State, M: Move, const P: usize> std::error::Error for InvalidMove<S, M, P> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}