aldrin_core/introspection/
lexical_id.rs1use super::ir;
2use crate::tags::{self, KeyTag, PrimaryKeyTag, PrimaryTag, Tag};
3use crate::{
4 Deserialize, DeserializeError, DeserializeKey, Deserializer, Serialize, SerializeError,
5 SerializeKey, Serializer, TypeId,
6};
7use std::fmt;
8use std::str::FromStr;
9use uuid::{uuid, Error as UuidError, Uuid};
10
11#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
12#[repr(transparent)]
13pub struct LexicalId(pub Uuid);
14
15impl LexicalId {
16 pub const NIL: Self = Self(Uuid::nil());
17
18 pub const BOOL: Self = Self(uuid!("f00cbfc4-cf2f-457a-aa56-35923c8b2571"));
19 pub const U8: Self = Self(uuid!("0ff2b764-1666-46e1-82ff-aa59264ba7f0"));
20 pub const I8: Self = Self(uuid!("055a9029-e3ff-4b38-922f-6a5eee338138"));
21 pub const U16: Self = Self(uuid!("355797a3-ef55-49d0-a1e3-3048247c9365"));
22 pub const I16: Self = Self(uuid!("3e86f004-6b09-46dd-aacd-f1161ad546f4"));
23 pub const U32: Self = Self(uuid!("dff83171-4355-4fde-a2a4-67bd804b31c2"));
24 pub const I32: Self = Self(uuid!("8afa8119-736a-4bab-ad71-3b6f8061bed0"));
25 pub const U64: Self = Self(uuid!("1a192e74-8220-4bad-bacb-3385e9c26abf"));
26 pub const I64: Self = Self(uuid!("a4669bfb-1c1c-43c4-ad3f-ea2afab22756"));
27 pub const F32: Self = Self(uuid!("046a2593-0627-44bf-8a6c-d24cb7ef54b2"));
28 pub const F64: Self = Self(uuid!("64d58c83-68f9-43d2-9401-04dbc61e34b0"));
29 pub const STRING: Self = Self(uuid!("034cb183-38c7-4d26-984e-c56730eafc3f"));
30 pub const UUID: Self = Self(uuid!("8cdde1d6-e2ca-4e19-860d-cbe901547816"));
31 pub const OBJECT_ID: Self = Self(uuid!("abab21a3-2ebe-47d6-9caa-1e4fd71e2171"));
32 pub const SERVICE_ID: Self = Self(uuid!("b241ed0d-89db-493e-a4f5-73f13aa4a7a0"));
33 pub const VALUE: Self = Self(uuid!("eb70f272-1c31-4933-a933-4030dd012f07"));
34 pub const BYTES: Self = Self(uuid!("300d98f6-3267-48c2-8aa0-dc36e28b0c43"));
35 pub const LIFETIME: Self = Self(uuid!("e406e363-6eef-41c8-83ae-d114fd6cb0b8"));
36 pub const UNIT: Self = Self(uuid!("630e81c6-b8f3-4c0f-97e3-44d214168d6c"));
37
38 pub const NAMESPACE_OPTION: Uuid = uuid!("050c596f-9fcc-4d5b-9caa-3507553bc64a");
39 pub const NAMESPACE_BOX: Uuid = uuid!("b3073eb9-7200-44ff-b79e-5165103a9382");
40 pub const NAMESPACE_VEC: Uuid = uuid!("c638aee9-5728-42f8-8405-49ae00280a85");
41 pub const NAMESPACE_MAP: Uuid = uuid!("69370c52-3211-46d9-8f7d-9ab0fd1536c9");
42 pub const NAMESPACE_SET: Uuid = uuid!("d9fa4f35-368c-4ad0-86f1-35ace576d58b");
43 pub const NAMESPACE_SENDER: Uuid = uuid!("052ac1dd-c0d4-4f4c-9b7a-78448875a21f");
44 pub const NAMESPACE_RECEIVER: Uuid = uuid!("d697238d-56e0-4132-980e-baf1a64c9bfd");
45 pub const NAMESPACE_RESULT: Uuid = uuid!("aef81d6c-35cc-43f7-99f3-a17c0eada1f4");
46 pub const NAMESPACE_ARRAY: Uuid = uuid!("770f9cf7-be15-454e-9fea-bb452fa813ed");
47 pub const NAMESPACE_CUSTOM: Uuid = uuid!("04334fe0-0ea2-44ea-97b2-c17a7a4cbbd3");
48 pub const NAMESPACE_SERVICE: Uuid = uuid!("ddd86559-be89-4b6c-a460-fc347cd6f00b");
49
50 pub fn option(ty: Self) -> Self {
51 Self::new_v5(Self::NAMESPACE_OPTION, ty.0)
52 }
53
54 pub fn box_ty(ty: Self) -> Self {
55 Self::new_v5(Self::NAMESPACE_BOX, ty.0)
56 }
57
58 pub fn vec(ty: Self) -> Self {
59 Self::new_v5(Self::NAMESPACE_VEC, ty.0)
60 }
61
62 pub fn map(key: Self, ty: Self) -> Self {
63 Self::new_v5_2(Self::NAMESPACE_MAP, key.0, ty.0)
64 }
65
66 pub fn set(ty: Self) -> Self {
67 Self::new_v5(Self::NAMESPACE_SET, ty.0)
68 }
69
70 pub fn sender(ty: Self) -> Self {
71 Self::new_v5(Self::NAMESPACE_SENDER, ty.0)
72 }
73
74 pub fn receiver(ty: Self) -> Self {
75 Self::new_v5(Self::NAMESPACE_RECEIVER, ty.0)
76 }
77
78 pub fn result(ok: Self, err: Self) -> Self {
79 Self::new_v5_2(Self::NAMESPACE_RESULT, ok.0, err.0)
80 }
81
82 pub fn array(ty: Self, len: u32) -> Self {
83 let mut name = [0; 20];
84 name[..16].copy_from_slice(ty.0.as_bytes());
85 name[16..].copy_from_slice(&len.to_le_bytes());
86 Self(Uuid::new_v5(&Self::NAMESPACE_ARRAY, &name))
87 }
88
89 pub fn custom(schema: impl AsRef<str>, name: impl AsRef<str>) -> Self {
90 Self::fully_qualified(Self::NAMESPACE_CUSTOM, schema, name, &[])
91 }
92
93 pub fn custom_generic<const N: usize>(
94 schema: impl AsRef<str>,
95 name: impl AsRef<str>,
96 types: &[Self; N],
97 ) -> Self {
98 Self::fully_qualified(Self::NAMESPACE_CUSTOM, schema, name, types)
99 }
100
101 pub fn service(schema: impl AsRef<str>, name: impl AsRef<str>) -> Self {
102 Self::fully_qualified(Self::NAMESPACE_SERVICE, schema, name, &[])
103 }
104
105 pub const fn is_nil(self) -> bool {
106 self.0.is_nil()
107 }
108
109 pub fn resolve(self, introspection: &ir::IntrospectionIr) -> Option<TypeId> {
110 introspection.resolve(self)
111 }
112
113 fn new_v5(ns: Uuid, ty: Uuid) -> Self {
114 Self(Uuid::new_v5(&ns, ty.as_bytes()))
115 }
116
117 fn new_v5_2(ns: Uuid, a: Uuid, b: Uuid) -> Self {
118 let mut name = [0; 32];
119 name[..16].copy_from_slice(a.as_bytes());
120 name[16..].copy_from_slice(b.as_bytes());
121 Self(Uuid::new_v5(&ns, &name))
122 }
123
124 fn fully_qualified<const N: usize>(
125 ns: Uuid,
126 schema: impl AsRef<str>,
127 name: impl AsRef<str>,
128 types: &[Self; N],
129 ) -> Self {
130 let mut fully_qualified = format!("{}::{}", schema.as_ref(), name.as_ref());
131
132 if N > 0 {
133 fully_qualified.push('<');
134 }
135
136 for (i, ty) in types.iter().enumerate() {
137 if i > 0 {
138 fully_qualified.push(',');
139 }
140
141 fully_qualified.push_str(&ty.to_string());
142 }
143
144 if N > 0 {
145 fully_qualified.push('>');
146 }
147
148 Self(Uuid::new_v5(&ns, fully_qualified.as_bytes()))
149 }
150}
151
152impl Tag for LexicalId {}
153
154impl PrimaryTag for LexicalId {
155 type Tag = Self;
156}
157
158impl Serialize<Self> for LexicalId {
159 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
160 serializer.serialize_uuid(self.0)
161 }
162}
163
164impl Serialize<LexicalId> for &LexicalId {
165 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
166 serializer.serialize::<LexicalId>(*self)
167 }
168}
169
170impl Deserialize<Self> for LexicalId {
171 fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
172 deserializer.deserialize_uuid().map(Self)
173 }
174}
175
176impl Serialize<tags::Uuid> for LexicalId {
177 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
178 serializer.serialize::<Self>(self)
179 }
180}
181
182impl Serialize<tags::Uuid> for &LexicalId {
183 fn serialize(self, serializer: Serializer) -> Result<(), SerializeError> {
184 serializer.serialize::<tags::Uuid>(*self)
185 }
186}
187
188impl Deserialize<tags::Uuid> for LexicalId {
189 fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
190 deserializer.deserialize::<Self, _>()
191 }
192}
193
194impl KeyTag for LexicalId {
195 type Impl = tags::Uuid;
196}
197
198impl PrimaryKeyTag for LexicalId {
199 type KeyTag = Self;
200}
201
202impl SerializeKey<Self> for LexicalId {
203 fn try_as_key(&self) -> Result<Uuid, SerializeError> {
204 Ok(self.0)
205 }
206}
207
208impl DeserializeKey<Self> for LexicalId {
209 fn try_from_key(key: Uuid) -> Result<Self, DeserializeError> {
210 Ok(Self(key))
211 }
212}
213
214impl SerializeKey<tags::Uuid> for LexicalId {
215 fn try_as_key(&self) -> Result<Uuid, SerializeError> {
216 Ok(self.0)
217 }
218}
219
220impl DeserializeKey<tags::Uuid> for LexicalId {
221 fn try_from_key(key: Uuid) -> Result<Self, DeserializeError> {
222 Ok(Self(key))
223 }
224}
225
226impl From<Uuid> for LexicalId {
227 fn from(uuid: Uuid) -> Self {
228 Self(uuid)
229 }
230}
231
232impl From<LexicalId> for Uuid {
233 fn from(id: LexicalId) -> Self {
234 id.0
235 }
236}
237
238impl fmt::Display for LexicalId {
239 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
240 self.0.fmt(f)
241 }
242}
243
244impl FromStr for LexicalId {
245 type Err = UuidError;
246
247 fn from_str(s: &str) -> Result<Self, UuidError> {
248 s.parse().map(Self)
249 }
250}