1use std::borrow::Cow;
2use std::marker::PhantomData;
3
4pub mod builders;
5use self::builders::TypeBuilder;
6
7pub trait TypeId: Clone + 'static {
8 const UNIT: Self;
9 const BOOL: Self;
10 const I8: Self;
11 const I16: Self;
12 const I32: Self;
13 const I64: Self;
14 const U8: Self;
15 const U16: Self;
16 const U32: Self;
17 const U64: Self;
18 const F32: Self;
19 const F64: Self;
20 const CHAR: Self;
21 const STR: Self;
22 const BYTES: Self;
23}
24
25#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
26pub struct StructField<T: TypeId> {
27 pub(crate) name: Cow<'static, str>,
28 pub(crate) id: T,
29}
30
31impl<T: TypeId> StructField<T> {
32 pub fn name(&self) -> &str {
33 &self.name
34 }
35
36 pub fn field_type(&self) -> &T {
37 &self.id
38 }
39}
40
41#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
42pub struct UnitVariant<T: TypeId> {
43 pub(crate) name: Cow<'static, str>,
44 pub(crate) _phan: PhantomData<T>,
45}
46
47impl<T: TypeId> UnitVariant<T> {
48 pub fn name(&self) -> &str {
49 &self.name
50 }
51}
52
53#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
54pub struct NewtypeVariant<T: TypeId> {
55 pub(crate) name: Cow<'static, str>,
56 pub(crate) value: T,
57}
58
59impl<T: TypeId> NewtypeVariant<T> {
60 pub fn name(&self) -> &str {
61 &self.name
62 }
63
64 pub fn inner_type(&self) -> &T {
65 &self.value
66 }
67}
68
69#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
70pub struct TupleVariant<T: TypeId> {
71 pub(crate) name: Cow<'static, str>,
72 pub(crate) elements: Cow<'static, [T]>,
73}
74
75impl<T: TypeId> TupleVariant<T> {
76 pub fn name(&self) -> &str {
77 &self.name
78 }
79
80 pub fn element_types(&self) -> &[T] {
81 &self.elements
82 }
83}
84
85#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
86pub struct StructVariant<T: TypeId> {
87 pub(crate) name: Cow<'static, str>,
88 pub(crate) fields: Cow<'static, [StructField<T>]>,
89}
90
91impl<T: TypeId> StructVariant<T> {
92 pub fn name(&self) -> &str {
93 &self.name
94 }
95
96 pub fn fields(&self) -> &[StructField<T>] {
97 &self.fields
98 }
99}
100
101#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
102pub enum EnumVariant<T: TypeId> {
103 Unit(UnitVariant<T>),
104 Newtype(NewtypeVariant<T>),
105 Tuple(TupleVariant<T>),
106 Struct(StructVariant<T>),
107}
108
109impl<T: TypeId> EnumVariant<T> {
110 pub fn name(&self) -> &str {
111 match self {
112 &EnumVariant::Unit(ref v) => v.name(),
113 &EnumVariant::Newtype(ref v) => v.name(),
114 &EnumVariant::Tuple(ref v) => v.name(),
115 &EnumVariant::Struct(ref v) => v.name(),
116 }
117 }
118
119 pub fn as_unit_variant(&self) -> Option<&UnitVariant<T>> {
120 if let &EnumVariant::Unit(ref unit_variant) = self {
121 Some(&unit_variant)
122 } else {
123 None
124 }
125 }
126
127 pub fn as_newtype_variant(&self) -> Option<&NewtypeVariant<T>> {
128 if let &EnumVariant::Newtype(ref newtype_variant) = self {
129 Some(&newtype_variant)
130 } else {
131 None
132 }
133 }
134
135 pub fn as_tuple_variant(&self) -> Option<&TupleVariant<T>> {
136 if let &EnumVariant::Tuple(ref tuple_variant) = self {
137 Some(&tuple_variant)
138 } else {
139 None
140 }
141 }
142
143 pub fn as_struct_variant(&self) -> Option<&StructVariant<T>> {
144 if let &EnumVariant::Struct(ref struct_variant) = self {
145 Some(&struct_variant)
146 } else {
147 None
148 }
149 }
150}
151
152#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
153pub struct OptionType<T: TypeId> {
154 pub(crate) value: T,
155}
156
157impl<T: TypeId> OptionType<T> {
158 pub fn inner_type(&self) -> &T {
159 &self.value
160 }
161}
162
163#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
164pub struct UnitStructType<T: TypeId> {
165 pub(crate) _phan: PhantomData<T>,
166 pub(crate) name: Cow<'static, str>,
167}
168
169impl<T: TypeId> UnitStructType<T> {
170 pub fn name(&self) -> &str {
171 &self.name
172 }
173}
174
175#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
176pub struct NewtypeStructType<T: TypeId> {
177 pub(crate) name: Cow<'static, str>,
178 pub(crate) value: T,
179}
180
181impl<T: TypeId> NewtypeStructType<T> {
182 pub fn name(&self) -> &str {
183 &self.name
184 }
185
186 pub fn inner_type(&self) -> &T {
187 &self.value
188 }
189}
190
191#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
192pub struct SeqType<T: TypeId> {
193 pub(crate) len: Option<usize>,
194 pub(crate) element: T,
195}
196
197impl<T: TypeId> SeqType<T> {
198 pub fn len(&self) -> Option<usize> {
199 self.len
200 }
201
202 pub fn element_type(&self) -> &T {
203 &self.element
204 }
205}
206
207#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
208pub struct TupleType<T: TypeId> {
209 pub(crate) elements: Cow<'static, [T]>,
210}
211
212impl<T: TypeId> TupleType<T> {
213 pub fn element_types(&self) -> &[T] {
214 &self.elements
215 }
216}
217
218#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
219pub struct TupleStructType<T: TypeId> {
220 pub(crate) name: Cow<'static, str>,
221 pub(crate) elements: Cow<'static, [T]>,
222}
223
224impl<T: TypeId> TupleStructType<T> {
225 pub fn name(&self) -> &str {
226 &self.name
227 }
228
229 pub fn element_types(&self) -> &[T] {
230 &self.elements
231 }
232}
233
234#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
235pub struct MapType<T: TypeId> {
236 pub(crate) key: T,
237 pub(crate) value: T,
238}
239
240impl<T: TypeId> MapType<T> {
241 pub fn key_type(&self) -> &T {
242 &self.key
243 }
244
245 pub fn value_type(&self) -> &T {
246 &self.value
247 }
248}
249
250#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
251pub struct StructType<T: TypeId> {
252 pub(crate) name: Cow<'static, str>,
253 pub(crate) fields: Cow<'static, [StructField<T>]>,
254}
255
256impl<T: TypeId> StructType<T> {
257 pub fn name(&self) -> &str {
258 &self.name
259 }
260
261 pub fn fields(&self) -> &[StructField<T>] {
262 &self.fields
263 }
264}
265
266#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
267pub struct EnumType<T: TypeId> {
268 pub(crate) name: Cow<'static, str>,
269 pub(crate) variants: Cow<'static, [EnumVariant<T>]>,
270}
271
272impl<T: TypeId> EnumType<T> {
273 pub fn name(&self) -> &str {
274 &self.name
275 }
276
277 pub fn variants(&self) -> &[EnumVariant<T>] {
278 &self.variants
279 }
280
281 pub fn variant(&self, idx: u32) -> Option<&EnumVariant<T>> {
282 self.variants.get(idx as usize)
283 }
284}
285
286#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
287pub enum Type<T: TypeId> {
288 Option(OptionType<T>),
289 UnitStruct(UnitStructType<T>),
290 NewtypeStruct(NewtypeStructType<T>),
291 Seq(SeqType<T>),
292 Tuple(TupleType<T>),
293 TupleStruct(TupleStructType<T>),
294 Map(MapType<T>),
295 Struct(StructType<T>),
296 Enum(EnumType<T>),
297}
298
299impl<T: TypeId> Type<T> {
300 #[inline]
301 pub fn build() -> TypeBuilder<T> {
302 TypeBuilder::new()
303 }
304}