-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
105 lines (93 loc) · 2.9 KB
/
schema.js
File metadata and controls
105 lines (93 loc) · 2.9 KB
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
import { z } from 'zod/v4';
export const IdentifierSchema = z.string().regex(/^[a-zA-Z0-9]+$/);
export const CommentSchema = z.string().optional();
export const Uint8Schema = z.int().min(0).max(255);
export const Uint16Schema = z.int().min(0).max(0xffff);
// Note: Types can be expressions of the form
// OcaMap<OcaUint8,OcaString>[3]
// This is not possible to express in a regexp but
// the following gets close.
export const TypeSchema = z.string().regex(/^[a-zA-Z0-9<>,[\]]+$/)
export function makeObject(o) {
return z.object(Object.assign({
comment: CommentSchema,
}, o));
}
export const PropertySchema = makeObject({
name: IdentifierSchema,
type: TypeSchema,
});
function makeEnum(name, valueType) {
return makeObject({
type: z.literal(name),
name: IdentifierSchema,
enumerators: z.array(makeObject({
name: IdentifierSchema,
value: valueType,
})),
extension_base: valueType.optional(),
});
}
export const Enum8Schema = makeEnum('enum8', Uint8Schema);
export const Enum16Schema = makeEnum('enum16', Uint16Schema);
const allBits16 = new Array(16).fill(1).map((one, index) => 1 << index);
export const Bitset16Schema = makeObject({
type: z.literal('bitset16'),
name: IdentifierSchema,
bits: z.array(makeObject({
name: IdentifierSchema,
value: z.union(allBits16.map((value) => z.literal(value))),
})),
});
export const StructSchema = makeObject({
type: z.literal('struct'),
name: IdentifierSchema,
properties: z.array(PropertySchema)
});
export const TypeDefSchema = makeObject({
type: z.literal('typedef'),
name: IdentifierSchema,
target: TypeSchema,
});
export const LevelAndIndexSchema = makeObject({
level: Uint16Schema,
index: Uint16Schema,
});
export const ClassIdSchema = z.string().regex(/^\d+(\.\d+)*$/);
export const CIDSchema = z.string().regex(/^[a-fA-F0-9]{6}$/)
export const ControlClassSchema = makeObject({
type: z.literal('control_class'),
name: IdentifierSchema,
parent: IdentifierSchema,
classid: z.union([
ClassIdSchema,
makeObject({
cid: CIDSchema.optional(),
suffix: ClassIdSchema
}),
]),
properties: z.array(makeObject({
name: IdentifierSchema,
type: TypeSchema,
id: LevelAndIndexSchema,
})).optional(),
methods: z.array(makeObject({
name: IdentifierSchema,
id: LevelAndIndexSchema,
arguments: z.array(PropertySchema),
return_values: z.array(PropertySchema),
})).optional(),
events: z.array(makeObject({
name: IdentifierSchema,
id: LevelAndIndexSchema,
argument: PropertySchema,
})).optional(),
});
export const Schema = z.discriminatedUnion("type", [
Enum8Schema,
Enum16Schema,
Bitset16Schema,
StructSchema,
TypeDefSchema,
ControlClassSchema,
]);