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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use spec::*;
use util::*;
use std::collections::{ HashSet };
use itertools::Itertools;
pub struct TypeDeanonymizer {
builder: SpecBuilder,
}
impl TypeDeanonymizer {
pub fn new(spec: &Spec) -> Self {
let mut result = TypeDeanonymizer {
builder: SpecBuilder::new(),
};
for (_, name) in spec.field_names() {
result.builder.import_field_name(name)
}
let mut field_offset = None;
for (name, interface) in spec.interfaces_by_name() {
result.builder.import_node_name(name);
let mut fields = vec![];
if interface.is_skippable() {
let name = field_offset.get_or_insert_with(||
result.builder.field_name("_offset")
);
fields.push(Field::new(
name.clone(),
Type::offset().required()
))
}
for field in interface.contents().fields() {
result.import_type(spec, field.type_(), None);
fields.push(field.clone());
}
let mut declaration = result.builder.add_interface(name)
.unwrap();
for field in fields.drain(..) {
declaration.with_field(field.name(), field.type_().clone());
}
}
for (name, definition) in spec.typedefs_by_name() {
result.builder.import_node_name(name);
if result.builder.get_typedef(name).is_some() {
continue
}
result.import_type(spec, &definition, Some(name.clone()));
}
for (name, definition) in spec.string_enums_by_name() {
result.builder.import_node_name(name);
let mut strings: Vec<_> = definition.strings()
.iter()
.collect();
let mut declaration = result.builder.add_string_enum(name)
.unwrap();
for string in strings.drain(..) {
declaration.with_string(&string);
}
}
debug!(target: "export_utils", "Names: {:?}", result.builder.names().keys().format(", "));
result
}
pub fn into_spec(self, options: SpecOptions) -> Spec {
self.builder.into_spec(options)
}
pub fn get_node_name(&self, name: &str) -> Option<NodeName> {
self.builder.get_node_name(name)
}
fn import_type(&mut self, spec: &Spec, type_: &Type, public_name: Option<NodeName>) -> (Option<HashSet<NodeName>>, NodeName) {
debug!(target: "export_utils", "import_type {:?} => {:?}", public_name, type_);
if type_.is_optional() {
let (_, spec_name) = self.import_typespec(spec, &type_.spec, None);
let my_name =
match public_name {
None => self.builder.node_name(&format!("Optional{}", spec_name)),
Some(ref name) => name.clone()
};
let deanonymized = Type::named(&spec_name).optional()
.unwrap();
if let Some(ref mut typedef) = self.builder.add_typedef(&my_name) {
debug!(target: "export_utils", "import_type introduced {:?}", my_name);
typedef.with_type(deanonymized.clone());
} else {
debug!(target: "export_utils", "import_type: Attempting to redefine typedef {name}", name = my_name.to_str());
}
(None, my_name)
} else {
self.import_typespec(spec, &type_.spec, public_name)
}
}
fn import_typespec(&mut self, spec: &Spec, type_spec: &TypeSpec, public_name: Option<NodeName>) -> (Option<HashSet<NodeName>>, NodeName) {
debug!(target: "export_utils", "import_typespec {:?} => {:?}", public_name, type_spec);
match *type_spec {
TypeSpec::Boolean |
TypeSpec::Number |
TypeSpec::String |
TypeSpec::Offset |
TypeSpec::Void => {
if let Some(ref my_name) = public_name {
if let Some(ref mut typedef) = self.builder.add_typedef(&my_name) {
debug!(target: "export_utils", "import_typespec: Defining {name} (primitive)", name = my_name.to_str());
typedef.with_type(type_spec.clone().required());
} else {
debug!(target: "export_utils", "import_typespec: Attempting to redefine typedef {name}", name = my_name.to_str());
}
}
(None, self.builder.node_name("@@"))
}
TypeSpec::NamedType(ref link) => {
let resolved = spec.get_type_by_name(link)
.unwrap_or_else(|| panic!("While deanonymizing, could not find the definition of {} in the original spec.", link.to_str()));
let (sum, rewrite, primitive) = match resolved {
NamedType::StringEnum(_) => {
(None, None, None)
}
NamedType::Typedef(ref type_) => {
let (sum, name) = self.import_type(spec, type_, Some(link.clone()));
(sum, Some(name), type_.get_primitive(spec))
}
NamedType::Interface(_) => {
let sum = [link.clone()].iter()
.cloned()
.collect();
(Some(sum), None, None)
}
};
debug!(target: "export_utils", "import_typespec dealing with named type {}, public name {:?} => {:?}",
link, public_name, rewrite);
if let Some(ref my_name) = public_name {
if let Some(content) = rewrite {
let deanonymized = match primitive {
None |
Some(IsNullable { is_nullable: true, .. }) |
Some(IsNullable { content: Primitive::Interface(_), .. }) => Type::named(&content).required(),
Some(IsNullable { content: Primitive::String, .. }) => Type::string().required(),
Some(IsNullable { content: Primitive::Number, .. }) => Type::number().required(),
Some(IsNullable { content: Primitive::Boolean, .. }) => Type::bool().required(),
Some(IsNullable { content: Primitive::Offset, .. }) => Type::offset().required(),
Some(IsNullable { content: Primitive::Void, .. }) => Type::void().required()
};
debug!(target: "export_utils", "import_typespec aliasing {:?} => {:?}",
my_name, deanonymized);
if let Some(ref mut typedef) = self.builder.add_typedef(&my_name) {
debug!(target: "export_utils", "import_typespec: Defining {name} (name to content)", name = my_name.to_str());
typedef.with_type(deanonymized.clone());
} else {
debug!(target: "export_utils", "import_typespec: Attempting to redefine typedef {name}", name = my_name.to_str());
}
}
let deanonymized = Type::named(link).required();
if let Some(ref mut typedef) = self.builder.add_typedef(&my_name) {
debug!(target: "export_utils", "import_typespec: Defining {name} (name to link)", name = my_name.to_str());
typedef.with_type(deanonymized.clone());
} else {
debug!(target: "export_utils", "import_typespec: Attempting to redefine typedef {name}", name = my_name.to_str());
}
}
(sum, link.clone())
}
TypeSpec::Array {
ref contents,
ref supports_empty
} => {
let (_, contents_name) = self.import_type(spec, contents, None);
let my_name =
match public_name {
None => self.builder.node_name(&format!("{non_empty}ListOf{content}",
non_empty =
if *supports_empty {
""
} else {
"NonEmpty"
},
content = contents_name.to_str())),
Some(ref name) => name.clone()
};
let deanonymized =
if *supports_empty {
Type::named(&contents_name).array()
} else {
Type::named(&contents_name).non_empty_array()
};
if let Some(ref mut typedef) = self.builder.add_typedef(&my_name) {
debug!(target: "export_utils", "import_typespec: Defining {name} (name to list)",
name = my_name.to_str());
typedef.with_type(deanonymized.clone());
} else {
debug!(target: "export_utils", "import_typespec: Attempting to redefine typedef {name}", name = my_name.to_str());
}
(None, my_name)
}
TypeSpec::TypeSum(ref sum) => {
let mut full_sum = HashSet::new();
let mut names = vec![];
for sub_type in sum.types() {
let (mut sub_sum, name) = self.import_typespec(spec, sub_type, None);
let mut sub_sum = sub_sum.unwrap_or_else(
|| panic!("While treating {:?}, attempting to create a sum containing {}, which isn't an interface or a sum of interfaces", type_spec, name)
);
names.push(name);
for item in sub_sum.drain() {
full_sum.insert(item);
}
}
let my_name =
match public_name {
None => self.builder.node_name(&format!("{}",
names.drain(..).format("Or"))),
Some(ref name) => name.clone()
};
let sum : Vec<_> = full_sum.iter()
.map(Type::named)
.collect();
let deanonymized = Type::sum(&sum).required();
if let Some(ref mut typedef) = self.builder.add_typedef(&my_name) {
debug!(target: "export_utils", "import_typespec: Defining {name} (name to sum)", name = my_name.to_str());
typedef.with_type(deanonymized.clone());
} else {
debug!(target: "export_utils", "import_type: Attempting to redefine typedef {name}", name = my_name.to_str());
}
(Some(full_sum), my_name)
}
}
}
}
pub struct TypeName;
impl TypeName {
pub fn type_(type_: &Type) -> String {
let spec_name = Self::type_spec(type_.spec());
if type_.is_optional() {
format!("Optional{}", spec_name)
} else {
spec_name
}
}
pub fn type_spec(spec: &TypeSpec) -> String {
match *spec {
TypeSpec::Array { ref contents, supports_empty: false } =>
format!("NonEmptyListOf{}", Self::type_(contents)),
TypeSpec::Array { ref contents, supports_empty: true } =>
format!("ListOf{}", Self::type_(contents)),
TypeSpec::NamedType(ref name) =>
name.to_string().clone(),
TypeSpec::Offset =>
"_Offset".to_string(),
TypeSpec::Boolean =>
"_Bool".to_string(),
TypeSpec::Number =>
"_Number".to_string(),
TypeSpec::String =>
"_String".to_string(),
TypeSpec::Void =>
"_Void".to_string(),
TypeSpec::TypeSum(ref sum) => {
format!("{}", sum.types()
.iter()
.map(Self::type_spec)
.format("Or"))
}
}
}
}
pub struct ToWebidl;
impl ToWebidl {
pub fn spec(spec: &TypeSpec, prefix: &str, indent: &str) -> Option<String> {
let result = match *spec {
TypeSpec::Offset => {
return None;
}
TypeSpec::Array { ref contents, ref supports_empty } => {
match Self::type_(&*contents, prefix, indent) {
None => { return None; }
Some(description) => format!("{emptiness}FrozenArray<{}>",
description,
emptiness = if *supports_empty { "" } else {"[NonEmpty] "} ),
}
}
TypeSpec::Boolean =>
"bool".to_string(),
TypeSpec::String =>
"string".to_string(),
TypeSpec::Number =>
"number".to_string(),
TypeSpec::NamedType(ref name) =>
name.to_str().to_string(),
TypeSpec::TypeSum(ref sum) => {
format!("({})", sum.types()
.iter()
.filter_map(|x| Self::spec(x, "", indent))
.format(" or "))
}
TypeSpec::Void => "void".to_string()
};
Some(result)
}
pub fn type_(type_: &Type, prefix: &str, indent: &str) -> Option<String> {
let pretty_type = Self::spec(type_.spec(), prefix, indent);
match pretty_type {
None => None,
Some(pretty_type) => Some(format!("{}{}",
pretty_type,
if type_.is_optional() { "?" } else { "" }))
}
}
pub fn interface(interface: &Interface, prefix: &str, indent: &str) -> String {
let mut result = format!("{prefix} interface {name} : Node {{\n", prefix=prefix, name=interface.name().to_str());
{
let prefix = format!("{prefix}{indent}",
prefix=prefix,
indent=indent);
for field in interface.contents().fields() {
match Self::type_(field.type_(), &prefix, indent) {
None => {},
Some(description) => {
if let Some(ref doc) = field.doc() {
result.push_str(&format!("{prefix}// {doc}\n", prefix = prefix, doc = doc));
}
result.push_str(&format!("{prefix}{description} {name};\n",
prefix = prefix,
name = field.name().to_str(),
description = description
));
if field.doc().is_some() {
result.push_str("\n");
}
}
}
}
}
result.push_str(&format!("{prefix} }}\n", prefix=prefix));
result
}
}