fours 0.1.0

Four in a row in your terminal, with three levels of opponent.
Documentation
use std::fmt;
use std::str::FromStr;

/// How hard the opponent plays.
///
/// Each level names a way of playing rather than a strength number, so that the opponent behind it
/// can be replaced without the level meaning something else. Which bot plays at which level is
/// decided in [`Opponent`](crate::bot::Opponent).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Difficulty {
    /// Plays a random legal move.
    Easy,
    /// Takes a win, blocks yours, and otherwise plays towards the middle.
    #[default]
    Medium,
    /// Looks several moves ahead.
    Hard,
}

impl Difficulty {
    /// Every level, from the easiest to the hardest.
    pub const ALL: [Self; 3] = [Self::Easy, Self::Medium, Self::Hard];

    /// The name of the level.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Easy => "Easy",
            Self::Medium => "Medium",
            Self::Hard => "Hard",
        }
    }

    /// The next level up, starting again at the easiest one after the hardest.
    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Easy => Self::Medium,
            Self::Medium => Self::Hard,
            Self::Hard => Self::Easy,
        }
    }
}

impl fmt::Display for Difficulty {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.name())
    }
}

impl FromStr for Difficulty {
    type Err = UnknownDifficulty;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Self::ALL
            .into_iter()
            .find(|level| level.name().eq_ignore_ascii_case(text))
            .ok_or(UnknownDifficulty)
    }
}

/// The text did not name a [`Difficulty`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnknownDifficulty;

impl fmt::Display for UnknownDifficulty {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("expected one of easy, medium or hard")
    }
}

impl std::error::Error for UnknownDifficulty {}