Skip to main content

server/domain/players/
dealer.rs

1use serde::{Deserialize, Serialize};
2
3use super::{Player, Pone};
4
5/// Represents the dealer player in a two-player game.
6///
7/// This type wraps a `Player` and provides convenient access to the
8/// corresponding dealer in the round.
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
10#[repr(transparent)]
11#[serde(transparent)]
12pub struct Dealer(Player);
13
14impl Dealer {
15    /// Returns the player who is the dealer.
16    pub const fn player(&self) -> Player {
17        self.0
18    }
19
20    /// Returns the opponent of the dealer, wrapped as the `Pone`.
21    pub fn opponent(&self) -> Pone {
22        Pone::from(self.0.opponent())
23    }
24}
25
26impl<T> std::ops::Index<Dealer> for [T] {
27    type Output = T;
28
29    fn index(&self, index: Dealer) -> &Self::Output {
30        &self[index.0]
31    }
32}
33
34impl<T> std::ops::Index<&Dealer> for [T] {
35    type Output = T;
36
37    fn index(&self, index: &Dealer) -> &Self::Output {
38        &self[index.0]
39    }
40}
41
42impl From<Player> for Dealer {
43    fn from(value: Player) -> Self {
44        Self(value)
45    }
46}
47
48impl std::fmt::Display for Dealer {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "Dealer({})", self.0)
51    }
52}