sqlx_ledger/
macros.rs

1#[macro_export]
2macro_rules! entity_id {
3    ($name:ident) => {
4        #[derive(
5            sqlx::Type,
6            Debug,
7            Clone,
8            Copy,
9            PartialEq,
10            Eq,
11            PartialOrd,
12            Ord,
13            Hash,
14            serde::Deserialize,
15            serde::Serialize,
16        )]
17        #[serde(transparent)]
18        #[sqlx(transparent)]
19        pub struct $name(uuid::Uuid);
20
21        impl $name {
22            #[allow(clippy::new_without_default)]
23            pub fn new() -> Self {
24                uuid::Uuid::new_v4().into()
25            }
26        }
27
28        impl From<uuid::Uuid> for $name {
29            fn from(uuid: uuid::Uuid) -> Self {
30                Self(uuid)
31            }
32        }
33
34        impl From<$name> for uuid::Uuid {
35            fn from(id: $name) -> Self {
36                id.0
37            }
38        }
39
40        impl From<&$name> for uuid::Uuid {
41            fn from(id: &$name) -> Self {
42                id.0
43            }
44        }
45
46        impl From<$name> for cel_interpreter::CelValue {
47            fn from(id: $name) -> Self {
48                cel_interpreter::CelValue::Uuid(id.0)
49            }
50        }
51
52        impl std::fmt::Display for $name {
53            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54                write!(f, "{}", self.0)
55            }
56        }
57
58        impl std::str::FromStr for $name {
59            type Err = uuid::Error;
60
61            fn from_str(s: &str) -> Result<Self, Self::Err> {
62                Ok(Self(uuid::Uuid::parse_str(s)?))
63            }
64        }
65    };
66}