Skip to main content

server/domain/types/
game_id.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4/// Unique identifier for a game instance.
5///
6/// Uses uuid::v7, so temporally sortable.
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
8#[repr(transparent)]
9#[serde(transparent)]
10pub struct GameId(Uuid);
11
12impl GameId {
13    /// Generates a new, random `GameId` using a Uuid.
14    pub fn new() -> Self {
15        Self(Uuid::now_v7())
16    }
17
18    /// Inner uuid value of the GameId.
19    pub fn value(self) -> Uuid {
20        self.0
21    }
22}
23
24impl From<Uuid> for GameId {
25    fn from(value: Uuid) -> Self {
26        GameId(value)
27    }
28}
29
30impl std::str::FromStr for GameId {
31    type Err = uuid::Error;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        let uuid = Uuid::from_str(s)?;
35        Ok(GameId(uuid))
36    }
37}
38
39impl std::fmt::Display for GameId {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(f, "GameId({})", self.0)
42    }
43}
44
45// #[cfg(test)]
46// mod test {
47//     use super::*;
48//     use crate::test::filtered_assert;
49
50//     #[test]
51//     fn game_id_is_displayable() {
52//         filtered_assert(
53//             GameId::new().to_string(),
54//             |actual| insta::assert_snapshot!(actual, @"GameId(<uuid>)"),
55//         );
56//     }
57// }