Skip to main content

sea_orm_spanner/
uuid_support.rs

1use {
2    gcloud_googleapis::spanner::v1::TypeCode,
3    gcloud_spanner::statement::{single_type, ToKind},
4    prost_types::value::Kind,
5};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct SpannerUuid(pub uuid::Uuid);
9
10impl SpannerUuid {
11    pub fn new(uuid: uuid::Uuid) -> Self {
12        Self(uuid)
13    }
14
15    pub fn inner(&self) -> &uuid::Uuid {
16        &self.0
17    }
18}
19
20impl From<uuid::Uuid> for SpannerUuid {
21    fn from(uuid: uuid::Uuid) -> Self {
22        Self(uuid)
23    }
24}
25
26impl From<SpannerUuid> for uuid::Uuid {
27    fn from(wrapper: SpannerUuid) -> Self {
28        wrapper.0
29    }
30}
31
32impl ToKind for SpannerUuid {
33    fn to_kind(&self) -> Kind {
34        Kind::StringValue(self.0.hyphenated().to_string())
35    }
36
37    fn get_type() -> gcloud_googleapis::spanner::v1::Type {
38        single_type(TypeCode::Uuid)
39    }
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub struct SpannerOptionalUuid(pub Option<uuid::Uuid>);
44
45impl SpannerOptionalUuid {
46    pub fn some(uuid: uuid::Uuid) -> Self {
47        Self(Some(uuid))
48    }
49
50    pub fn none() -> Self {
51        Self(None)
52    }
53}
54
55impl From<Option<uuid::Uuid>> for SpannerOptionalUuid {
56    fn from(uuid: Option<uuid::Uuid>) -> Self {
57        Self(uuid)
58    }
59}
60
61impl ToKind for SpannerOptionalUuid {
62    fn to_kind(&self) -> Kind {
63        match &self.0 {
64            Some(v) => Kind::StringValue(v.hyphenated().to_string()),
65            None => Kind::NullValue(prost_types::NullValue::NullValue.into()),
66        }
67    }
68
69    fn get_type() -> gcloud_googleapis::spanner::v1::Type {
70        single_type(TypeCode::Uuid)
71    }
72}