Skip to main content

server/domain/scoring/
score_item.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    display::format_vec,
5    domain::{Card, Points, ScoreKind},
6};
7
8/// Represents a single scoring event in the game.
9///
10/// Each `ScoreItem` captures:
11/// - the type of score (`kind`),
12/// - the set of cards that contributed to the score (`cards`),
13/// - and the number of points awarded (`points`).
14#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
15pub struct ScoreItem {
16    kind: ScoreKind,
17    cards: Vec<Card>,
18    points: Points,
19}
20
21impl ScoreItem {
22    /// Constructs a new `ScoreItem` with the specified kind, contributing cards,
23    /// and points awarded.
24    pub fn new(kind: ScoreKind, cards: Vec<Card>, points: Points) -> Self {
25        Self {
26            kind,
27            cards,
28            points,
29        }
30    }
31
32    /// Returns the kind of score this item represents.
33    #[must_use]
34    pub fn kind(&self) -> ScoreKind {
35        self.kind
36    }
37
38    /// Returns an immutable reference to the cards that contributed to this scoring event.
39    #[must_use]
40    pub fn cards(&self) -> &Vec<Card> {
41        &self.cards
42    }
43
44    /// Returns the number of points awarded for this scoring item.
45    #[must_use]
46    pub fn points(&self) -> Points {
47        self.points
48    }
49}
50
51impl std::fmt::Display for ScoreItem {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        write!(
54            f,
55            "{}: ({}) -> {}",
56            self.kind.as_ref(),
57            format_vec(self.cards.as_slice()),
58            self.points
59        )
60    }
61}