1pub struct TypeId;
14
15impl TypeId {
16 pub const NESTED_TYPE: u8 = 1;
20 pub const INT8: u8 = 2;
22 pub const UINT8: u8 = 3;
24 pub const INT16: u8 = 4;
26 pub const UINT16: u8 = 5;
28 pub const INT32: u8 = 6;
30 pub const UINT32: u8 = 7;
32 pub const INT64: u8 = 8;
34 pub const UINT64: u8 = 9;
36 pub const FLOAT32: u8 = 10;
38 pub const FLOAT64: u8 = 11;
40 pub const BOOL: u8 = 15;
42 pub const BYTE: u8 = 16;
44 pub const STRING: u8 = 17;
46 pub const WSTRING: u8 = 18;
48 pub const FIXED_STRING: u8 = 19;
50 pub const FIXED_WSTRING: u8 = 20;
52 pub const BOUNDED_STRING: u8 = 21;
54 pub const BOUNDED_WSTRING: u8 = 22;
56
57 pub const NESTED_TYPE_ARRAY: u8 = 49;
61 pub const INT8_ARRAY: u8 = 50;
63 pub const UINT8_ARRAY: u8 = 51;
65 pub const INT16_ARRAY: u8 = 52;
67 pub const UINT16_ARRAY: u8 = 53;
69 pub const INT32_ARRAY: u8 = 54;
71 pub const UINT32_ARRAY: u8 = 55;
73 pub const INT64_ARRAY: u8 = 56;
75 pub const UINT64_ARRAY: u8 = 57;
77 pub const FLOAT32_ARRAY: u8 = 58;
79 pub const FLOAT64_ARRAY: u8 = 59;
81 pub const BOOL_ARRAY: u8 = 63;
83 pub const STRING_ARRAY: u8 = 65;
85
86 pub const NESTED_TYPE_BOUNDED_SEQUENCE: u8 = 97;
90 pub const INT8_BOUNDED_SEQUENCE: u8 = 98;
92 pub const UINT8_BOUNDED_SEQUENCE: u8 = 99;
94 pub const INT16_BOUNDED_SEQUENCE: u8 = 100;
96 pub const UINT16_BOUNDED_SEQUENCE: u8 = 101;
98 pub const INT32_BOUNDED_SEQUENCE: u8 = 102;
100 pub const UINT32_BOUNDED_SEQUENCE: u8 = 103;
102 pub const INT64_BOUNDED_SEQUENCE: u8 = 104;
104 pub const UINT64_BOUNDED_SEQUENCE: u8 = 105;
106 pub const FLOAT32_BOUNDED_SEQUENCE: u8 = 106;
108 pub const FLOAT64_BOUNDED_SEQUENCE: u8 = 107;
110 pub const BOOL_BOUNDED_SEQUENCE: u8 = 111;
112 pub const STRING_BOUNDED_SEQUENCE: u8 = 113;
114
115 pub const NESTED_TYPE_UNBOUNDED_SEQUENCE: u8 = 145;
119 pub const INT8_UNBOUNDED_SEQUENCE: u8 = 146;
121 pub const UINT8_UNBOUNDED_SEQUENCE: u8 = 147;
123 pub const INT16_UNBOUNDED_SEQUENCE: u8 = 148;
125 pub const UINT16_UNBOUNDED_SEQUENCE: u8 = 149;
127 pub const INT32_UNBOUNDED_SEQUENCE: u8 = 150;
129 pub const UINT32_UNBOUNDED_SEQUENCE: u8 = 151;
131 pub const INT64_UNBOUNDED_SEQUENCE: u8 = 152;
133 pub const UINT64_UNBOUNDED_SEQUENCE: u8 = 153;
135 pub const FLOAT32_UNBOUNDED_SEQUENCE: u8 = 154;
137 pub const FLOAT64_UNBOUNDED_SEQUENCE: u8 = 155;
139 pub const BOOL_UNBOUNDED_SEQUENCE: u8 = 159;
141 pub const STRING_UNBOUNDED_SEQUENCE: u8 = 161;
143
144 pub const ARRAY_OFFSET: u8 = 48;
148 pub const BOUNDED_SEQUENCE_OFFSET: u8 = 96;
150 pub const UNBOUNDED_SEQUENCE_OFFSET: u8 = 144;
152
153 pub const fn is_nested(type_id: u8) -> bool {
155 type_id == Self::NESTED_TYPE
156 || type_id == Self::NESTED_TYPE_ARRAY
157 || type_id == Self::NESTED_TYPE_BOUNDED_SEQUENCE
158 || type_id == Self::NESTED_TYPE_UNBOUNDED_SEQUENCE
159 }
160
161 pub const fn is_array(type_id: u8) -> bool {
163 type_id >= 49 && type_id <= 65
164 }
165
166 pub const fn is_bounded_sequence(type_id: u8) -> bool {
168 type_id >= 97 && type_id <= 113
169 }
170
171 pub const fn is_unbounded_sequence(type_id: u8) -> bool {
173 type_id >= 145 && type_id <= 161
174 }
175
176 pub const fn is_single(type_id: u8) -> bool {
178 type_id >= 1 && type_id <= 17
179 }
180
181 pub const fn base_type(type_id: u8) -> u8 {
183 if Self::is_unbounded_sequence(type_id) {
184 type_id - Self::UNBOUNDED_SEQUENCE_OFFSET
185 } else if Self::is_bounded_sequence(type_id) {
186 type_id - Self::BOUNDED_SEQUENCE_OFFSET
187 } else if Self::is_array(type_id) {
188 type_id - Self::ARRAY_OFFSET
189 } else {
190 type_id
191 }
192 }
193
194 pub const fn type_name(type_id: u8) -> Option<&'static str> {
196 match Self::base_type(type_id) {
197 Self::NESTED_TYPE => Some("nested"),
198 Self::INT8 => Some("int8"),
199 Self::UINT8 => Some("uint8"),
200 Self::INT16 => Some("int16"),
201 Self::UINT16 => Some("uint16"),
202 Self::INT32 => Some("int32"),
203 Self::UINT32 => Some("uint32"),
204 Self::INT64 => Some("int64"),
205 Self::UINT64 => Some("uint64"),
206 Self::FLOAT32 => Some("float32"),
207 Self::FLOAT64 => Some("float64"),
208 Self::BOOL => Some("bool"),
209 Self::STRING => Some("string"),
210 _ => None,
211 }
212 }
213}
214
215#[cfg(test)]
216mod tests {
217 use super::*;
218
219 #[test]
220 fn test_type_id_offsets() {
221 assert_eq!(TypeId::INT32_ARRAY, TypeId::INT32 + TypeId::ARRAY_OFFSET);
222 assert_eq!(
223 TypeId::INT32_BOUNDED_SEQUENCE,
224 TypeId::INT32 + TypeId::BOUNDED_SEQUENCE_OFFSET
225 );
226 assert_eq!(
227 TypeId::INT32_UNBOUNDED_SEQUENCE,
228 TypeId::INT32 + TypeId::UNBOUNDED_SEQUENCE_OFFSET
229 );
230 }
231
232 #[test]
233 fn test_is_nested() {
234 assert!(TypeId::is_nested(TypeId::NESTED_TYPE));
235 assert!(TypeId::is_nested(TypeId::NESTED_TYPE_ARRAY));
236 assert!(TypeId::is_nested(TypeId::NESTED_TYPE_BOUNDED_SEQUENCE));
237 assert!(TypeId::is_nested(TypeId::NESTED_TYPE_UNBOUNDED_SEQUENCE));
238 assert!(!TypeId::is_nested(TypeId::INT32));
239 }
240
241 #[test]
242 fn test_is_array() {
243 assert!(TypeId::is_array(TypeId::INT32_ARRAY));
244 assert!(TypeId::is_array(TypeId::STRING_ARRAY));
245 assert!(!TypeId::is_array(TypeId::INT32));
246 assert!(!TypeId::is_array(TypeId::INT32_UNBOUNDED_SEQUENCE));
247 }
248
249 #[test]
250 fn test_base_type() {
251 assert_eq!(TypeId::base_type(TypeId::INT32), TypeId::INT32);
252 assert_eq!(TypeId::base_type(TypeId::INT32_ARRAY), TypeId::INT32);
253 assert_eq!(
254 TypeId::base_type(TypeId::INT32_BOUNDED_SEQUENCE),
255 TypeId::INT32
256 );
257 assert_eq!(
258 TypeId::base_type(TypeId::INT32_UNBOUNDED_SEQUENCE),
259 TypeId::INT32
260 );
261 }
262
263 #[test]
264 fn test_type_name() {
265 assert_eq!(TypeId::type_name(TypeId::INT32), Some("int32"));
266 assert_eq!(TypeId::type_name(TypeId::INT32_ARRAY), Some("int32"));
267 assert_eq!(TypeId::type_name(TypeId::STRING), Some("string"));
268 assert_eq!(TypeId::type_name(TypeId::NESTED_TYPE), Some("nested"));
269 }
270}