api/dto/user_id.rs
1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4/// Data transfer object for a user identifier, wrapping a UUID.
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
6#[repr(transparent)]
7#[serde(transparent)]
8pub struct UserIdDTO(Uuid);
9
10impl UserIdDTO {
11 /// Creates a new `UserIdDTO` using the domain's method of assigning ids.
12 #[must_use]
13 pub fn new() -> Self {
14 Self(Uuid::new_v4())
15 }
16
17 /// The inner uuid value.
18 pub fn value(self) -> Uuid {
19 self.0
20 }
21
22 /// A short name for the user.
23 pub fn short_name(&self) -> String {
24 String::from(&self.0.to_string()[..8])
25 }
26}
27
28impl Default for UserIdDTO {
29 fn default() -> Self {
30 Self(Uuid::nil())
31 }
32}
33
34impl std::fmt::Display for UserIdDTO {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 self.0.fmt(f)
37 }
38}
39
40// #[cfg(test)]
41// mod test {
42// use serde::Serialize;
43
44// use crate::UserIdDTO;
45
46// #[test]
47// fn user_id_can_be_serialized() {
48// let user_id_0 = UserIdDTO::new();
49// println!("{user_id_0}");
50// let user_id_1 = user_id_0.\;
51// println!("{user_id_1:?}");
52// todo!()
53// }
54// }