-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.ts
174 lines (155 loc) · 4.48 KB
/
schema.ts
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
import {
any,
array,
Context,
defaulted,
enums,
integer,
literal,
max,
min,
number,
object,
optional,
partial,
refine,
size,
string,
Struct,
tuple,
} from "superstruct";
//=[ Monkeypatching ]===========================================================
// source: https://github.com/ianstormtaylor/superstruct/pull/966
// solves: https://github.com/ianstormtaylor/superstruct/issues/830
// TODO: remove when PR is merged
// NOTE: this is needed only by the bounded vertices count int MainItem
function dynamic<T>(
fn: (value: unknown, ctx: Context) => Struct<T, any>,
): Struct<T, null> {
return new Struct({
type: "dynamic",
schema: null,
*entries(value, ctx) {
const struct = fn(value, ctx);
yield* struct.entries(value, ctx);
},
validator(value, ctx) {
const struct = fn(value, ctx);
return struct.validator(value, ctx);
},
coercer(value, ctx) {
const struct = fn(value, ctx);
return struct.coercer(value, ctx);
},
refiner(value, ctx) {
const struct = fn(value, ctx);
return struct.refiner(value, ctx);
},
});
}
//=[ Basic ]====================================================================
export const Alignment = max(min(integer(), 0), 3);
export const Angle = max(min(integer(), 0), 360);
export const Factor = max(min(number(), 0), 1);
export const Level = max(min(integer(), 1), 3);
export const Natural = min(integer(), 0);
export const Percent = max(min(integer(), 0), 100);
export const Version = literal(0);
//=[ Composite ]================================================================
export const Colour = tuple([
Angle,
Percent,
Percent,
defaulted(optional(Factor), 1),
]);
export const Shorten = refine(
object({
source: defaulted(optional(Percent), 0),
target: defaulted(optional(Percent), 0),
}),
"Shorten",
({ source = 0, target = 0 }) => source + target <= 100,
); // TODO: decent error message
//=[ Edge ]=====================================================================
export const EdgeStyleBody = partial(
object({
level: Level, // TODO: mark as deprecated
name: defaulted(
enums(["cell", "squiggly", "barred", "dashed", "dotted", "none"]),
"cell",
),
}),
);
export const EdgeStyleHead = partial(
object({
name: defaulted(
enums(["arrowhead", "none", "epi", "harpoon"]),
"arrowhead",
),
side: enums(["top", "bottom"]), // NOTE: used only by harpoon
}),
);
export const EdgeStyleTail = partial(
object({
name: defaulted(
enums(["none", "maps to", "mono", "hook", "arrowhead"]),
"none",
),
side: enums(["top", "bottom"]), // NOTE: used only by hook
}),
);
export const EdgeStyle = partial(
object({
// NOTE: tail/body/head are used only when name is arrow
body: defaulted(EdgeStyleBody, {}),
head: defaulted(EdgeStyleHead, {}),
name: defaulted(
enums(["arrow", "adjunction", "corner", "corner-inverse"]),
"arrow",
),
tail: defaulted(EdgeStyleTail, {}),
}),
);
export const EdgeOptions = partial(
object({
colour: defaulted(Colour, [0, 0, 0, 1]),
curve: defaulted(integer(), 0),
label_position: defaulted(Percent, 50),
length: Percent, // TODO: mark as deprecated
level: defaulted(Level, 1),
offset: defaulted(integer(), 0),
shorten: defaulted(Shorten, {}),
style: defaulted(EdgeStyle, {}),
}),
);
export const Edge = edge();
export function edge(self = -1, max = Infinity) {
return refine(
tuple([
refine(Natural, "Source", (n) => n != self && n <= max),
refine(Natural, "Target", (n) => n != self && n <= max),
defaulted(optional(string()), ""),
defaulted(optional(Alignment), 0),
defaulted(optional(EdgeOptions), {}),
defaulted(optional(Colour), [0, 0, 0]),
]),
"Edge",
([source, target]) => source != target,
);
}
//=[ Vertex ]===================================================================
export const Vertex = tuple([
integer(),
integer(),
defaulted(optional(string()), ""),
defaulted(optional(Colour), [0, 0, 0]),
]);
//=[ Main ]=====================================================================
const MainItem = dynamic((_, { path: [index], branch: [main] }) => {
if (index == 0) return Version;
if (index == 1) return max(Natural, main.length - 2);
if (index - 1 <= main[1]) return Vertex;
if (index - 1 > main[1]) return edge(index - 2, main.length - 3);
return any();
});
export const Main = size(array(MainItem), 3, Infinity);