Skip to main content

api/dto/
game_id.rs

1use serde::{Deserialize, Serialize};
2// use server::domain::GameId;
3use uuid::Uuid;
4
5use crate::dto::DTOError;
6
7/// Data transfer object for a game identifier, wrapping a ULID for unique, sortable identification.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
9#[repr(transparent)]
10#[serde(transparent)]
11pub struct GameIdDTO(Uuid);
12
13impl GameIdDTO {
14    /// Internal value of the id.
15    pub fn value(self) -> Uuid {
16        self.0
17    }
18}
19
20impl Default for GameIdDTO {
21    fn default() -> Self {
22        Self(Uuid::nil())
23    }
24}
25
26impl std::fmt::Display for GameIdDTO {
27    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28        self.0.fmt(f)
29    }
30}
31
32impl std::str::FromStr for GameIdDTO {
33    type Err = DTOError;
34
35    fn from_str(s: &str) -> Result<Self, Self::Err> {
36        let uuid = Uuid::from_str(s)?;
37        Ok(GameIdDTO(uuid))
38    }
39}
40
41impl From<Uuid> for GameIdDTO {
42    fn from(value: Uuid) -> Self {
43        Self(value)
44    }
45}