aldrin_core/introspection/
lexical_id.rs

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