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    };
41}
42
43fmt_uuid_to_box_value!(uuid::fmt::Braced, braced);
44fmt_uuid_to_box_value!(uuid::fmt::Hyphenated, hyphenated);
45fmt_uuid_to_box_value!(uuid::fmt::Simple, simple);
46fmt_uuid_to_box_value!(uuid::fmt::Urn, urn);
47
48impl Value {
49    pub fn is_uuid(&self) -> bool {
50        matches!(self, Self::Uuid(_))
51    }
52    pub fn as_ref_uuid(&self) -> Option<&Uuid> {
53        match self {
54            Self::Uuid(v) => v.as_ref(),
55            _ => panic!("not Value::Uuid"),
56        }
57    }
58}