benzina 0.5.1

Various helper types and macros for `diesel`
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/// Creates one or more new types that wrap a [`Uuid`].
///
/// This macro creates one or more `UUID` newtypes for which you can specify the name. The macro
/// generates code which implements many useful traits, including the ones needed to work with
/// [`diesel`].
///
/// The generated structs do not expose any method or trait to create an arbitrary instance[^See note], in
/// order to provide the guarantee that the `UUID` is valid. However, it is possible to choose to
/// add traits and methods to customize the behavior.
#[cfg_attr(
    not(feature = "example-generated"),
    doc = "To see the documentation of a generated typed `UUID`, consider re-building the \
        documentation with the feature `example-generated` enabled."
)]
#[cfg_attr(
    feature = "example-generated",
    doc = "You can see the documentation for an example generated `UUID` struct \
        [`here`](crate::example_generated::FooId)."
)]
///
/// [`Uuid`]: ::uuid::Uuid
///
/// # Examples
///
/// The usage of the macro is pretty straightforward: you specify the name of the type, and it is
/// also possible to add attributes as needed.
///
/// ```
/// use benzina::typed_uuid;
/// use uuid::Uuid;
///
/// typed_uuid! (
///     /// You can add documentation.
///     FooId,
///
///     // Attributes and visibility work
///     #[expect(dead_code)]
///     #[derive(Default)]
///     pub BarId,
/// );
///
/// fn use_foo(foo_id: FooId) {
///     // Implements and derives many traits like `Display`
///     println!("{foo_id}");
///
///     let _actual_uuid: Uuid = foo_id.get();
/// }
/// ```
///
/// Keep in mind that there is no way[^See note] to construct an instance unless the instantiation is done
/// inside the module containing the new type or a submodule of it:
///
/// ```compile_fail,E0603
/// use uuid::Uuid;
///
/// mod inner {
///     benzina::typed_uuid!(pub Foo);
/// }
///
/// // Error E0603: a constructor is private if any of the fields is private
/// let foo = inner::Foo(Uuid::default());
/// //               ^^^ private tuple struct constructor
/// ```
///
/// [^See note]: There is no way in normal usage to construct an instance. The exception is with the
/// `dangerous_new` method, which is gated behind the `dangerous-construction` feature and intended
/// for special cases (including testing). If the `dangerous-construction` feature is enabled, it is
/// recommended to use [`clippy::disallowed_methods`](https://rust-lang.github.io/rust-clippy/stable/index.html#disallowed_methods) to prevent the usage of `dangerous_new` outside
/// of the desired situations.
#[macro_export]
macro_rules! typed_uuid {
    (
        $(
            $(#[$attr:meta])*
            $vis:vis $name:ident
        ),+ $(,)?
    ) => {
        $(
            $(#[$attr])*
            #[derive(
                $crate::__private::std::fmt::Debug,
                $crate::__private::std::clone::Clone,
                $crate::__private::std::marker::Copy,
                $crate::__private::std::cmp::PartialEq,
                $crate::__private::std::cmp::Eq,
                $crate::__private::std::cmp::PartialOrd,
                $crate::__private::std::cmp::Ord,
                $crate::__private::std::hash::Hash,
            )]
            $vis struct $name($crate::__private::uuid::Uuid);

            impl $name {
                $crate::__typed_uuid__impl_dangerous_construction!($vis);

                /// Gets the actual `Uuid`.
                #[must_use]
                #[allow(unused)]
                $vis fn get(&self) -> $crate::__private::uuid::Uuid {
                    self.0
                }
            }

            impl $crate::__private::diesel::deserialize::FromSql<$crate::__private::diesel::pg::sql_types::Uuid, $crate::__private::diesel::pg::Pg> for $name {
                fn from_sql(value: $crate::__private::diesel::pg::PgValue<'_>) -> $crate::__private::diesel::deserialize::Result<Self> {
                    $crate::__private::uuid::Uuid::from_slice(value.as_bytes())
                        .map(Self)
                        .map_err(Into::into)
                }
            }

            impl $crate::__private::diesel::serialize::ToSql<$crate::__private::diesel::pg::sql_types::Uuid, $crate::__private::diesel::pg::Pg> for $name {
                fn to_sql<'b>(&'b self, out: &mut $crate::__private::diesel::serialize::Output<'b, '_, $crate::__private::diesel::pg::Pg>) -> $crate::__private::diesel::serialize::Result {
                    $crate::__private::std::io::Write::write_all(out, self.0.as_bytes())
                        .map(|_| $crate::__private::diesel::serialize::IsNull::No)
                        .map_err(Into::into)
                }
            }

            // These are manually implemented because the derive macro uses `diesel` instead of the
            // private path.
            impl<
                '__expr,
            > $crate::__private::diesel::expression::AsExpression<$crate::__private::diesel::pg::sql_types::Uuid>
            for &'__expr $name {
                type Expression = $crate::__private::diesel::internal::derives::as_expression::Bound<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                    Self,
                >;
                fn as_expression(
                    self,
                ) -> <Self as $crate::__private::diesel::expression::AsExpression<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                >>::Expression {
                    $crate::__private::diesel::internal::derives::as_expression::Bound::new(self)
                }
            }
            impl<
                '__expr,
            > $crate::__private::diesel::expression::AsExpression<
                $crate::__private::diesel::sql_types::Nullable<$crate::__private::diesel::pg::sql_types::Uuid>,
            > for &'__expr $name {
                type Expression = $crate::__private::diesel::internal::derives::as_expression::Bound<
                    $crate::__private::diesel::sql_types::Nullable<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                    >,
                    Self,
                >;
                fn as_expression(
                    self,
                ) -> <Self as $crate::__private::diesel::expression::AsExpression<
                    $crate::__private::diesel::sql_types::Nullable<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                    >,
                >>::Expression {
                    $crate::__private::diesel::internal::derives::as_expression::Bound::new(self)
                }
            }
            impl<
                '__expr,
                '__expr2,
            > $crate::__private::diesel::expression::AsExpression<$crate::__private::diesel::pg::sql_types::Uuid>
            for &'__expr2 &'__expr $name {
                type Expression = $crate::__private::diesel::internal::derives::as_expression::Bound<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                    Self,
                >;
                fn as_expression(
                    self,
                ) -> <Self as $crate::__private::diesel::expression::AsExpression<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                >>::Expression {
                    $crate::__private::diesel::internal::derives::as_expression::Bound::new(self)
                }
            }
            impl<
                '__expr,
                '__expr2,
            > $crate::__private::diesel::expression::AsExpression<
                $crate::__private::diesel::sql_types::Nullable<$crate::__private::diesel::pg::sql_types::Uuid>,
            > for &'__expr2 &'__expr $name {
                type Expression = $crate::__private::diesel::internal::derives::as_expression::Bound<
                    $crate::__private::diesel::sql_types::Nullable<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                    >,
                    Self,
                >;
                fn as_expression(
                    self,
                ) -> <Self as $crate::__private::diesel::expression::AsExpression<
                    $crate::__private::diesel::sql_types::Nullable<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                    >,
                >>::Expression {
                    $crate::__private::diesel::internal::derives::as_expression::Bound::new(self)
                }
            }
            impl<
                __DB,
            > $crate::__private::diesel::serialize::ToSql<
                $crate::__private::diesel::sql_types::Nullable<$crate::__private::diesel::pg::sql_types::Uuid>,
                __DB,
            > for $name
            where
                __DB: $crate::__private::diesel::backend::Backend,
                Self: $crate::__private::diesel::serialize::ToSql<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                    __DB,
                >,
            {
                fn to_sql<'__b>(
                    &'__b self,
                    out: &mut $crate::__private::diesel::serialize::Output<'__b, '_, __DB>,
                ) -> $crate::__private::diesel::serialize::Result {
                    $crate::__private::diesel::serialize::ToSql::<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                        __DB,
                    >::to_sql(self, out)
                }
            }
            impl $crate::__private::diesel::expression::AsExpression<
                $crate::__private::diesel::pg::sql_types::Uuid,
            > for $name {
                type Expression = $crate::__private::diesel::internal::derives::as_expression::Bound<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                    Self,
                >;
                fn as_expression(
                    self,
                ) -> <Self as $crate::__private::diesel::expression::AsExpression<
                    $crate::__private::diesel::pg::sql_types::Uuid,
                >>::Expression {
                    $crate::__private::diesel::internal::derives::as_expression::Bound::new(self)
                }
            }
            impl $crate::__private::diesel::expression::AsExpression<
                $crate::__private::diesel::sql_types::Nullable<$crate::__private::diesel::pg::sql_types::Uuid>,
            > for $name {
                type Expression = $crate::__private::diesel::internal::derives::as_expression::Bound<
                    $crate::__private::diesel::sql_types::Nullable<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                    >,
                    Self,
                >;
                fn as_expression(
                    self,
                ) -> <Self as $crate::__private::diesel::expression::AsExpression<
                    $crate::__private::diesel::sql_types::Nullable<
                        $crate::__private::diesel::pg::sql_types::Uuid,
                    >,
                >>::Expression {
                    $crate::__private::diesel::internal::derives::as_expression::Bound::new(self)
                }
            }

            impl<__DB, __ST> $crate::__private::diesel::deserialize::Queryable<__ST, __DB> for $name
            where
                __DB: $crate::__private::diesel::backend::Backend,
                __ST: $crate::__private::diesel::sql_types::SingleValue,
                Self: $crate::__private::diesel::deserialize::FromSql<__ST, __DB>,
            {
                type Row = Self;
                fn build(row: Self) -> $crate::__private::diesel::deserialize::Result<Self> {
                    Ok(row)
                }
            }

            impl $crate::__private::std::cmp::PartialEq<$crate::__private::uuid::Uuid> for $name {
                fn eq(&self, other: &$crate::__private::uuid::Uuid) -> bool {
                    self.0 == *other
                }
            }

            impl $crate::__private::std::cmp::PartialEq<$name> for $crate::__private::uuid::Uuid {
                fn eq(&self, other: &$name) -> bool {
                    *self == other.0
                }
            }

            impl $crate::__private::std::cmp::PartialEq<$crate::__private::uuid::NonNilUuid> for $name {
                fn eq(&self, other: &$crate::__private::uuid::NonNilUuid) -> bool {
                    self.0 == *other
                }
            }

            impl $crate::__private::std::cmp::PartialEq<$name> for $crate::__private::uuid::NonNilUuid {
                fn eq(&self, other: &$name) -> bool {
                    *self == other.0
                }
            }

            impl $crate::__private::std::cmp::PartialOrd<$crate::__private::uuid::Uuid> for $name {
                fn partial_cmp(&self, other: &$crate::__private::uuid::Uuid) -> $crate::__private::std::option::Option<$crate::__private::std::cmp::Ordering> {
                    $crate::__private::std::cmp::PartialOrd::partial_cmp(&self.0, other)
                }
            }

            impl $crate::__private::std::cmp::PartialOrd<$name> for $crate::__private::uuid::Uuid {
                fn partial_cmp(&self, other: &$name) -> $crate::__private::std::option::Option<$crate::__private::std::cmp::Ordering> {
                    $crate::__private::std::cmp::PartialOrd::partial_cmp(self, &other.0)
                }
            }

            impl $crate::__private::std::convert::AsRef<[u8]> for $name {
                fn as_ref(&self) -> &[u8] {
                    $crate::__private::std::convert::AsRef::as_ref(&self.0)
                }
            }

            impl $crate::__private::std::convert::AsRef<$crate::__private::uuid::Uuid> for $name {
                fn as_ref(&self) -> &$crate::__private::uuid::Uuid {
                    &self.0
                }
            }

            impl $crate::__private::std::borrow::Borrow<$crate::__private::uuid::Uuid> for $name {
                fn borrow(&self) -> &$crate::__private::uuid::Uuid {
                    &self.0
                }
            }

            impl $crate::__private::std::convert::From<$name> for $crate::__private::uuid::Uuid {
                fn from(value: $name) -> Self {
                    value.0
                }
            }

            $crate::__typed_uuid__forward_from!(
                $name:
                $crate::__private::uuid::fmt::Braced,
                $crate::__private::uuid::fmt::Hyphenated,
                $crate::__private::uuid::fmt::Simple,
                $crate::__private::uuid::fmt::Urn,
                $crate::__private::std::string::String,
                $crate::__private::std::vec::Vec<u8>,
            );

            impl $crate::__private::std::fmt::Display for $name {
                fn fmt(&self, f: &mut $crate::__private::std::fmt::Formatter<'_>) -> $crate::__private::std::fmt::Result {
                    $crate::__private::std::fmt::Display::fmt(&self.0, f)
                }
            }

            impl $crate::__private::std::fmt::LowerHex for $name {
                fn fmt(&self, f: &mut $crate::__private::std::fmt::Formatter<'_>) -> $crate::__private::std::fmt::Result {
                    $crate::__private::std::fmt::LowerHex::fmt(&self.0, f)
                }
            }

            impl $crate::__private::std::fmt::UpperHex for $name {
                fn fmt(&self, f: &mut $crate::__private::std::fmt::Formatter<'_>) -> $crate::__private::std::fmt::Result {
                    $crate::__private::std::fmt::UpperHex::fmt(&self.0, f)
                }
            }

            $crate::__typed_uuid__impl_serde!($name);
        )+
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! __typed_uuid__forward_from {
    ($name:ident: $($ty:path),+ $(,)?) => {
        $(
            impl $crate::__private::std::convert::From<$name> for $ty {
                fn from(value: $name) -> Self {
                    Self::from(value.0)
                }
            }
        )+
    };
}

#[macro_export]
#[doc(hidden)]
#[cfg(feature = "dangerous-construction")]
macro_rules! __typed_uuid__impl_dangerous_construction {
    ($vis:vis) => {
        /// Creates a new typed `Uuid` which does not come from the database.
        #[must_use]
        #[allow(unused)]
        $vis fn dangerous_new(inner: $crate::__private::uuid::Uuid) -> Self {
            Self(inner)
        }
    };
}

#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "dangerous-construction"))]
macro_rules! __typed_uuid__impl_dangerous_construction {
    ($vis:vis) => {};
}

#[macro_export]
#[doc(hidden)]
#[cfg(feature = "serde")]
macro_rules! __typed_uuid__impl_serde {
    ($name:ident) => {
        impl $crate::__private::serde_core::Serialize for $name {
            fn serialize<S>(
                &self,
                serializer: S,
            ) -> $crate::__private::std::result::Result<S::Ok, S::Error>
            where
                S: $crate::__private::serde_core::Serializer,
            {
                $crate::__private::serde_core::Serialize::serialize(&self.0, serializer)
            }
        }
    };
}

#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "serde"))]
macro_rules! __typed_uuid__impl_serde {
    ($name:ident) => {};
}

#[cfg(test)]
mod test {
    use uuid::Uuid;

    #[test]
    fn creates_new_typed_uuid() {
        crate::typed_uuid!(pub FooId);
        let inner = Uuid::new_v4();
        let new = FooId::dangerous_new(inner);
        assert_eq!(new.get(), inner);
    }

    #[test]
    fn partial_ord() {
        crate::typed_uuid!(Foo);
        let one = Uuid::from_u128(1);
        let two = Uuid::from_u128(2);

        assert!(Foo::dangerous_new(one) < two);
        assert!(one < Foo::dangerous_new(two));
        assert!(Foo::dangerous_new(one) <= one);
        assert!(one <= Foo::dangerous_new(one));
    }
}