game:mode: A simple enum to represent the different stages of a round

Specifically at the stage of the game where players may start
presenting Items (i.e. a round), this enum allows us to validate if an
Item should be played at that round stage: this is called a mode.
This commit is contained in:
2026-04-16 21:15:29 +01:00
parent 976e9bf7b6
commit 804c632c10
2 changed files with 54 additions and 0 deletions

View File

@@ -1,2 +1,3 @@
pub mod deck; pub mod deck;
pub mod mode;
pub mod playerbuilder; pub mod playerbuilder;

53
src/game/mode.rs Normal file
View File

@@ -0,0 +1,53 @@
use crate::{
card::Card,
modes::item::{Item, ItemParseError},
};
#[derive(Debug, Clone, PartialEq, Eq)]
/// All possible stages of a Round where an Item may be presented by a player,
/// distinguished by Mode (Single, Pair, Triples, etc). `Any` represents the
/// "decision" stage where a single player may play any Item of any of the
/// available Round Modes to start off the new round.
pub enum Mode {
Any,
Single,
Pair,
Triple,
}
#[derive(Debug, Clone, PartialEq, Eq)]
/// Types of errors which may occur when validating some set of cards against a
/// Mode.
pub enum ModeValidateError {
ParseError(ItemParseError),
ModeMismatch,
}
impl Mode {
/// Given mode `self`, validate if `cards` creates a valid Item for that
/// mode.
/// Returns `Ok(Item)` if so, otherwise return `Err(ModeValidateError)`.
pub fn validate_cards(
&self,
cards: &[Card],
) -> Result<Item, ModeValidateError> {
let item = Item::parse(cards).map_err(ModeValidateError::ParseError)?;
if *self == Mode::Any || *self == item.mode() {
Ok(item)
} else {
Err(ModeValidateError::ModeMismatch)
}
}
}
impl Item {
/// Return the Mode we'd expect this Item to be played in - trivial enum
/// conversion.
fn mode(&self) -> Mode {
match self {
Self::Single(_) => Mode::Single,
Self::Pair(_) => Mode::Pair,
Self::Triple(_) => Mode::Triple,
}
}
}