Skip to main content

stormchaser_model/
id.rs

1use nutype::nutype;
2use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
3use sqlx::{
4    decode::Decode,
5    encode::{Encode, IsNull},
6    postgres::{PgArgumentBuffer, PgTypeInfo, PgValueRef},
7    Postgres, Type,
8};
9use uuid::Uuid;
10
11macro_rules! impl_id_traits {
12    ($t:ident) => {
13        impl Type<Postgres> for $t {
14            fn type_info() -> PgTypeInfo {
15                <Uuid as Type<Postgres>>::type_info()
16            }
17        }
18
19        impl<'r> Decode<'r, Postgres> for $t {
20            fn decode(value: PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
21                let inner = <Uuid as Decode<'r, Postgres>>::decode(value)?;
22                Ok($t::new(inner))
23            }
24        }
25
26        impl<'q> Encode<'q, Postgres> for $t {
27            fn encode_by_ref(
28                &self,
29                buf: &mut PgArgumentBuffer,
30            ) -> Result<IsNull, sqlx::error::BoxDynError> {
31                <Uuid as Encode<'q, Postgres>>::encode_by_ref(&self.into_inner(), buf)
32            }
33        }
34
35        impl JsonSchema for $t {
36            fn schema_name() -> String {
37                stringify!($t).to_string()
38            }
39            fn json_schema(gen: &mut SchemaGenerator) -> Schema {
40                <Uuid as JsonSchema>::json_schema(gen)
41            }
42        }
43
44        impl utoipa::PartialSchema for $t {
45            fn schema() -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
46                utoipa::openapi::ObjectBuilder::new()
47                    .schema_type(utoipa::openapi::schema::Type::String)
48                    .format(Some(utoipa::openapi::schema::SchemaFormat::KnownFormat(
49                        utoipa::openapi::schema::KnownFormat::Uuid,
50                    )))
51                    .build()
52                    .into()
53            }
54        }
55
56        impl utoipa::ToSchema for $t {
57            fn name() -> std::borrow::Cow<'static, str> {
58                std::borrow::Cow::Borrowed(stringify!($t))
59            }
60        }
61
62        impl Default for $t {
63            fn default() -> Self {
64                Self::new(Uuid::nil())
65            }
66        }
67
68        impl $t {
69            pub fn new_v4() -> Self {
70                Self::new(Uuid::new_v4())
71            }
72        }
73    };
74}
75
76#[nutype(derive(
77    Debug,
78    Clone,
79    Copy,
80    PartialEq,
81    Eq,
82    Hash,
83    Serialize,
84    Deserialize,
85    Display,
86    FromStr
87))]
88pub struct RunId(Uuid);
89impl_id_traits!(RunId);
90
91#[nutype(derive(
92    Debug,
93    Clone,
94    Copy,
95    PartialEq,
96    Eq,
97    Hash,
98    Serialize,
99    Deserialize,
100    Display,
101    FromStr
102))]
103pub struct StepId(Uuid);
104impl_id_traits!(StepId);
105
106#[nutype(derive(
107    Debug,
108    Clone,
109    Copy,
110    PartialEq,
111    Eq,
112    Hash,
113    Serialize,
114    Deserialize,
115    Display,
116    FromStr
117))]
118pub struct StepInstanceId(Uuid);
119impl_id_traits!(StepInstanceId);
120
121#[nutype(derive(
122    Debug,
123    Clone,
124    Copy,
125    PartialEq,
126    Eq,
127    Hash,
128    Serialize,
129    Deserialize,
130    Display,
131    FromStr
132))]
133pub struct EventId(Uuid);
134impl_id_traits!(EventId);
135
136#[nutype(derive(
137    Debug,
138    Clone,
139    Copy,
140    PartialEq,
141    Eq,
142    Hash,
143    Serialize,
144    Deserialize,
145    Display,
146    FromStr
147))]
148pub struct RuleId(Uuid);
149impl_id_traits!(RuleId);
150
151#[nutype(derive(
152    Debug,
153    Clone,
154    Copy,
155    PartialEq,
156    Eq,
157    Hash,
158    Serialize,
159    Deserialize,
160    Display,
161    FromStr
162))]
163pub struct WebhookId(Uuid);
164impl_id_traits!(WebhookId);
165
166#[nutype(derive(
167    Debug,
168    Clone,
169    Copy,
170    PartialEq,
171    Eq,
172    Hash,
173    Serialize,
174    Deserialize,
175    Display,
176    FromStr
177))]
178pub struct BackendId(Uuid);
179impl_id_traits!(BackendId);
180
181#[nutype(derive(
182    Debug,
183    Clone,
184    Copy,
185    PartialEq,
186    Eq,
187    Hash,
188    Serialize,
189    Deserialize,
190    Display,
191    FromStr
192))]
193pub struct ArtifactId(Uuid);
194impl_id_traits!(ArtifactId);
195
196#[nutype(derive(
197    Debug,
198    Clone,
199    Copy,
200    PartialEq,
201    Eq,
202    Hash,
203    Serialize,
204    Deserialize,
205    Display,
206    FromStr
207))]
208pub struct TestReportId(Uuid);
209impl_id_traits!(TestReportId);
210
211#[nutype(derive(
212    Debug,
213    Clone,
214    Copy,
215    PartialEq,
216    Eq,
217    Hash,
218    Serialize,
219    Deserialize,
220    Display,
221    FromStr
222))]
223pub struct CronWorkflowId(Uuid);
224impl_id_traits!(CronWorkflowId);
225
226#[nutype(derive(
227    Debug,
228    Clone,
229    Copy,
230    PartialEq,
231    Eq,
232    Hash,
233    Serialize,
234    Deserialize,
235    Display,
236    FromStr
237))]
238pub struct StepDefinitionId(Uuid);
239impl_id_traits!(StepDefinitionId);