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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
mod internal;
use darling::{FromDeriveInput, FromMeta};
use syn::{DeriveInput, Generics, Ident, Type, Visibility};
#[derive(Debug)]
pub struct Ent {
pub ident: Ident,
pub vis: Visibility,
pub generics: Generics,
pub id: Ident,
pub id_ty: Type,
pub database: Ident,
pub database_ty: Type,
pub created: Ident,
pub created_ty: Type,
pub last_updated: Ident,
pub last_updated_ty: Type,
pub fields: Vec<EntField>,
pub edges: Vec<EntEdge>,
}
#[derive(Debug)]
pub struct EntField {
pub name: Ident,
pub ty: Type,
pub indexed: bool,
pub mutable: bool,
}
#[derive(Debug)]
pub struct EntEdge {
pub name: Ident,
pub ty: Type,
pub ent_ty: Type,
pub ent_query_ty: Option<Type>,
pub wrap: bool,
pub use_id_slice: bool,
pub kind: EntEdgeKind,
pub deletion_policy: EntEdgeDeletionPolicy,
}
#[derive(Debug, Clone, FromMeta)]
pub enum EntEdgeDeletionPolicy {
Nothing,
Shallow,
Deep,
}
impl Default for EntEdgeDeletionPolicy {
fn default() -> Self {
Self::Nothing
}
}
#[derive(Debug)]
pub enum EntEdgeKind {
Maybe,
One,
Many,
}
impl FromDeriveInput for Ent {
fn from_derive_input(input: &DeriveInput) -> darling::Result<Self> {
let ent = internal::Ent::from_derive_input(input)?;
let data_struct = ent
.data
.take_struct()
.expect("Ent only supports named structs");
let mut id = None;
let mut id_ty = None;
let mut database = None;
let mut database_ty = None;
let mut created = None;
let mut created_ty = None;
let mut last_updated = None;
let mut last_updated_ty = None;
let mut fields = Vec::new();
let mut edges = Vec::new();
let mut errors = vec![];
for f in &data_struct.fields {
let mut acted_on_field = false;
let name = f.ident.as_ref().expect("Ent only supports named structs");
let ty = &f.ty;
if let Err(e) = f.validate_zero_or_one_known_fields() {
errors.push(e);
}
if f.is_id_field() {
acted_on_field = true;
if id.is_some() {
errors.push(
darling::Error::custom("Already have an id elsewhere").with_span(&name),
);
} else {
id = Some(name);
id_ty = Some(ty);
}
}
if f.is_database_field() {
acted_on_field = true;
if database.is_some() {
errors.push(
darling::Error::custom("Already have a database elsewhere")
.with_span(&name),
);
} else {
database = Some(name);
database_ty = Some(ty);
}
}
if f.is_created_field() {
acted_on_field = true;
if created.is_some() {
errors.push(
darling::Error::custom("Already have a created timestamp elsewhere")
.with_span(&name),
);
} else {
created = Some(name);
created_ty = Some(ty);
}
}
if f.is_last_updated_field() {
acted_on_field = true;
if last_updated.is_some() {
errors.push(
darling::Error::custom("Already have a last_updated timestamp elsewhere")
.with_span(&name),
);
} else {
last_updated = Some(name);
last_updated_ty = Some(ty);
}
}
if let Some(attr) = f.field_attr.clone().map(|a| a.unwrap_or_default()) {
acted_on_field = true;
fields.push(EntField {
name: name.clone(),
ty: f.ty.clone(),
indexed: attr.indexed.is_some(),
mutable: attr.mutable.is_some(),
});
}
if let Some(attr) = f.edge_attr.clone() {
acted_on_field = true;
let kind = match infer_edge_kind_from_ty(&f.ty) {
Ok(k) => k,
Err(e) => {
errors.push(e);
continue;
}
};
edges.push(EntEdge {
name: name.clone(),
ty: f.ty.clone(),
ent_ty: syn::parse_str(&attr.r#type)?,
ent_query_ty: attr
.query_ty
.and_then(|type_str| syn::parse_str(&type_str).ok()),
wrap: attr.wrap.is_some(),
use_id_slice: attr.use_id_slice.is_some(),
kind,
deletion_policy: attr.deletion_policy,
});
}
if acted_on_field {
continue;
} else {
fields.push(EntField {
name: name.clone(),
ty: f.ty.clone(),
indexed: false,
mutable: false,
});
}
}
if id.is_none() {
errors.push(darling::Error::custom("No id field provided").with_span(input));
}
if database.is_none() {
errors.push(darling::Error::custom("No database field provided").with_span(input));
}
if created.is_none() {
errors.push(darling::Error::custom("No created field provided").with_span(input));
}
if last_updated.is_none() {
errors.push(darling::Error::custom("No last_updated field provided").with_span(input));
}
if !errors.is_empty() {
return Err(darling::Error::multiple(errors));
}
Ok(Ent {
ident: ent.ident,
vis: ent.vis,
generics: ent.generics,
id: id.cloned().unwrap(),
id_ty: id_ty.cloned().unwrap(),
database: database.cloned().unwrap(),
database_ty: database_ty.cloned().unwrap(),
created: created.cloned().unwrap(),
created_ty: created_ty.cloned().unwrap(),
last_updated: last_updated.cloned().unwrap(),
last_updated_ty: last_updated_ty.cloned().unwrap(),
fields,
edges,
})
}
}
fn infer_edge_kind_from_ty(ty: &Type) -> darling::Result<EntEdgeKind> {
match &ty {
Type::Path(x) => {
let segment = match x.path.segments.last() {
Some(seg) => seg,
None => {
return Err(darling::Error::custom("Missing edge id type").with_span(x));
}
};
Ok(match segment.ident.to_string().to_lowercase().as_str() {
"option" => EntEdgeKind::Maybe,
"vec" | "vecdeque" | "linkedlist" | "binaryheap" | "hashset" | "btreeset" => {
EntEdgeKind::Many
}
_ => EntEdgeKind::One,
})
}
x => Err(darling::Error::custom("Unexpected edge id type").with_span(x)),
}
}