Skip to main content

server/domain/scoring/
pegging.rs

1use serde::{Deserialize, Serialize};
2
3use crate::domain::{Player, ScoreSheet};
4
5/// Represents pegging information for a specific player during scoring.
6///
7/// Pegging is the process of awarding points during the play
8/// sequence but is also used here for pegging of the pone hand, dealer hand
9/// and crib.
10///
11/// This structure associates a player with the score sheet recording their
12/// pegging-related points.
13#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14pub struct Pegging {
15    recipient: Player,
16    sheet: ScoreSheet,
17}
18
19/// Trait for types that contain pegging information.
20pub trait HasPegging {
21    /// Returns an immutable reference to optional pegging information.
22    /// This is used to provide pegging scores for hands and crib.
23    /// The optional return allows for the most recent "play" pegging to be returned,
24    /// in the case where no scoring play has been made yet.
25    fn pegging(&self) -> Option<&Pegging>;
26}
27
28impl Pegging {
29    /// Constructs a new pegging record for the specified `player`, using the
30    /// provided `sheet` as the underlying scoring record.
31    ///
32    /// Callers are responsible for ensuring that the `sheet` is appropriate
33    /// for pegging-scoring usage.
34    pub fn new(player: Player, sheet: ScoreSheet) -> Self {
35        Self {
36            recipient: player,
37            sheet,
38        }
39    }
40
41    /// Returns an immutable reference to the player associated with this pegging record.
42    #[must_use]
43    pub fn recipient(&self) -> &Player {
44        &self.recipient
45    }
46
47    /// Returns an immutable reference to the score sheet that records pegging-related points.
48    #[must_use]
49    pub fn score_sheet(&self) -> &ScoreSheet {
50        &self.sheet
51    }
52}
53
54impl std::fmt::Display for Pegging {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(f, "{} -> {}", self.recipient, self.sheet)
57    }
58}