Skip to main content

sql_builder/value/
with_uuid.rs

1use super::*;
2
3type_to_value!(Uuid, Uuid, Uuid);
4type_ref_clone_to_value!(Uuid);
5
6macro_rules! fmt_uuid_to_box_value {
7    ( $type: ty, $conversion_fn: ident ) => {
8        impl From<$type> for Value {
9            fn from(x: $type) -> Value {
10                Value::Uuid(Some(x.into_uuid()))
11            }
12        }
13
14        impl Nullable for $type {
15            fn null() -> Value {
16                Value::Uuid(None)
17            }
18        }
19
20        impl ValueType for $type {
21            fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
22                match v {
23                    Value::Uuid(Some(x)) => Ok(x.$conversion_fn()),
24                    _ => Err(ValueTypeErr),
25                }
26            }
27
28            fn type_name() -> String {
29                stringify!($type).to_owned()
30            }
31
32            fn array_type() -> ArrayType {
33                ArrayType::Uuid
34            }
35
36            fn column_type() -> ColumnType {
37                ColumnType::Uuid
38            }
39
40            const COLUMN_KIND: ColumnKind = ColumnKind::Uuid;
41        }
42    };
43}
44
45fmt_uuid_to_box_value!(uuid::fmt::Braced, braced);
46fmt_uuid_to_box_value!(uuid::fmt::Hyphenated, hyphenated);
47fmt_uuid_to_box_value!(uuid::fmt::Simple, simple);
48fmt_uuid_to_box_value!(uuid::fmt::Urn, urn);
49
50impl Value {
51    pub fn is_uuid(&self) -> bool {
52        matches!(self, Self::Uuid(_))
53    }
54    pub fn as_ref_uuid(&self) -> Option<&Uuid> {
55        match self {
56            Self::Uuid(v) => v.as_ref(),
57            _ => panic!("not Value::Uuid"),
58        }
59    }
60}