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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use crate::extend::ExtendGenerics;
use weedle::common::Generics;
use weedle::term::*;
use weedle::types::*;

/// Extension methods for `MayBeNull<T>`
pub trait ExtendMayBeNull<T> {
	fn new(type_: T, q_mark: Option<QMark>) -> Self;
	fn new_optional(type_: T) -> Self;
	fn new_required(type_: T) -> Self;
	fn is_optional(&self) -> bool;
	fn is_required(&self) -> bool;
}

impl<T> ExtendMayBeNull<T> for MayBeNull<T> {
	fn new(type_: T, q_mark: Option<QMark>) -> Self {
		Self { type_, q_mark }
	}

	fn new_optional(type_: T) -> Self {
		Self::new(type_, Some(QMark))
	}

	fn new_required(type_: T) -> Self {
		Self::new(type_, None)
	}

	fn is_optional(&self) -> bool {
		self.q_mark.is_some()
	}

	fn is_required(&self) -> bool {
		self.q_mark.is_none()
	}
}

/// Extension methods for floating point types
pub trait ExtendFloatingPointType {
	fn unrestricted(&self) -> Option<Unrestricted>;
}

/// Extension methods for creating floating point types
pub trait ExtendFloatingPointTypeNew {
	type FloatType;
	fn new(unrestricted: Option<Unrestricted>, ty: Self::FloatType) -> Self;
	fn new_unrestricted() -> Self;
	fn new_restricted() -> Self;
}

impl ExtendFloatingPointType for FloatingPointType {
	fn unrestricted(&self) -> Option<Unrestricted> {
		match self {
			Self::Float(f) => f.unrestricted,
			Self::Double(f) => f.unrestricted,
		}
	}
}

impl ExtendFloatingPointType for DoubleType {
	fn unrestricted(&self) -> Option<Unrestricted> {
		self.unrestricted
	}
}

impl ExtendFloatingPointTypeNew for DoubleType {
	type FloatType = Double;

	fn new(unrestricted: Option<Unrestricted>, ty: Self::FloatType) -> Self {
		Self {
			unrestricted,
			double: ty,
		}
	}

	fn new_unrestricted() -> Self {
		Self::new(Some(Unrestricted), Double)
	}

	fn new_restricted() -> Self {
		Self::new(None, Double)
	}
}

impl ExtendFloatingPointType for FloatType {
	fn unrestricted(&self) -> Option<Unrestricted> {
		self.unrestricted
	}
}

impl ExtendFloatingPointTypeNew for FloatType {
	type FloatType = Float;

	fn new(unrestricted: Option<Unrestricted>, ty: Self::FloatType) -> Self {
		Self {
			unrestricted,
			float: ty,
		}
	}

	fn new_unrestricted() -> Self {
		Self::new(Some(Unrestricted), Float)
	}

	fn new_restricted() -> Self {
		Self::new(None, Float)
	}
}

/// Extension methods for integer types
pub trait ExtendIntegerType {
	fn unsigned(&self) -> Option<Unsigned>;
}

/// Extension methods for creating integer types
pub trait ExtendIntegerTypeNew {
	type IntegerType;
	fn new(unsigned: Option<Unsigned>, ty: Self::IntegerType) -> Self;
	fn new_signed() -> Self;
	fn new_unsigned() -> Self;
}

impl ExtendIntegerType for IntegerType {
	fn unsigned(&self) -> Option<Unsigned> {
		match self {
			Self::Short(i) => i.unsigned,
			Self::Long(i) => i.unsigned,
			Self::LongLong(i) => i.unsigned,
		}
	}
}

impl ExtendIntegerType for ShortType {
	fn unsigned(&self) -> Option<Unsigned> {
		self.unsigned
	}
}

impl ExtendIntegerTypeNew for ShortType {
	type IntegerType = Short;

	fn new(unsigned: Option<Unsigned>, short: Self::IntegerType) -> Self {
		Self { unsigned, short }
	}

	fn new_signed() -> Self {
		Self::new(None, Short)
	}

	fn new_unsigned() -> Self {
		Self::new(Some(Unsigned), Short)
	}
}

impl ExtendIntegerType for LongType {
	fn unsigned(&self) -> Option<Unsigned> {
		self.unsigned
	}
}

impl ExtendIntegerTypeNew for LongType {
	type IntegerType = Long;

	fn new(unsigned: Option<Unsigned>, long: Self::IntegerType) -> Self {
		Self { unsigned, long }
	}

	fn new_signed() -> Self {
		Self::new(None, Long)
	}

	fn new_unsigned() -> Self {
		Self::new(Some(Unsigned), Long)
	}
}

impl ExtendIntegerType for LongLongType {
	fn unsigned(&self) -> Option<Unsigned> {
		self.unsigned
	}
}

impl ExtendIntegerTypeNew for LongLongType {
	type IntegerType = (Long, Long);

	#[rustfmt::skip]
	fn new(unsigned: Option<Unsigned>, long_long: Self::IntegerType) -> Self {
		Self { unsigned, long_long }
	}

	fn new_signed() -> Self {
		Self::new(None, (Long, Long))
	}

	fn new_unsigned() -> Self {
		Self::new(Some(Unsigned), (Long, Long))
	}
}

/// Extension methods for `PromiseType`
pub trait ExtendPromiseType<'a> {
	fn new(ty: ReturnType<'a>) -> Self;
}

impl<'a> ExtendPromiseType<'a> for PromiseType<'a> {
	fn new(ty: ReturnType<'a>) -> Self {
		Self {
			promise: Promise,
			generics: Generics::new(Box::new(ty)),
		}
	}
}

/// Extension methods for `SequenceType`
pub trait ExtendSequenceType<'a> {
	fn new(ty: Type<'a>) -> Self;
}

impl<'a> ExtendSequenceType<'a> for SequenceType<'a> {
	fn new(ty: Type<'a>) -> Self {
		Self {
			sequence: Sequence,
			generics: Generics::new(Box::new(ty)),
		}
	}
}

/// Extension methods for `FrozenArrayType`
pub trait ExtendFrozenArrayType<'a> {
	fn new(ty: Type<'a>) -> Self;
}

impl<'a> ExtendFrozenArrayType<'a> for FrozenArrayType<'a> {
	fn new(ty: Type<'a>) -> Self {
		Self {
			frozen_array: FrozenArray,
			generics: Generics::new(Box::new(ty)),
		}
	}
}

/// Extension methods for `RecordType`
pub trait ExtendRecordType<'a> {
	fn new(key: RecordKeyType<'a>, value: Type<'a>) -> Self;
}

impl<'a> ExtendRecordType<'a> for RecordType<'a> {
	fn new(key: RecordKeyType<'a>, value: Type<'a>) -> Self {
		Self {
			record: Record,
			generics: Generics::new((Box::new(key), Comma, Box::new(value))),
		}
	}
}

/// Extension methods for `RecordKeyType`
pub trait ExtendRecordKeyType<'a> {
	fn byte() -> Self;
	fn dom() -> Self;
	fn usv() -> Self;
	fn non_any(nat: NonAnyType<'a>) -> Self;
}

impl<'a> ExtendRecordKeyType<'a> for RecordKeyType<'a> {
	fn byte() -> Self {
		Self::Byte(ByteString)
	}

	fn dom() -> Self {
		Self::DOM(DOMString)
	}

	fn usv() -> Self {
		Self::USV(USVString)
	}

	fn non_any(nat: NonAnyType<'a>) -> Self {
		Self::NonAny(nat)
	}
}

/// Extension methods for `Type`
pub trait ExtendType {
	fn single_any() -> Self;
}

impl<'a> ExtendType for Type<'a> {
	fn single_any() -> Self {
		Self::Single(SingleType::any())
	}
}

/// Extension methods for `SingleType`
pub trait ExtendSingleType {
	fn any() -> Self;
}

impl<'a> ExtendSingleType for SingleType<'a> {
	fn any() -> Self {
		Self::Any(Any)
	}
}

#[cfg(test)]
mod extend_maybenull {
	use crate::extend::ExtendMayBeNull;
	use weedle::term::{Boolean, Byte, QMark};
	use weedle::types::MayBeNull;

	#[test]
	fn test_new() {
		let maybe = MayBeNull::new(Byte, Some(QMark));
		assert_eq!(maybe.type_, Byte);
		assert_eq!(maybe.q_mark, Some(QMark));
	}

	#[test]
	fn test_is_required() {
		let maybe = MayBeNull::new_required(Boolean);
		assert!(maybe.is_required());
		assert!(maybe.q_mark.is_none());
	}
}

#[cfg(test)]
mod extend_float {
	use crate::extend::{ExtendFloatingPointType, ExtendFloatingPointTypeNew};
	use weedle::term::Unrestricted;
	use weedle::types::{DoubleType, FloatType, FloatingPointType};

	#[test]
	fn test_unrestricted_webidl_f32() {
		let wi_f32 = FloatingPointType::Float(FloatType::new_unrestricted());
		assert_eq!(wi_f32.unrestricted(), Some(Unrestricted));
	}

	#[test]
	fn test_unrestricted_webidl_f64() {
		let wi_f64 = FloatingPointType::Double(DoubleType::new_unrestricted());
		assert_eq!(wi_f64.unrestricted(), Some(Unrestricted));
	}
}

#[cfg(test)]
mod extend_integer {
	use crate::extend::{ExtendIntegerType, ExtendIntegerTypeNew};
	use weedle::term::Unsigned;
	use weedle::types::{IntegerType, LongLongType, LongType, ShortType};

	#[test]
	fn test_short() {
		let int1 = IntegerType::Short(ShortType::new_unsigned());
		assert_eq!(int1.unsigned(), Some(Unsigned));

		let int2 = IntegerType::Short(ShortType::new_signed());
		assert_eq!(int2.unsigned(), None);
	}

	#[test]
	fn test_long() {
		let int1 = IntegerType::Long(LongType::new_unsigned());
		assert_eq!(int1.unsigned(), Some(Unsigned));

		let int2 = IntegerType::Long(LongType::new_signed());
		assert_eq!(int2.unsigned(), None);
	}

	#[test]
	fn test_long_long() {
		let int1 = IntegerType::LongLong(LongLongType::new_unsigned());
		assert_eq!(int1.unsigned(), Some(Unsigned));

		let int2 = IntegerType::LongLong(LongLongType::new_signed());
		assert_eq!(int2.unsigned(), None);
	}
}

#[cfg(test)]
mod extend_promise {
	use crate::extend::{ExtendGenerics, ExtendNonAnyType, ExtendPromiseType};
	use weedle::common::Generics;
	use weedle::term::Promise;
	use weedle::types::{NonAnyType, PromiseType, ReturnType, SingleType, Type};

	#[test]
	fn test_new() {
		let p = PromiseType::new(ReturnType::Type(Type::Single(SingleType::NonAny(
			NonAnyType::boolean(),
		))));

		assert_eq!(p.promise, Promise);
		assert_eq!(
			p.generics,
			Generics::new(Box::new(ReturnType::Type(Type::Single(
				SingleType::NonAny(NonAnyType::boolean())
			))))
		);
	}
}

#[cfg(test)]
mod extend_frozen_array {
	use crate::extend::{ExtendFrozenArrayType, ExtendGenerics, ExtendNonAnyType};
	use weedle::common::Generics;
	use weedle::term::FrozenArray;
	use weedle::types::{FrozenArrayType, NonAnyType, SingleType, Type};

	#[test]
	fn test_new() {
		let f = FrozenArrayType::new(Type::Single(SingleType::NonAny(NonAnyType::boolean())));

		assert_eq!(f.frozen_array, FrozenArray);
		assert_eq!(
			f.generics,
			Generics::new(Box::new(Type::Single(SingleType::NonAny(
				NonAnyType::boolean()
			))))
		);
	}
}

#[cfg(test)]
mod extend_record {
	use crate::extend::{ExtendRecordKeyType, ExtendRecordType, ExtendType};
	use weedle::types::{RecordKeyType, RecordType, Type};

	#[test]
	fn test_new() {
		let r = RecordType::new(RecordKeyType::byte(), Type::single_any());

		assert_eq!(*(r.generics.body.0), RecordKeyType::byte());
		assert_eq!(*(r.generics.body.2), Type::single_any());
	}
}

#[cfg(test)]
mod extend_record_key {
	use crate::extend::{ExtendNonAnyType, ExtendRecordKeyType};
	use weedle::term::{Boolean, ByteString, DOMString, USVString};
	use weedle::types::{MayBeNull, NonAnyType, RecordKeyType};

	#[test]
	fn test_variant_byte() {
		assert_eq!(RecordKeyType::byte(), RecordKeyType::Byte(ByteString));
	}

	#[test]
	fn test_variant_dom() {
		assert_eq!(RecordKeyType::dom(), RecordKeyType::DOM(DOMString));
	}

	#[test]
	fn test_variant_usv() {
		assert_eq!(RecordKeyType::usv(), RecordKeyType::USV(USVString));
	}

	#[test]
	fn test_variant_nat() {
		assert_eq!(
			RecordKeyType::non_any(NonAnyType::boolean()),
			RecordKeyType::NonAny(NonAnyType::Boolean(MayBeNull {
				type_: Boolean,
				q_mark: None,
			}))
		);
	}
}