1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use crate;
use ;
use JsonSchema;
use ;
use Tsify;
/// Never type for operators.
/// # Type
/// The Type trait is at the heart of a node type system.
/// For a reference implementation have a look at the [`demo_type`](crate::demo_type) module.
///
/// ## Nodety's type model
/// [Type]s are atomic. That means that they are always leaf nodes in a type expression.
/// But you will probably want to make your types generic to express things like `Map<K, V>`
/// where Array is one of your types. To represent this, type expressions have [Constructor][TypeExpr::Constructor]s.
/// A constructor contains a Type and a map of parameters which are themselves type expressions.
/// You can use them to represent two scenarios:
/// ### Generic parameters
/// ```
/// # use maplit::btreemap;
/// # use nodety::{Type, TypeExpr, NoOperator};
/// // Let's say your type structure looks something like this:
/// #[derive(Clone, Debug, PartialEq)]
/// pub enum MyType {
/// Map,
/// Integer,
/// String
/// }
///
/// impl Type for MyType {
/// type Operator = NoOperator;
/// }
///
/// // Then you can represent the type `Map<Integer, String>` as follows:
/// let t_map = TypeExpr::<MyType>::Constructor {
/// inner: MyType::Map,
/// parameters: btreemap! {
/// "K".into() => TypeExpr::Type(MyType::Integer),
/// "V".into() => TypeExpr::Type(MyType::String),
/// },
/// };
/// ```
/// ### Records / Objects
/// You can represent records / objects in the same way.
/// Let's say you want to represent the type `{ property_a: Integer, property_b: String, property_c: String }`.
/// ```
/// # use maplit::btreemap;
/// # use nodety::{Type, TypeExpr, NoOperator};
/// #[derive(Clone, Debug, PartialEq)]
/// pub enum MyType {
/// Record,
/// Integer,
/// String
/// }
///
/// impl Type for MyType {
/// type Operator = NoOperator;
/// }
///
/// let t_map = TypeExpr::<MyType>::Constructor {
/// inner: MyType::Record,
/// parameters: btreemap! {
/// "property_a".into() => TypeExpr::Type(MyType::Integer),
/// "property_b".into() => TypeExpr::Type(MyType::String),
/// "property_c".into() => TypeExpr::Type(MyType::String),
/// },
/// };
/// ```
///
/// ## Constructor subtyping
///
/// A Constructor A is considered a supertype of another constructor B if A's inner type is a supertype of B's inner type.
/// And A has the same or more parameters than B. And A's parameters are each supertypes of B's parameters.
///
/// ## Intersections
///
/// The intersection between two constructors A and B exists if A's inner type is equal to B's inner type.
/// Then the intersection will get all the parameters of A and B. And the parameters that are present in both
/// A and B will be the intersection of both.