Skip to main content

server/domain/types/
user_id.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4/// Identifier for a user/account.
5#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
6#[repr(transparent)]
7#[serde(transparent)]
8pub struct UserId(Uuid);
9
10impl UserId {
11    /// Generates a new random version-4 UUID.
12    #[must_use]
13    #[inline]
14    pub fn new() -> Self {
15        Self::from(Uuid::new_v4())
16    }
17
18    /// Returns the inner [`Uuid`].
19    #[inline]
20    #[must_use]
21    pub fn value(self) -> Uuid {
22        self.0
23    }
24}
25
26impl From<Uuid> for UserId {
27    fn from(value: Uuid) -> Self {
28        Self(value)
29    }
30}
31
32impl std::fmt::Display for UserId {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(f, "UserId({})", self.0)
35    }
36}
37
38// #[cfg(test)]
39// mod test {
40//     use super::*;
41//     use crate::test::filtered_assert;
42
43//     #[test]
44//     fn user_id_is_displayable() {
45//         filtered_assert(
46//             UserId::new().to_string(),
47//             |actual| insta::assert_snapshot!(actual, @"UserId(<uuid>)"),
48//         );
49//     }
50// }