@mica-js/document 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -2
- package/dist/canvas/index.js +2 -1
- package/dist/canvas/index.js.map +1 -1
- package/dist/fused/index.d.ts +643 -0
- package/dist/fused/index.js +2878 -0
- package/dist/fused/index.js.map +1 -0
- package/dist/schematic/index.js +2 -1
- package/dist/schematic/index.js.map +1 -1
- package/dist/uuid-DegOw3iI.js +10 -0
- package/dist/uuid-DegOw3iI.js.map +1 -0
- package/dist/{validate-Cz-ZbbEr.js → validate-Co8aIzz6.js} +2 -9
- package/dist/validate-Co8aIzz6.js.map +1 -0
- package/package.json +5 -1
- package/dist/validate-Cz-ZbbEr.js.map +0 -1
|
@@ -0,0 +1,2878 @@
|
|
|
1
|
+
import { t as isUuidV4 } from "../uuid-DegOw3iI.js";
|
|
2
|
+
//#region src/fused/diagnostics.ts
|
|
3
|
+
/** JSON Pointer of a field path; segments escape `~` and `/` (RFC 6901). */
|
|
4
|
+
function toPointer(path) {
|
|
5
|
+
if (path.length === 0) return "";
|
|
6
|
+
return `/${path.map((segment) => segment.replace(/~/g, "~0").replace(/\//g, "~1")).join("/")}`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Thrown when fused device data violates its contract.
|
|
10
|
+
*
|
|
11
|
+
* `diagnostics` carries every problem found, each with an explicit code and JSON Pointer, so a
|
|
12
|
+
* host can report them without re-parsing.
|
|
13
|
+
*/
|
|
14
|
+
var FusedDocumentError = class extends Error {
|
|
15
|
+
diagnostics;
|
|
16
|
+
constructor(diagnostics) {
|
|
17
|
+
super(buildMessage(diagnostics));
|
|
18
|
+
this.name = "FusedDocumentError";
|
|
19
|
+
this.diagnostics = diagnostics;
|
|
20
|
+
}
|
|
21
|
+
/** Code of the first diagnostic, for coarse programmatic handling. */
|
|
22
|
+
get code() {
|
|
23
|
+
return this.diagnostics[0]?.code;
|
|
24
|
+
}
|
|
25
|
+
/** Field path of the first diagnostic, for coarse programmatic handling. */
|
|
26
|
+
get pointer() {
|
|
27
|
+
return this.diagnostics[0]?.pointer;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/** Records one diagnostic for `path` as an error. */
|
|
31
|
+
function report(diagnostics, code, message, path) {
|
|
32
|
+
diagnostics.push({
|
|
33
|
+
severity: "error",
|
|
34
|
+
code,
|
|
35
|
+
message,
|
|
36
|
+
pointer: toPointer(path)
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function buildMessage(diagnostics) {
|
|
40
|
+
const errors = diagnostics.filter((diagnostic) => diagnostic.severity === "error");
|
|
41
|
+
const [first, ...rest] = errors.length > 0 ? errors : diagnostics;
|
|
42
|
+
if (first === void 0) return "Fused device validation failed.";
|
|
43
|
+
const location = first.pointer === "" ? "" : ` at ${first.pointer}`;
|
|
44
|
+
const suffix = rest.length === 0 ? "" : ` (and ${rest.length} more)`;
|
|
45
|
+
return `${first.code}${location}: ${first.message}${suffix}`;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/fused/rules.ts
|
|
49
|
+
/** Lexical rules of device identity, versions, paths and member names (RFC 0020 / RFC 0024). */
|
|
50
|
+
var NAME_SEGMENT = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
51
|
+
var DEVICE_ID = /^[a-z0-9]+(?:-[a-z0-9]+)*:[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
52
|
+
var SEMVER = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
53
|
+
var INTEGRITY = /^sha256:[0-9a-f]{64}$/;
|
|
54
|
+
var MEMBER_KEY = /^[A-Za-z][A-Za-z0-9]*$/;
|
|
55
|
+
var EVENT_KEY = /^onDid[A-Z][A-Za-z0-9]*$/;
|
|
56
|
+
var PIN_ID = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
57
|
+
var CODICON = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
58
|
+
var DESIGNATOR_PREFIX = /^[A-Za-z][A-Za-z0-9]*$/;
|
|
59
|
+
var EXTENSION = /^\.[A-Za-z0-9]+$/;
|
|
60
|
+
var COLOR = /^#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
61
|
+
var DRIVE_LETTER = /^[A-Za-z]:/;
|
|
62
|
+
var URL_SCHEME = /^[A-Za-z][A-Za-z0-9+.-]*:/;
|
|
63
|
+
/** True when the text carries C0 or DEL control characters. */
|
|
64
|
+
function hasControlCharacters(value) {
|
|
65
|
+
for (const character of value) {
|
|
66
|
+
const code = character.codePointAt(0) ?? 0;
|
|
67
|
+
if (code < 32 || code === 127) return true;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
/** Publisher namespace or device name segment; defaults to `U` for designator prefixes. */
|
|
72
|
+
function isNameSegment(value) {
|
|
73
|
+
return NAME_SEGMENT.test(value);
|
|
74
|
+
}
|
|
75
|
+
/** Full `publisher:name` device id (RFC 0020). */
|
|
76
|
+
function isDeviceId(value) {
|
|
77
|
+
return DEVICE_ID.test(value);
|
|
78
|
+
}
|
|
79
|
+
/** Problem of a `publisher:name` device id, or `undefined` when it is well formed. */
|
|
80
|
+
function deviceIdProblem(value) {
|
|
81
|
+
if (value.length === 0) return "must not be empty";
|
|
82
|
+
return isDeviceId(value) ? void 0 : "must be a \"publisher:name\" pair of lowercase dash-separated segments";
|
|
83
|
+
}
|
|
84
|
+
/** Full SemVer version, including prerelease and build metadata (RFC 0020). */
|
|
85
|
+
function isSemVer(value) {
|
|
86
|
+
return SEMVER.test(value);
|
|
87
|
+
}
|
|
88
|
+
/** Release identity or file digest: `sha256:` plus 64 lowercase hex digits (RFC 0020). */
|
|
89
|
+
function isIntegrity(value) {
|
|
90
|
+
return INTEGRITY.test(value);
|
|
91
|
+
}
|
|
92
|
+
/** Property, method, public type and view id key (RFC 0020). */
|
|
93
|
+
function isMemberKey(value) {
|
|
94
|
+
return MEMBER_KEY.test(value);
|
|
95
|
+
}
|
|
96
|
+
/** Event key in `onDidXxx` form (RFC 0020 / RFC 0021). */
|
|
97
|
+
function isEventKey(value) {
|
|
98
|
+
return EVENT_KEY.test(value);
|
|
99
|
+
}
|
|
100
|
+
/** Model pin name, matching the DDL identifier form (RFC 0021). */
|
|
101
|
+
function isPinId(value) {
|
|
102
|
+
return PIN_ID.test(value);
|
|
103
|
+
}
|
|
104
|
+
/** VS Code Codicons name supplied by the editor (RFC 0020). */
|
|
105
|
+
function isCodiconName(value) {
|
|
106
|
+
return CODICON.test(value);
|
|
107
|
+
}
|
|
108
|
+
/** Reference designator prefix (RFC 0020). */
|
|
109
|
+
function isDesignatorPrefix(value) {
|
|
110
|
+
return DESIGNATOR_PREFIX.test(value);
|
|
111
|
+
}
|
|
112
|
+
/** `binary` extension filter entry, e.g. `.bin` (RFC 0020). */
|
|
113
|
+
function isExtensionFilter(value) {
|
|
114
|
+
return EXTENSION.test(value);
|
|
115
|
+
}
|
|
116
|
+
/** `#RRGGBB` / `#RRGGBBAA` color (RFC 0009). */
|
|
117
|
+
function isColor(value) {
|
|
118
|
+
return COLOR.test(value);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Package-root relative POSIX file path problem, or `undefined` when it is well formed.
|
|
122
|
+
*
|
|
123
|
+
* Case sensitivity, empty segments, `.` / `..`, backslashes and absolute paths are rejected
|
|
124
|
+
* (RFC 0020).
|
|
125
|
+
*/
|
|
126
|
+
function packagePathProblem(path) {
|
|
127
|
+
return relativePathProblem(path, "package root");
|
|
128
|
+
}
|
|
129
|
+
/** Project-root relative POSIX file path problem, or `undefined` when it is well formed. */
|
|
130
|
+
function projectPathProblem(path) {
|
|
131
|
+
return relativePathProblem(path, "project root");
|
|
132
|
+
}
|
|
133
|
+
function relativePathProblem(path, root) {
|
|
134
|
+
if (path.length === 0) return "must not be empty";
|
|
135
|
+
if (path.startsWith("/")) return `must be relative to the ${root}, not absolute`;
|
|
136
|
+
if (DRIVE_LETTER.test(path)) return "must not carry a drive letter";
|
|
137
|
+
if (URL_SCHEME.test(path)) return "must not be a URL";
|
|
138
|
+
if (path.includes("\\")) return "must use POSIX separators";
|
|
139
|
+
if (hasControlCharacters(path)) return "must not contain control characters";
|
|
140
|
+
const segments = path.split("/");
|
|
141
|
+
if (segments[segments.length - 1] === "") return "must name a file, not a directory";
|
|
142
|
+
for (const segment of segments) {
|
|
143
|
+
if (segment.length === 0) return "must not contain empty segments";
|
|
144
|
+
if (segment === "." || segment === "..") return `must not contain "${segment}" segments`;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** Problem of a file name that must end in one of `extensions`, or `undefined` when it does. */
|
|
148
|
+
function extensionProblem(path, extensions) {
|
|
149
|
+
const lower = path.toLowerCase();
|
|
150
|
+
return extensions.map((extension) => extension.toLowerCase()).some((extension) => lower.endsWith(extension)) ? void 0 : `must name a ${extensions.join(" or ")} file`;
|
|
151
|
+
}
|
|
152
|
+
/** `sha256:` digest problem of a release identity or file digest, or `undefined` when valid. */
|
|
153
|
+
function integrityProblem(value) {
|
|
154
|
+
return isIntegrity(value) ? void 0 : "must be \"sha256:\" followed by 64 lowercase hexadecimal digits";
|
|
155
|
+
}
|
|
156
|
+
/** Normalizes `#RRGGBB` / `#RRGGBBAA` to upper case; `undefined` when the text is not a color. */
|
|
157
|
+
function normalizeColor(value) {
|
|
158
|
+
return isColor(value) ? value.toUpperCase() : void 0;
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/fused/json.ts
|
|
162
|
+
/**
|
|
163
|
+
* Strict JSON reader for the fused contracts.
|
|
164
|
+
*
|
|
165
|
+
* `JSON.parse` silently keeps the last of two equal keys, which would let a manifest or a `.sch`
|
|
166
|
+
* file carry two different values for one field. RFC 0020 and RFC 0024 require rejecting
|
|
167
|
+
* duplicate keys, so the parsers decode JSON text with this reader instead. Keys are compared
|
|
168
|
+
* after unescaping, so `"a"` and `"\u0061"` are the same key.
|
|
169
|
+
*/
|
|
170
|
+
/** Deepest object/array nesting the reader accepts. */
|
|
171
|
+
var STRICT_JSON_MAX_DEPTH = 256;
|
|
172
|
+
/**
|
|
173
|
+
* Decodes JSON text, rejecting duplicate keys and reporting every problem with a JSON Pointer.
|
|
174
|
+
*
|
|
175
|
+
* @throws {FusedDocumentError} when the text is not valid JSON or repeats an object key.
|
|
176
|
+
*/
|
|
177
|
+
function parseStrictJson(text) {
|
|
178
|
+
const result = decodeStrictJson(text);
|
|
179
|
+
if (!result.ok) throw new FusedDocumentError(result.diagnostics);
|
|
180
|
+
return result.value;
|
|
181
|
+
}
|
|
182
|
+
/** Non-throwing form of {@link parseStrictJson}; the caller decides how to surface the problems. */
|
|
183
|
+
function decodeStrictJson(text) {
|
|
184
|
+
return new Reader(text).decode();
|
|
185
|
+
}
|
|
186
|
+
var Reader = class {
|
|
187
|
+
#text;
|
|
188
|
+
#diagnostics = [];
|
|
189
|
+
#index = 0;
|
|
190
|
+
#failed = false;
|
|
191
|
+
constructor(text) {
|
|
192
|
+
this.#text = text;
|
|
193
|
+
}
|
|
194
|
+
decode() {
|
|
195
|
+
this.#skipWhitespace();
|
|
196
|
+
const value = this.#readValue([], 0);
|
|
197
|
+
if (this.#failed) return this.#failure();
|
|
198
|
+
this.#skipWhitespace();
|
|
199
|
+
if (this.#index !== this.#text.length) {
|
|
200
|
+
this.#fail("Unexpected content after the JSON value.");
|
|
201
|
+
return this.#failure();
|
|
202
|
+
}
|
|
203
|
+
if (this.#diagnostics.length > 0) return this.#failure();
|
|
204
|
+
return {
|
|
205
|
+
ok: true,
|
|
206
|
+
value
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
#failure() {
|
|
210
|
+
return {
|
|
211
|
+
ok: false,
|
|
212
|
+
diagnostics: this.#diagnostics
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
#fail(message) {
|
|
216
|
+
if (!this.#failed) report(this.#diagnostics, "json/invalid", `Invalid JSON at offset ${String(this.#index)}: ${message}`, []);
|
|
217
|
+
this.#failed = true;
|
|
218
|
+
}
|
|
219
|
+
#skipWhitespace() {
|
|
220
|
+
while (this.#index < this.#text.length) {
|
|
221
|
+
const code = this.#text.charCodeAt(this.#index);
|
|
222
|
+
if (code !== 32 && code !== 9 && code !== 10 && code !== 13) return;
|
|
223
|
+
this.#index += 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
#readValue(path, depth) {
|
|
227
|
+
this.#skipWhitespace();
|
|
228
|
+
if (this.#index >= this.#text.length) return this.#fail("Unexpected end of input; expected a value.");
|
|
229
|
+
if (depth > 256) return this.#fail(`Nesting deeper than ${String(256)} is not supported.`);
|
|
230
|
+
switch (this.#text[this.#index]) {
|
|
231
|
+
case "{": return this.#readObject(path, depth + 1);
|
|
232
|
+
case "[": return this.#readArray(path, depth + 1);
|
|
233
|
+
case "\"": return this.#readString();
|
|
234
|
+
case "t": return this.#readKeyword("true", true);
|
|
235
|
+
case "f": return this.#readKeyword("false", false);
|
|
236
|
+
case "n": return this.#readKeyword("null", null);
|
|
237
|
+
default: return this.#readNumber();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
#readKeyword(keyword, value) {
|
|
241
|
+
if (this.#text.startsWith(keyword, this.#index)) {
|
|
242
|
+
this.#index += keyword.length;
|
|
243
|
+
return value;
|
|
244
|
+
}
|
|
245
|
+
return this.#fail(`Expected ${keyword}.`);
|
|
246
|
+
}
|
|
247
|
+
#readNumber() {
|
|
248
|
+
NUMBER.lastIndex = this.#index;
|
|
249
|
+
const match = NUMBER.exec(this.#text);
|
|
250
|
+
if (match === null) return this.#fail("Expected a value.");
|
|
251
|
+
this.#index = NUMBER.lastIndex;
|
|
252
|
+
return Number(match[0]);
|
|
253
|
+
}
|
|
254
|
+
#readString() {
|
|
255
|
+
this.#index += 1;
|
|
256
|
+
let value = "";
|
|
257
|
+
let chunkStart = this.#index;
|
|
258
|
+
for (;;) {
|
|
259
|
+
if (this.#index >= this.#text.length) return this.#fail("Unterminated string.");
|
|
260
|
+
const code = this.#text.charCodeAt(this.#index);
|
|
261
|
+
if (code === 34) {
|
|
262
|
+
value += this.#text.slice(chunkStart, this.#index);
|
|
263
|
+
this.#index += 1;
|
|
264
|
+
return value;
|
|
265
|
+
}
|
|
266
|
+
if (code === 92) {
|
|
267
|
+
value += this.#text.slice(chunkStart, this.#index);
|
|
268
|
+
this.#index += 1;
|
|
269
|
+
const escape = this.#readEscape();
|
|
270
|
+
if (escape === void 0) return;
|
|
271
|
+
value += escape;
|
|
272
|
+
chunkStart = this.#index;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (code < 32) return this.#fail("Control characters must be escaped inside strings.");
|
|
276
|
+
this.#index += 1;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
#readEscape() {
|
|
280
|
+
if (this.#index >= this.#text.length) return this.#fail("Unterminated escape sequence.");
|
|
281
|
+
const character = this.#text[this.#index];
|
|
282
|
+
this.#index += 1;
|
|
283
|
+
switch (character) {
|
|
284
|
+
case "\"": return "\"";
|
|
285
|
+
case "\\": return "\\";
|
|
286
|
+
case "/": return "/";
|
|
287
|
+
case "b": return "\b";
|
|
288
|
+
case "f": return "\f";
|
|
289
|
+
case "n": return "\n";
|
|
290
|
+
case "r": return "\r";
|
|
291
|
+
case "t": return " ";
|
|
292
|
+
case "u": return this.#readUnicodeEscape();
|
|
293
|
+
default: return this.#fail(`Unknown escape sequence "\\${String(character)}".`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
#readUnicodeEscape() {
|
|
297
|
+
const digits = this.#text.slice(this.#index, this.#index + 4);
|
|
298
|
+
if (!/^[0-9a-fA-F]{4}$/.test(digits)) return this.#fail("Unicode escapes need exactly four hexadecimal digits.");
|
|
299
|
+
this.#index += 4;
|
|
300
|
+
return String.fromCharCode(Number.parseInt(digits, 16));
|
|
301
|
+
}
|
|
302
|
+
#readObject(path, depth) {
|
|
303
|
+
this.#index += 1;
|
|
304
|
+
const object = {};
|
|
305
|
+
const keys = /* @__PURE__ */ new Set();
|
|
306
|
+
this.#skipWhitespace();
|
|
307
|
+
if (this.#text[this.#index] === "}") {
|
|
308
|
+
this.#index += 1;
|
|
309
|
+
return object;
|
|
310
|
+
}
|
|
311
|
+
for (;;) {
|
|
312
|
+
this.#skipWhitespace();
|
|
313
|
+
if (this.#text[this.#index] !== "\"") return this.#fail("Expected a property name.");
|
|
314
|
+
const key = this.#readString();
|
|
315
|
+
if (key === void 0) return;
|
|
316
|
+
this.#skipWhitespace();
|
|
317
|
+
if (this.#text[this.#index] !== ":") return this.#fail("Expected \":\" after the property name.");
|
|
318
|
+
this.#index += 1;
|
|
319
|
+
const memberPath = [...path, key];
|
|
320
|
+
const duplicate = keys.has(key);
|
|
321
|
+
if (duplicate) report(this.#diagnostics, "json/duplicate-key", `Duplicate JSON key ${JSON.stringify(key)}; the contract does not allow one key twice.`, memberPath);
|
|
322
|
+
else keys.add(key);
|
|
323
|
+
const value = this.#readValue(memberPath, depth);
|
|
324
|
+
if (this.#failed) return;
|
|
325
|
+
if (!duplicate) Object.defineProperty(object, key, {
|
|
326
|
+
value,
|
|
327
|
+
enumerable: true,
|
|
328
|
+
writable: true,
|
|
329
|
+
configurable: true
|
|
330
|
+
});
|
|
331
|
+
this.#skipWhitespace();
|
|
332
|
+
const next = this.#text[this.#index];
|
|
333
|
+
if (next === ",") {
|
|
334
|
+
this.#index += 1;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (next === "}") {
|
|
338
|
+
this.#index += 1;
|
|
339
|
+
return object;
|
|
340
|
+
}
|
|
341
|
+
return this.#fail("Expected \",\" or \"}\" in the object.");
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
#readArray(path, depth) {
|
|
345
|
+
this.#index += 1;
|
|
346
|
+
const array = [];
|
|
347
|
+
this.#skipWhitespace();
|
|
348
|
+
if (this.#text[this.#index] === "]") {
|
|
349
|
+
this.#index += 1;
|
|
350
|
+
return array;
|
|
351
|
+
}
|
|
352
|
+
for (;;) {
|
|
353
|
+
const item = this.#readValue([...path, String(array.length)], depth);
|
|
354
|
+
if (this.#failed) return;
|
|
355
|
+
array.push(item);
|
|
356
|
+
this.#skipWhitespace();
|
|
357
|
+
const next = this.#text[this.#index];
|
|
358
|
+
if (next === ",") {
|
|
359
|
+
this.#index += 1;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (next === "]") {
|
|
363
|
+
this.#index += 1;
|
|
364
|
+
return array;
|
|
365
|
+
}
|
|
366
|
+
return this.#fail("Expected \",\" or \"]\" in the array.");
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
var NUMBER = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
|
|
371
|
+
//#endregion
|
|
372
|
+
//#region src/fused/read.ts
|
|
373
|
+
/** Structural readers shared by the fused parsers; every failure reports code and field path. */
|
|
374
|
+
function isRecord(value) {
|
|
375
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
376
|
+
}
|
|
377
|
+
function hasOwn(value, key) {
|
|
378
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
379
|
+
}
|
|
380
|
+
function hasErrors(diagnostics) {
|
|
381
|
+
return diagnostics.some((diagnostic) => diagnostic.severity === "error");
|
|
382
|
+
}
|
|
383
|
+
function forbidUnknownFields(value, allowed, path, diagnostics) {
|
|
384
|
+
for (const key of Object.keys(value)) if (!allowed.includes(key)) report(diagnostics, "schema/unknown-field", `Unknown field "${key}".`, [...path, key]);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Strings are decoded as strict JSON text, which rejects duplicate keys; any other value is
|
|
388
|
+
* validated as-is so callers that already hold a plain object stay supported.
|
|
389
|
+
*/
|
|
390
|
+
function parseJsonInput(input, diagnostics) {
|
|
391
|
+
if (typeof input !== "string") return {
|
|
392
|
+
ok: true,
|
|
393
|
+
value: input
|
|
394
|
+
};
|
|
395
|
+
const decoded = decodeStrictJson(input);
|
|
396
|
+
if (!decoded.ok) {
|
|
397
|
+
diagnostics.push(...decoded.diagnostics);
|
|
398
|
+
return {
|
|
399
|
+
ok: false,
|
|
400
|
+
value: void 0
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
return {
|
|
404
|
+
ok: true,
|
|
405
|
+
value: decoded.value
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
function readStringField(value, key, path, diagnostics, required = true, options = {}) {
|
|
409
|
+
const fieldPath = [...path, key];
|
|
410
|
+
if (!hasOwn(value, key)) {
|
|
411
|
+
if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
const field = value[key];
|
|
415
|
+
if (typeof field !== "string") {
|
|
416
|
+
report(diagnostics, "schema/invalid-type", `Field "${key}" must be a string.`, fieldPath);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (options.nonEmpty === true && field.length === 0) {
|
|
420
|
+
report(diagnostics, "schema/invalid-value", `Field "${key}" must not be empty.`, fieldPath);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
return field;
|
|
424
|
+
}
|
|
425
|
+
function readNumberField(value, key, path, diagnostics, required = true) {
|
|
426
|
+
const fieldPath = [...path, key];
|
|
427
|
+
if (!hasOwn(value, key)) {
|
|
428
|
+
if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const field = value[key];
|
|
432
|
+
if (typeof field !== "number") {
|
|
433
|
+
report(diagnostics, "schema/invalid-type", `Field "${key}" must be a number.`, fieldPath);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (!Number.isFinite(field)) {
|
|
437
|
+
report(diagnostics, "schema/invalid-value", `Field "${key}" must be a finite number.`, fieldPath);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
return field;
|
|
441
|
+
}
|
|
442
|
+
function readIntegerField(value, key, path, diagnostics, required = true) {
|
|
443
|
+
const field = readNumberField(value, key, path, diagnostics, required);
|
|
444
|
+
if (field === void 0) return;
|
|
445
|
+
if (!Number.isSafeInteger(field)) {
|
|
446
|
+
report(diagnostics, "schema/invalid-value", `Field "${key}" must be a safe integer.`, [...path, key]);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
return field;
|
|
450
|
+
}
|
|
451
|
+
function readBooleanField(value, key, path, diagnostics, required = true) {
|
|
452
|
+
const fieldPath = [...path, key];
|
|
453
|
+
if (!hasOwn(value, key)) {
|
|
454
|
+
if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
const field = value[key];
|
|
458
|
+
if (typeof field !== "boolean") {
|
|
459
|
+
report(diagnostics, "schema/invalid-type", `Field "${key}" must be a boolean.`, fieldPath);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
return field;
|
|
463
|
+
}
|
|
464
|
+
function readObjectField(value, key, path, diagnostics, required = true) {
|
|
465
|
+
const fieldPath = [...path, key];
|
|
466
|
+
if (!hasOwn(value, key)) {
|
|
467
|
+
if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const field = value[key];
|
|
471
|
+
if (!isRecord(field)) {
|
|
472
|
+
report(diagnostics, "schema/invalid-type", `Field "${key}" must be an object.`, fieldPath);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
return field;
|
|
476
|
+
}
|
|
477
|
+
function readArrayField(value, key, path, diagnostics, required = true) {
|
|
478
|
+
const fieldPath = [...path, key];
|
|
479
|
+
if (!hasOwn(value, key)) {
|
|
480
|
+
if (required) report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const field = value[key];
|
|
484
|
+
if (!Array.isArray(field)) {
|
|
485
|
+
report(diagnostics, "schema/invalid-type", `Field "${key}" must be an array.`, fieldPath);
|
|
486
|
+
return;
|
|
487
|
+
}
|
|
488
|
+
return field;
|
|
489
|
+
}
|
|
490
|
+
/** Compares two strings by UTF-16 code unit, independent of the host locale. */
|
|
491
|
+
function compareStrings(a, b) {
|
|
492
|
+
if (a === b) return 0;
|
|
493
|
+
return a < b ? -1 : 1;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Records an immutable snapshot of a parsed structure.
|
|
497
|
+
*
|
|
498
|
+
* Parsers only build plain objects and arrays, so freezing them is safe; array-buffer views are
|
|
499
|
+
* left alone because their contents cannot be frozen.
|
|
500
|
+
*/
|
|
501
|
+
function deepFreeze(value) {
|
|
502
|
+
if (typeof value !== "object" || value === null || ArrayBuffer.isView(value)) return value;
|
|
503
|
+
if (Object.isFrozen(value)) return value;
|
|
504
|
+
Object.freeze(value);
|
|
505
|
+
for (const entry of Object.values(value)) deepFreeze(entry);
|
|
506
|
+
return value;
|
|
507
|
+
}
|
|
508
|
+
//#endregion
|
|
509
|
+
//#region src/fused/geometry.ts
|
|
510
|
+
/** Reading and validation of device 2D geometry (RFC 0022). */
|
|
511
|
+
/**
|
|
512
|
+
* Parses a `Geometry2D` declaration (RFC 0022).
|
|
513
|
+
*
|
|
514
|
+
* `pins` is the authoritative pin id set; when given, the geometry pin keys must match it exactly.
|
|
515
|
+
*
|
|
516
|
+
* @throws {FusedDocumentError} with the diagnostic code and JSON Pointer of every violation.
|
|
517
|
+
*/
|
|
518
|
+
function parseGeometry2D(value, pins) {
|
|
519
|
+
const diagnostics = [];
|
|
520
|
+
const parsed = parseJsonInput(value, diagnostics);
|
|
521
|
+
const geometry = parsed.ok ? readGeometry2D(parsed.value, [], diagnostics, pins) : void 0;
|
|
522
|
+
if (geometry === void 0 || hasErrors(diagnostics)) throw new FusedDocumentError(diagnostics);
|
|
523
|
+
return deepFreeze(geometry);
|
|
524
|
+
}
|
|
525
|
+
/** Reads one geometry value at `path`; `undefined` when it does not match the contract. */
|
|
526
|
+
function readGeometry2D(value, path, diagnostics, pins) {
|
|
527
|
+
if (!isRecord(value)) {
|
|
528
|
+
report(diagnostics, "schema/invalid-type", "Geometry must be an object.", path);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
forbidUnknownFields(value, [
|
|
532
|
+
"sizing",
|
|
533
|
+
"bounds",
|
|
534
|
+
"bodyBounds",
|
|
535
|
+
"bodyImage",
|
|
536
|
+
"pins",
|
|
537
|
+
"designator"
|
|
538
|
+
], path, diagnostics);
|
|
539
|
+
const sizing = readSizing(value, path, diagnostics);
|
|
540
|
+
const bounds = readRect2D(readObjectField(value, "bounds", path, diagnostics), [...path, "bounds"], diagnostics);
|
|
541
|
+
const bodyBounds = readRect2D(readObjectField(value, "bodyBounds", path, diagnostics), [...path, "bodyBounds"], diagnostics);
|
|
542
|
+
const bodyImage = readBodyImage(value, path, diagnostics);
|
|
543
|
+
const pinGeometry = readPins$1(value, path, diagnostics);
|
|
544
|
+
const designator = hasOwn(value, "designator") ? readPoint2D(value["designator"], [...path, "designator"], diagnostics) : void 0;
|
|
545
|
+
if (bounds !== void 0 && bodyBounds !== void 0 && !rectInside(bodyBounds, bounds)) report(diagnostics, "view2d/invalid-geometry", "The body bounds must lie inside the bounds.", [...path, "bodyBounds"]);
|
|
546
|
+
if (pinGeometry !== void 0 && pins !== void 0) checkPinIds(pinGeometry, pins, path, diagnostics);
|
|
547
|
+
if (sizing === void 0 || bounds === void 0 || bodyBounds === void 0 || pinGeometry === void 0 || hasOwn(value, "designator") && designator === void 0) return;
|
|
548
|
+
return {
|
|
549
|
+
sizing,
|
|
550
|
+
bounds,
|
|
551
|
+
bodyBounds,
|
|
552
|
+
...bodyImage === void 0 ? {} : { bodyImage },
|
|
553
|
+
pins: pinGeometry,
|
|
554
|
+
...designator === void 0 ? {} : { designator }
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
function readSizing(value, path, diagnostics) {
|
|
558
|
+
const sizing = readStringField(value, "sizing", path, diagnostics, true, { nonEmpty: true });
|
|
559
|
+
if (sizing === void 0) return;
|
|
560
|
+
if (sizing !== "physical" && sizing !== "flexible") {
|
|
561
|
+
report(diagnostics, "view2d/invalid-geometry", "Field \"sizing\" must be \"physical\" or \"flexible\".", [...path, "sizing"]);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
return sizing;
|
|
565
|
+
}
|
|
566
|
+
function readBodyImage(value, path, diagnostics) {
|
|
567
|
+
const bodyImage = readStringField(value, "bodyImage", path, diagnostics, false, { nonEmpty: true });
|
|
568
|
+
if (bodyImage === void 0) return;
|
|
569
|
+
const pathProblem = packagePathProblem(bodyImage);
|
|
570
|
+
if (pathProblem !== void 0) {
|
|
571
|
+
report(diagnostics, "view2d/invalid-geometry", `Field "bodyImage" ${pathProblem}.`, [...path, "bodyImage"]);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const extensionIssue = extensionProblem(bodyImage, [".png", ".svg"]);
|
|
575
|
+
if (extensionIssue !== void 0) {
|
|
576
|
+
report(diagnostics, "view2d/invalid-geometry", `Field "bodyImage" ${extensionIssue}.`, [...path, "bodyImage"]);
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
return bodyImage;
|
|
580
|
+
}
|
|
581
|
+
function readPins$1(value, path, diagnostics) {
|
|
582
|
+
const rawPins = readObjectField(value, "pins", path, diagnostics);
|
|
583
|
+
if (rawPins === void 0) return;
|
|
584
|
+
const pins = Object.create(null);
|
|
585
|
+
let failed = false;
|
|
586
|
+
for (const [pinId, raw] of Object.entries(rawPins)) {
|
|
587
|
+
const pinPath = [
|
|
588
|
+
...path,
|
|
589
|
+
"pins",
|
|
590
|
+
pinId
|
|
591
|
+
];
|
|
592
|
+
if (!isPinId(pinId)) {
|
|
593
|
+
report(diagnostics, "view2d/invalid-geometry", `Pin id ${JSON.stringify(pinId)} is not a legal model pin name.`, pinPath);
|
|
594
|
+
failed = true;
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
const pin = readPinGeometry(raw, pinPath, diagnostics);
|
|
598
|
+
if (pin === void 0) {
|
|
599
|
+
failed = true;
|
|
600
|
+
continue;
|
|
601
|
+
}
|
|
602
|
+
pins[pinId] = pin;
|
|
603
|
+
}
|
|
604
|
+
return failed ? void 0 : pins;
|
|
605
|
+
}
|
|
606
|
+
function readPinGeometry(value, path, diagnostics) {
|
|
607
|
+
if (!isRecord(value)) {
|
|
608
|
+
report(diagnostics, "schema/invalid-type", "Pin geometry must be an object.", path);
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
forbidUnknownFields(value, [
|
|
612
|
+
"position",
|
|
613
|
+
"outward",
|
|
614
|
+
"label"
|
|
615
|
+
], path, diagnostics);
|
|
616
|
+
const position = readPoint2D(readObjectField(value, "position", path, diagnostics), [...path, "position"], diagnostics);
|
|
617
|
+
const outward = readPoint2D(readObjectField(value, "outward", path, diagnostics), [...path, "outward"], diagnostics);
|
|
618
|
+
if (outward !== void 0 && outward.x === 0 && outward.y === 0) {
|
|
619
|
+
report(diagnostics, "view2d/invalid-geometry", "Field \"outward\" must be a non-zero vector.", [...path, "outward"]);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
let label;
|
|
623
|
+
if (hasOwn(value, "label")) {
|
|
624
|
+
const rawLabel = value["label"];
|
|
625
|
+
const labelPath = [...path, "label"];
|
|
626
|
+
if (!isRecord(rawLabel)) {
|
|
627
|
+
report(diagnostics, "schema/invalid-type", "Field \"label\" must be an object.", labelPath);
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
forbidUnknownFields(rawLabel, ["position", "visible"], labelPath, diagnostics);
|
|
631
|
+
const labelPosition = readPoint2D(readObjectField(rawLabel, "position", labelPath, diagnostics), [...labelPath, "position"], diagnostics);
|
|
632
|
+
const visible = readBooleanField(rawLabel, "visible", labelPath, diagnostics, false);
|
|
633
|
+
if (labelPosition === void 0) return;
|
|
634
|
+
label = {
|
|
635
|
+
position: labelPosition,
|
|
636
|
+
...visible === void 0 ? {} : { visible }
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
if (position === void 0 || outward === void 0) return;
|
|
640
|
+
return {
|
|
641
|
+
position,
|
|
642
|
+
outward,
|
|
643
|
+
...label === void 0 ? {} : { label }
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
function readRect2D(value, path, diagnostics) {
|
|
647
|
+
if (value === void 0) return;
|
|
648
|
+
forbidUnknownFields(value, [
|
|
649
|
+
"x",
|
|
650
|
+
"y",
|
|
651
|
+
"width",
|
|
652
|
+
"height"
|
|
653
|
+
], path, diagnostics);
|
|
654
|
+
const x = readNumberField(value, "x", path, diagnostics);
|
|
655
|
+
const y = readNumberField(value, "y", path, diagnostics);
|
|
656
|
+
const width = readNumberField(value, "width", path, diagnostics);
|
|
657
|
+
const height = readNumberField(value, "height", path, diagnostics);
|
|
658
|
+
if (width !== void 0 && width <= 0) {
|
|
659
|
+
report(diagnostics, "schema/invalid-value", "Field \"width\" must be positive.", [...path, "width"]);
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
if (height !== void 0 && height <= 0) {
|
|
663
|
+
report(diagnostics, "schema/invalid-value", "Field \"height\" must be positive.", [...path, "height"]);
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
if (x === void 0 || y === void 0 || width === void 0 || height === void 0) return;
|
|
667
|
+
return {
|
|
668
|
+
x,
|
|
669
|
+
y,
|
|
670
|
+
width,
|
|
671
|
+
height
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
function readPoint2D(value, path, diagnostics) {
|
|
675
|
+
if (!isRecord(value)) {
|
|
676
|
+
report(diagnostics, "schema/invalid-type", "Point must be an object.", path);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
forbidUnknownFields(value, ["x", "y"], path, diagnostics);
|
|
680
|
+
const x = readNumberField(value, "x", path, diagnostics);
|
|
681
|
+
const y = readNumberField(value, "y", path, diagnostics);
|
|
682
|
+
return x === void 0 || y === void 0 ? void 0 : {
|
|
683
|
+
x,
|
|
684
|
+
y
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
function rectInside(inner, outer) {
|
|
688
|
+
return inner.x >= outer.x && inner.y >= outer.y && inner.x + inner.width <= outer.x + outer.width && inner.y + inner.height <= outer.y + outer.height;
|
|
689
|
+
}
|
|
690
|
+
function checkPinIds(geometry, declared, path, diagnostics) {
|
|
691
|
+
const geometryIds = Object.keys(geometry).sort(compareStrings);
|
|
692
|
+
const declaredIds = [...declared].sort(compareStrings);
|
|
693
|
+
const missing = declaredIds.filter((pin) => !geometryIds.includes(pin));
|
|
694
|
+
const extra = geometryIds.filter((pin) => !declaredIds.includes(pin));
|
|
695
|
+
if (missing.length === 0 && extra.length === 0) return;
|
|
696
|
+
report(diagnostics, "view2d/pin-mismatch", `Geometry pins must match the device pins: ${[missing.length === 0 ? void 0 : `missing ${missing.map(quote).join(", ")}`, extra.length === 0 ? void 0 : `undeclared ${extra.map(quote).join(", ")}`].filter((entry) => entry !== void 0).join("; ")}.`, [...path, "pins"]);
|
|
697
|
+
}
|
|
698
|
+
function quote(value) {
|
|
699
|
+
return JSON.stringify(value);
|
|
700
|
+
}
|
|
701
|
+
//#endregion
|
|
702
|
+
//#region src/fused/manifest-properties.ts
|
|
703
|
+
var PROPERTY_TYPES = [
|
|
704
|
+
"string",
|
|
705
|
+
"integer",
|
|
706
|
+
"number",
|
|
707
|
+
"boolean",
|
|
708
|
+
"color",
|
|
709
|
+
"enum",
|
|
710
|
+
"image",
|
|
711
|
+
"binary",
|
|
712
|
+
"firmware",
|
|
713
|
+
"program"
|
|
714
|
+
];
|
|
715
|
+
var COMMON_FIELDS = [
|
|
716
|
+
"type",
|
|
717
|
+
"title",
|
|
718
|
+
"description",
|
|
719
|
+
"default",
|
|
720
|
+
"nullable",
|
|
721
|
+
"group",
|
|
722
|
+
"order"
|
|
723
|
+
];
|
|
724
|
+
var TYPE_FIELDS = {
|
|
725
|
+
string: [...COMMON_FIELDS, "maxLength"],
|
|
726
|
+
integer: [
|
|
727
|
+
...COMMON_FIELDS,
|
|
728
|
+
"min",
|
|
729
|
+
"max",
|
|
730
|
+
"step"
|
|
731
|
+
],
|
|
732
|
+
number: [
|
|
733
|
+
...COMMON_FIELDS,
|
|
734
|
+
"min",
|
|
735
|
+
"max",
|
|
736
|
+
"exclusiveMin",
|
|
737
|
+
"step",
|
|
738
|
+
"unit"
|
|
739
|
+
],
|
|
740
|
+
boolean: COMMON_FIELDS,
|
|
741
|
+
color: [...COMMON_FIELDS, "alpha"],
|
|
742
|
+
enum: [...COMMON_FIELDS, "options"],
|
|
743
|
+
image: [
|
|
744
|
+
...COMMON_FIELDS,
|
|
745
|
+
"maxBytes",
|
|
746
|
+
"maxPixels"
|
|
747
|
+
],
|
|
748
|
+
binary: [
|
|
749
|
+
...COMMON_FIELDS,
|
|
750
|
+
"maxBytes",
|
|
751
|
+
"extensions"
|
|
752
|
+
],
|
|
753
|
+
firmware: [
|
|
754
|
+
...COMMON_FIELDS,
|
|
755
|
+
"maxBytes",
|
|
756
|
+
"addressSpaces",
|
|
757
|
+
"addressSpace",
|
|
758
|
+
"loadAddress"
|
|
759
|
+
],
|
|
760
|
+
program: [
|
|
761
|
+
...COMMON_FIELDS,
|
|
762
|
+
"loader",
|
|
763
|
+
"formats",
|
|
764
|
+
"addressSpaces",
|
|
765
|
+
"defaults",
|
|
766
|
+
"templates"
|
|
767
|
+
]
|
|
768
|
+
};
|
|
769
|
+
function isDevicePropertyType(value) {
|
|
770
|
+
return PROPERTY_TYPES.includes(value);
|
|
771
|
+
}
|
|
772
|
+
/** Reads the `properties` map of a manifest. */
|
|
773
|
+
function readPropertyDescriptors(value, path, diagnostics) {
|
|
774
|
+
if (!isRecord(value)) {
|
|
775
|
+
report(diagnostics, "schema/invalid-type", "Field \"properties\" must be an object.", path);
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
const descriptors = Object.create(null);
|
|
779
|
+
let failed = false;
|
|
780
|
+
for (const [key, raw] of Object.entries(value)) {
|
|
781
|
+
const descriptorPath = [...path, key];
|
|
782
|
+
if (!isMemberKey(key)) {
|
|
783
|
+
report(diagnostics, "package/invalid-manifest", `Property key ${JSON.stringify(key)} must match [A-Za-z][A-Za-z0-9]*.`, descriptorPath);
|
|
784
|
+
failed = true;
|
|
785
|
+
continue;
|
|
786
|
+
}
|
|
787
|
+
const descriptor = readPropertyDescriptor(raw, descriptorPath, diagnostics);
|
|
788
|
+
if (descriptor === void 0) {
|
|
789
|
+
failed = true;
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
descriptors[key] = descriptor;
|
|
793
|
+
}
|
|
794
|
+
return failed ? void 0 : descriptors;
|
|
795
|
+
}
|
|
796
|
+
function readPropertyDescriptor(value, path, diagnostics) {
|
|
797
|
+
if (!isRecord(value)) {
|
|
798
|
+
report(diagnostics, "schema/invalid-type", "Property description must be an object.", path);
|
|
799
|
+
return;
|
|
800
|
+
}
|
|
801
|
+
const type = readStringField(value, "type", path, diagnostics, true, { nonEmpty: true });
|
|
802
|
+
if (type === void 0) return;
|
|
803
|
+
if (!isDevicePropertyType(type)) {
|
|
804
|
+
report(diagnostics, "package/invalid-manifest", `Unknown property type ${JSON.stringify(type)}.`, [...path, "type"]);
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
forbidUnknownFields(value, TYPE_FIELDS[type], path, diagnostics);
|
|
808
|
+
const draft = { type };
|
|
809
|
+
let failed = false;
|
|
810
|
+
const title = readStringField(value, "title", path, diagnostics, false, { nonEmpty: true });
|
|
811
|
+
if (title !== void 0) draft.title = title;
|
|
812
|
+
const description = readStringField(value, "description", path, diagnostics, false, { nonEmpty: true });
|
|
813
|
+
if (description !== void 0) draft.description = description;
|
|
814
|
+
const group = readStringField(value, "group", path, diagnostics, false, { nonEmpty: true });
|
|
815
|
+
if (group !== void 0) draft.group = group;
|
|
816
|
+
if (readBooleanField(value, "nullable", path, diagnostics, false) === true) draft.nullable = true;
|
|
817
|
+
if (hasOwn(value, "order")) {
|
|
818
|
+
const order = readIntegerField(value, "order", path, diagnostics);
|
|
819
|
+
if (order === void 0) failed = true;
|
|
820
|
+
else if (order < 0) {
|
|
821
|
+
report(diagnostics, "package/invalid-manifest", "Field \"order\" must be a non-negative integer.", [...path, "order"]);
|
|
822
|
+
failed = true;
|
|
823
|
+
} else draft.order = order;
|
|
824
|
+
}
|
|
825
|
+
switch (type) {
|
|
826
|
+
case "program":
|
|
827
|
+
if (!readProgramFields(value, path, diagnostics, draft)) failed = true;
|
|
828
|
+
break;
|
|
829
|
+
case "string": {
|
|
830
|
+
const maxLength = readCount(value, "maxLength", path, diagnostics);
|
|
831
|
+
if (maxLength === "invalid") failed = true;
|
|
832
|
+
else if (maxLength !== void 0) draft.maxLength = maxLength;
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
case "integer":
|
|
836
|
+
case "number":
|
|
837
|
+
if (!readBounds(value, type, path, diagnostics, draft)) failed = true;
|
|
838
|
+
if (type === "number") {
|
|
839
|
+
const unit = readStringField(value, "unit", path, diagnostics, false, { nonEmpty: true });
|
|
840
|
+
if (unit !== void 0) draft.unit = unit;
|
|
841
|
+
}
|
|
842
|
+
break;
|
|
843
|
+
case "color":
|
|
844
|
+
if (readBooleanField(value, "alpha", path, diagnostics, false) === true) draft.alpha = true;
|
|
845
|
+
break;
|
|
846
|
+
case "enum": {
|
|
847
|
+
const options = readOptions(value, path, diagnostics);
|
|
848
|
+
if (options === void 0) failed = true;
|
|
849
|
+
else draft.options = options;
|
|
850
|
+
break;
|
|
851
|
+
}
|
|
852
|
+
case "image":
|
|
853
|
+
case "binary":
|
|
854
|
+
case "firmware": {
|
|
855
|
+
const maxBytes = readCount(value, "maxBytes", path, diagnostics);
|
|
856
|
+
if (maxBytes === "invalid") failed = true;
|
|
857
|
+
else if (maxBytes !== void 0) draft.maxBytes = maxBytes;
|
|
858
|
+
if (type === "image") {
|
|
859
|
+
const maxPixels = readCount(value, "maxPixels", path, diagnostics);
|
|
860
|
+
if (maxPixels === "invalid") failed = true;
|
|
861
|
+
else if (maxPixels !== void 0) draft.maxPixels = maxPixels;
|
|
862
|
+
}
|
|
863
|
+
if (type === "binary") {
|
|
864
|
+
const extensions = readExtensions(value, path, diagnostics);
|
|
865
|
+
if (extensions === "invalid") failed = true;
|
|
866
|
+
else if (extensions !== void 0) draft.extensions = extensions;
|
|
867
|
+
}
|
|
868
|
+
if (type === "firmware" && !readFirmwareFields(value, path, diagnostics, draft)) failed = true;
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
if (hasOwn(value, "default")) {
|
|
873
|
+
const defaultValue = checkDefault(value["default"], draft, [...path, "default"], diagnostics);
|
|
874
|
+
if (defaultValue !== void 0) draft.default = defaultValue.value;
|
|
875
|
+
else failed = true;
|
|
876
|
+
}
|
|
877
|
+
return failed ? void 0 : draft;
|
|
878
|
+
}
|
|
879
|
+
function readCount(value, key, path, diagnostics) {
|
|
880
|
+
if (!hasOwn(value, key)) return;
|
|
881
|
+
const count = readIntegerField(value, key, path, diagnostics);
|
|
882
|
+
if (count === void 0) return "invalid";
|
|
883
|
+
if (count < 0) {
|
|
884
|
+
report(diagnostics, "package/invalid-manifest", `Field "${key}" must be a non-negative integer.`, [...path, key]);
|
|
885
|
+
return "invalid";
|
|
886
|
+
}
|
|
887
|
+
return count;
|
|
888
|
+
}
|
|
889
|
+
function readBounds(value, type, path, diagnostics, draft) {
|
|
890
|
+
let ok = true;
|
|
891
|
+
const min = readNumberField(value, "min", path, diagnostics, false);
|
|
892
|
+
const max = readNumberField(value, "max", path, diagnostics, false);
|
|
893
|
+
if (type === "integer") {
|
|
894
|
+
if (hasOwn(value, "min") && (min === void 0 || !Number.isSafeInteger(min))) {
|
|
895
|
+
report(diagnostics, "package/invalid-manifest", "Field \"min\" must be a safe integer for an integer property.", [...path, "min"]);
|
|
896
|
+
ok = false;
|
|
897
|
+
} else if (min !== void 0) draft.min = min;
|
|
898
|
+
if (hasOwn(value, "max") && (max === void 0 || !Number.isSafeInteger(max))) {
|
|
899
|
+
report(diagnostics, "package/invalid-manifest", "Field \"max\" must be a safe integer for an integer property.", [...path, "max"]);
|
|
900
|
+
ok = false;
|
|
901
|
+
} else if (max !== void 0) draft.max = max;
|
|
902
|
+
} else {
|
|
903
|
+
if (min !== void 0) draft.min = min;
|
|
904
|
+
if (max !== void 0) draft.max = max;
|
|
905
|
+
const exclusiveMin = readNumberField(value, "exclusiveMin", path, diagnostics, false);
|
|
906
|
+
if (exclusiveMin !== void 0) draft.exclusiveMin = exclusiveMin;
|
|
907
|
+
}
|
|
908
|
+
if (draft.min !== void 0 && draft.max !== void 0 && draft.min > draft.max) {
|
|
909
|
+
report(diagnostics, "package/invalid-manifest", "Field \"min\" must not be greater than \"max\".", [...path, "min"]);
|
|
910
|
+
ok = false;
|
|
911
|
+
}
|
|
912
|
+
if (hasOwn(value, "step")) {
|
|
913
|
+
const step = readNumberField(value, "step", path, diagnostics, false);
|
|
914
|
+
if (step === void 0 || step <= 0) {
|
|
915
|
+
report(diagnostics, "package/invalid-manifest", "Field \"step\" must be greater than zero.", [...path, "step"]);
|
|
916
|
+
ok = false;
|
|
917
|
+
} else if (type === "integer" && !Number.isSafeInteger(step)) {
|
|
918
|
+
report(diagnostics, "package/invalid-manifest", "Field \"step\" must be a safe integer for an integer property.", [...path, "step"]);
|
|
919
|
+
ok = false;
|
|
920
|
+
} else draft.step = step;
|
|
921
|
+
}
|
|
922
|
+
return ok;
|
|
923
|
+
}
|
|
924
|
+
function readOptions(value, path, diagnostics) {
|
|
925
|
+
const raw = readArrayField(value, "options", path, diagnostics);
|
|
926
|
+
if (raw === void 0) return;
|
|
927
|
+
if (raw.length === 0) {
|
|
928
|
+
report(diagnostics, "package/invalid-manifest", "Field \"options\" must contain at least one option.", [...path, "options"]);
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
const options = [];
|
|
932
|
+
const seen = /* @__PURE__ */ new Set();
|
|
933
|
+
let failed = false;
|
|
934
|
+
raw.forEach((entry, index) => {
|
|
935
|
+
const optionPath = [
|
|
936
|
+
...path,
|
|
937
|
+
"options",
|
|
938
|
+
String(index)
|
|
939
|
+
];
|
|
940
|
+
if (!isRecord(entry)) {
|
|
941
|
+
report(diagnostics, "schema/invalid-type", "Enum option must be an object.", optionPath);
|
|
942
|
+
failed = true;
|
|
943
|
+
return;
|
|
944
|
+
}
|
|
945
|
+
forbidUnknownFields(entry, ["value", "label"], optionPath, diagnostics);
|
|
946
|
+
const optionValue = readStringField(entry, "value", optionPath, diagnostics, true, { nonEmpty: true });
|
|
947
|
+
const label = readStringField(entry, "label", optionPath, diagnostics, true, { nonEmpty: true });
|
|
948
|
+
if (optionValue === void 0 || label === void 0) {
|
|
949
|
+
failed = true;
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
if (seen.has(optionValue)) {
|
|
953
|
+
report(diagnostics, "package/invalid-manifest", `Enum option value ${JSON.stringify(optionValue)} is declared more than once.`, [...optionPath, "value"]);
|
|
954
|
+
failed = true;
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
seen.add(optionValue);
|
|
958
|
+
options.push({
|
|
959
|
+
value: optionValue,
|
|
960
|
+
label
|
|
961
|
+
});
|
|
962
|
+
});
|
|
963
|
+
return failed ? void 0 : options;
|
|
964
|
+
}
|
|
965
|
+
function readExtensions(value, path, diagnostics) {
|
|
966
|
+
if (!hasOwn(value, "extensions")) return;
|
|
967
|
+
const raw = readArrayField(value, "extensions", path, diagnostics);
|
|
968
|
+
if (raw === void 0) return "invalid";
|
|
969
|
+
if (raw.length === 0) {
|
|
970
|
+
report(diagnostics, "package/invalid-manifest", "Field \"extensions\" must contain at least one extension.", [...path, "extensions"]);
|
|
971
|
+
return "invalid";
|
|
972
|
+
}
|
|
973
|
+
const extensions = [];
|
|
974
|
+
const seen = /* @__PURE__ */ new Set();
|
|
975
|
+
let failed = false;
|
|
976
|
+
raw.forEach((entry, index) => {
|
|
977
|
+
const entryPath = [
|
|
978
|
+
...path,
|
|
979
|
+
"extensions",
|
|
980
|
+
String(index)
|
|
981
|
+
];
|
|
982
|
+
if (typeof entry !== "string" || !isExtensionFilter(entry)) {
|
|
983
|
+
report(diagnostics, "package/invalid-manifest", "Extension filters must match \"\\.[A-Za-z0-9]+\".", entryPath);
|
|
984
|
+
failed = true;
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
const folded = entry.toLowerCase();
|
|
988
|
+
if (seen.has(folded)) {
|
|
989
|
+
report(diagnostics, "package/invalid-manifest", `Extension filter ${JSON.stringify(entry)} is declared more than once.`, entryPath);
|
|
990
|
+
failed = true;
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
seen.add(folded);
|
|
994
|
+
extensions.push(entry);
|
|
995
|
+
});
|
|
996
|
+
return failed ? "invalid" : extensions;
|
|
997
|
+
}
|
|
998
|
+
function readProgramFields(value, path, diagnostics, draft) {
|
|
999
|
+
const fail = (message, field) => {
|
|
1000
|
+
report(diagnostics, "package/invalid-manifest", message, [...path, field]);
|
|
1001
|
+
return false;
|
|
1002
|
+
};
|
|
1003
|
+
let ok = true;
|
|
1004
|
+
if (hasOwn(value, "templates")) {
|
|
1005
|
+
const templates = value["templates"];
|
|
1006
|
+
if (!Array.isArray(templates) || templates.some((item) => typeof item !== "string" || item.length === 0) || new Set(templates).size !== templates.length) ok = fail("Templates must be unique non-empty string IDs.", "templates");
|
|
1007
|
+
else draft.templates = templates;
|
|
1008
|
+
}
|
|
1009
|
+
const loader = value["loader"];
|
|
1010
|
+
if (loader !== "memory" && loader !== "filesystem") return fail("Program loader must be memory or filesystem.", "loader");
|
|
1011
|
+
draft.loader = loader;
|
|
1012
|
+
if (loader === "filesystem") {
|
|
1013
|
+
for (const field of [
|
|
1014
|
+
"formats",
|
|
1015
|
+
"addressSpaces",
|
|
1016
|
+
"defaults"
|
|
1017
|
+
]) if (hasOwn(value, field)) ok = fail(`Filesystem programs do not declare ${field}.`, field);
|
|
1018
|
+
return ok;
|
|
1019
|
+
}
|
|
1020
|
+
const formats = value["formats"];
|
|
1021
|
+
const isFormat = (item) => item === "bin" || item === "hex" || item === "elf";
|
|
1022
|
+
if (!Array.isArray(formats) || formats.length === 0 || !formats.every(isFormat) || new Set(formats).size !== formats.length) return fail("Memory formats must be a non-empty unique subset of bin, hex and elf.", "formats");
|
|
1023
|
+
draft.formats = formats;
|
|
1024
|
+
const rawSpaces = readObjectField(value, "addressSpaces", path, diagnostics);
|
|
1025
|
+
if (rawSpaces === void 0 || Object.keys(rawSpaces).length === 0) return fail("Memory programs require address spaces.", "addressSpaces");
|
|
1026
|
+
const spaces = Object.create(null);
|
|
1027
|
+
for (const [id, raw] of Object.entries(rawSpaces)) {
|
|
1028
|
+
if (id.length === 0) {
|
|
1029
|
+
ok = fail("Address space IDs must not be empty.", "addressSpaces");
|
|
1030
|
+
continue;
|
|
1031
|
+
}
|
|
1032
|
+
const space = readAddressSpace(raw, [
|
|
1033
|
+
...path,
|
|
1034
|
+
"addressSpaces",
|
|
1035
|
+
id
|
|
1036
|
+
], diagnostics);
|
|
1037
|
+
if (space === void 0) ok = false;
|
|
1038
|
+
else spaces[id] = space;
|
|
1039
|
+
}
|
|
1040
|
+
draft.addressSpaces = spaces;
|
|
1041
|
+
if (!formats.includes("bin") && !formats.includes("hex")) return hasOwn(value, "defaults") ? fail("ELF-only programs do not declare defaults.", "defaults") : ok;
|
|
1042
|
+
const defaults = readObjectField(value, "defaults", path, diagnostics);
|
|
1043
|
+
if (defaults === void 0) return false;
|
|
1044
|
+
const defaultsPath = [...path, "defaults"];
|
|
1045
|
+
forbidUnknownFields(defaults, ["addressSpace", "loadAddress"], defaultsPath, diagnostics);
|
|
1046
|
+
const addressSpace = readStringField(defaults, "addressSpace", defaultsPath, diagnostics, true, { nonEmpty: true });
|
|
1047
|
+
if (addressSpace === void 0 || spaces[addressSpace] === void 0) return fail("Default addressSpace must name a declared space.", "defaults");
|
|
1048
|
+
const loadAddress = readIntegerField(defaults, "loadAddress", defaultsPath, diagnostics, formats.includes("bin"));
|
|
1049
|
+
if (formats.includes("bin")) {
|
|
1050
|
+
const space = spaces[addressSpace];
|
|
1051
|
+
if (loadAddress === void 0 || loadAddress < space.start || loadAddress >= space.start + space.size) return fail("Default loadAddress must lie in its address space.", "defaults");
|
|
1052
|
+
} else if (hasOwn(defaults, "loadAddress")) return fail("Only BIN programs declare a default loadAddress.", "defaults");
|
|
1053
|
+
draft.defaults = {
|
|
1054
|
+
addressSpace,
|
|
1055
|
+
...loadAddress === void 0 ? {} : { loadAddress }
|
|
1056
|
+
};
|
|
1057
|
+
return ok;
|
|
1058
|
+
}
|
|
1059
|
+
function readFirmwareFields(value, path, diagnostics, draft) {
|
|
1060
|
+
let ok = true;
|
|
1061
|
+
const spacesPath = [...path, "addressSpaces"];
|
|
1062
|
+
const rawSpaces = readObjectField(value, "addressSpaces", path, diagnostics);
|
|
1063
|
+
let spaces;
|
|
1064
|
+
if (rawSpaces === void 0) ok = false;
|
|
1065
|
+
else if (Object.keys(rawSpaces).length === 0) {
|
|
1066
|
+
report(diagnostics, "package/invalid-manifest", "Field \"addressSpaces\" must declare at least one address space.", spacesPath);
|
|
1067
|
+
ok = false;
|
|
1068
|
+
} else {
|
|
1069
|
+
spaces = Object.create(null);
|
|
1070
|
+
for (const [id, raw] of Object.entries(rawSpaces)) {
|
|
1071
|
+
const spacePath = [...spacesPath, id];
|
|
1072
|
+
if (id.length === 0) {
|
|
1073
|
+
report(diagnostics, "package/invalid-manifest", "Address space ids must not be empty.", spacePath);
|
|
1074
|
+
ok = false;
|
|
1075
|
+
continue;
|
|
1076
|
+
}
|
|
1077
|
+
const space = readAddressSpace(raw, spacePath, diagnostics);
|
|
1078
|
+
if (space === void 0) {
|
|
1079
|
+
ok = false;
|
|
1080
|
+
continue;
|
|
1081
|
+
}
|
|
1082
|
+
spaces[id] = space;
|
|
1083
|
+
}
|
|
1084
|
+
if (ok) draft.addressSpaces = spaces;
|
|
1085
|
+
}
|
|
1086
|
+
const addressSpace = readStringField(value, "addressSpace", path, diagnostics, true, { nonEmpty: true });
|
|
1087
|
+
const loadAddress = readIntegerField(value, "loadAddress", path, diagnostics, true);
|
|
1088
|
+
if (loadAddress !== void 0 && loadAddress < 0) {
|
|
1089
|
+
report(diagnostics, "package/invalid-manifest", "Field \"loadAddress\" must be a non-negative safe integer.", [...path, "loadAddress"]);
|
|
1090
|
+
ok = false;
|
|
1091
|
+
}
|
|
1092
|
+
if (addressSpace === void 0 || spaces === void 0) return false;
|
|
1093
|
+
const space = spaces[addressSpace];
|
|
1094
|
+
if (space === void 0) {
|
|
1095
|
+
report(diagnostics, "package/invalid-manifest", `Field "addressSpace" must name a declared address space; "${addressSpace}" is unknown.`, [...path, "addressSpace"]);
|
|
1096
|
+
return false;
|
|
1097
|
+
}
|
|
1098
|
+
draft.addressSpace = addressSpace;
|
|
1099
|
+
if (loadAddress === void 0) return false;
|
|
1100
|
+
draft.loadAddress = loadAddress;
|
|
1101
|
+
if (loadAddress < space.start || loadAddress >= space.start + space.size) {
|
|
1102
|
+
report(diagnostics, "package/invalid-manifest", `Field "loadAddress" must lie inside the "${addressSpace}" range.`, [...path, "loadAddress"]);
|
|
1103
|
+
ok = false;
|
|
1104
|
+
}
|
|
1105
|
+
return ok;
|
|
1106
|
+
}
|
|
1107
|
+
function readAddressSpace(value, path, diagnostics) {
|
|
1108
|
+
if (!isRecord(value)) {
|
|
1109
|
+
report(diagnostics, "schema/invalid-type", "Address space must be an object.", path);
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
forbidUnknownFields(value, [
|
|
1113
|
+
"title",
|
|
1114
|
+
"start",
|
|
1115
|
+
"size"
|
|
1116
|
+
], path, diagnostics);
|
|
1117
|
+
const title = readStringField(value, "title", path, diagnostics, false, { nonEmpty: true });
|
|
1118
|
+
const start = readIntegerField(value, "start", path, diagnostics, true);
|
|
1119
|
+
const size = readIntegerField(value, "size", path, diagnostics, true);
|
|
1120
|
+
let failed = false;
|
|
1121
|
+
if (start !== void 0 && start < 0) {
|
|
1122
|
+
report(diagnostics, "package/invalid-manifest", "Field \"start\" must be a non-negative safe integer.", [...path, "start"]);
|
|
1123
|
+
failed = true;
|
|
1124
|
+
}
|
|
1125
|
+
if (size !== void 0 && size <= 0) {
|
|
1126
|
+
report(diagnostics, "package/invalid-manifest", "Field \"size\" must be a positive safe integer.", [...path, "size"]);
|
|
1127
|
+
failed = true;
|
|
1128
|
+
}
|
|
1129
|
+
if (start !== void 0 && size !== void 0 && !Number.isSafeInteger(start + size)) {
|
|
1130
|
+
report(diagnostics, "package/invalid-manifest", "The address space must end at a safe integer.", [...path, "size"]);
|
|
1131
|
+
failed = true;
|
|
1132
|
+
}
|
|
1133
|
+
if (failed || start === void 0 || size === void 0) return;
|
|
1134
|
+
return {
|
|
1135
|
+
...title === void 0 ? {} : { title },
|
|
1136
|
+
start,
|
|
1137
|
+
size
|
|
1138
|
+
};
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Validates a declared default against its own description (RFC 0020 rejects an unusable default
|
|
1142
|
+
* instead of dropping it).
|
|
1143
|
+
*/
|
|
1144
|
+
function checkDefault(value, descriptor, path, diagnostics) {
|
|
1145
|
+
const fail = (message) => {
|
|
1146
|
+
report(diagnostics, "package/invalid-manifest", message, path);
|
|
1147
|
+
};
|
|
1148
|
+
if (value === null) return descriptor.nullable === true ? { value: null } : fail("A default of null requires \"nullable\": true.");
|
|
1149
|
+
switch (descriptor.type) {
|
|
1150
|
+
case "string": {
|
|
1151
|
+
if (typeof value !== "string") return fail("Default must be a string.");
|
|
1152
|
+
const maxLength = descriptor.maxLength;
|
|
1153
|
+
if (maxLength !== void 0 && [...value].length > maxLength) return fail(`Default must be at most ${String(maxLength)} characters.`);
|
|
1154
|
+
return { value };
|
|
1155
|
+
}
|
|
1156
|
+
case "integer":
|
|
1157
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) return fail("Default must be a safe integer.");
|
|
1158
|
+
if (descriptor.min !== void 0 && value < descriptor.min) return fail(`Default must be at least ${String(descriptor.min)}.`);
|
|
1159
|
+
if (descriptor.max !== void 0 && value > descriptor.max) return fail(`Default must be at most ${String(descriptor.max)}.`);
|
|
1160
|
+
return { value };
|
|
1161
|
+
case "number":
|
|
1162
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return fail("Default must be a finite number.");
|
|
1163
|
+
if (descriptor.exclusiveMin !== void 0 && value <= descriptor.exclusiveMin) return fail(`Default must be greater than ${String(descriptor.exclusiveMin)}.`);
|
|
1164
|
+
if (descriptor.min !== void 0 && value < descriptor.min) return fail(`Default must be at least ${String(descriptor.min)}.`);
|
|
1165
|
+
if (descriptor.max !== void 0 && value > descriptor.max) return fail(`Default must be at most ${String(descriptor.max)}.`);
|
|
1166
|
+
return { value };
|
|
1167
|
+
case "boolean": return typeof value === "boolean" ? { value } : fail("Default must be a boolean.");
|
|
1168
|
+
case "color": {
|
|
1169
|
+
const color = typeof value === "string" ? normalizeColor(value) : void 0;
|
|
1170
|
+
if (color === void 0) return fail("Default must be a \"#RRGGBB\" or \"#RRGGBBAA\" color.");
|
|
1171
|
+
if (descriptor.alpha !== true && color.length === 9 && !color.endsWith("FF")) return fail("Default must use an FF alpha channel unless \"alpha\": true.");
|
|
1172
|
+
return { value: color };
|
|
1173
|
+
}
|
|
1174
|
+
case "enum": {
|
|
1175
|
+
const allowed = (descriptor.options ?? []).map((option) => option.value);
|
|
1176
|
+
return typeof value === "string" && allowed.includes(value) ? { value } : fail(`Default must be one of ${allowed.map((entry) => JSON.stringify(entry)).join(", ")}.`);
|
|
1177
|
+
}
|
|
1178
|
+
case "image":
|
|
1179
|
+
case "binary": {
|
|
1180
|
+
const reference = checkPackageReference(value, path, diagnostics);
|
|
1181
|
+
return reference === void 0 ? void 0 : { value: reference };
|
|
1182
|
+
}
|
|
1183
|
+
case "firmware":
|
|
1184
|
+
case "program": {
|
|
1185
|
+
const reference = checkPackageReference(value, path, diagnostics);
|
|
1186
|
+
if (reference === void 0) return;
|
|
1187
|
+
if (descriptor.type === "program") {
|
|
1188
|
+
const format = reference.src.split(".").at(-1)?.toLowerCase();
|
|
1189
|
+
const bin = format === "bin";
|
|
1190
|
+
const hex = format === "hex" || format === "ihx";
|
|
1191
|
+
const decodedFormat = bin ? "bin" : hex ? "hex" : format === "elf" || format === "axf" ? "elf" : void 0;
|
|
1192
|
+
if (descriptor.loader === "memory" && (decodedFormat === void 0 || !descriptor.formats?.includes(decodedFormat))) return fail("The default program file must use an accepted format.");
|
|
1193
|
+
if ((descriptor.loader === "filesystem" || !bin && !hex) && (reference.addressSpace !== void 0 || reference.loadAddress !== void 0)) return fail("This program default does not accept address overrides.");
|
|
1194
|
+
if (!bin && reference.loadAddress !== void 0) return fail("Only BIN defaults accept loadAddress.");
|
|
1195
|
+
}
|
|
1196
|
+
const spaceId = reference.addressSpace ?? (descriptor.type === "program" ? descriptor.defaults?.addressSpace : void 0);
|
|
1197
|
+
if (spaceId !== void 0) {
|
|
1198
|
+
const space = descriptor.addressSpaces?.[spaceId];
|
|
1199
|
+
if (space === void 0) return fail(`Default address space "${spaceId}" is not declared.`);
|
|
1200
|
+
const loadAddress = reference.loadAddress;
|
|
1201
|
+
if (loadAddress !== void 0 && (loadAddress < space.start || loadAddress >= space.start + space.size)) return fail(`Default load address must lie inside the "${spaceId}" range.`);
|
|
1202
|
+
}
|
|
1203
|
+
return { value: reference };
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
/** A package-mode resource reference, the only file form a manifest default may use. */
|
|
1208
|
+
function checkPackageReference(value, path, diagnostics) {
|
|
1209
|
+
if (!isRecord(value)) {
|
|
1210
|
+
report(diagnostics, "package/invalid-manifest", "Default must be a \"package\" resource reference.", path);
|
|
1211
|
+
return;
|
|
1212
|
+
}
|
|
1213
|
+
forbidUnknownFields(value, [
|
|
1214
|
+
"src",
|
|
1215
|
+
"mode",
|
|
1216
|
+
"addressSpace",
|
|
1217
|
+
"loadAddress"
|
|
1218
|
+
], path, diagnostics);
|
|
1219
|
+
const mode = readStringField(value, "mode", path, diagnostics, true, { nonEmpty: true });
|
|
1220
|
+
const src = readStringField(value, "src", path, diagnostics, true, { nonEmpty: true });
|
|
1221
|
+
if (mode !== "package") {
|
|
1222
|
+
report(diagnostics, "package/invalid-manifest", "A declared package default must use \"mode\": \"package\".", [...path, "mode"]);
|
|
1223
|
+
return;
|
|
1224
|
+
}
|
|
1225
|
+
if (src === void 0) return;
|
|
1226
|
+
const srcProblem = packagePathProblem(src);
|
|
1227
|
+
if (srcProblem !== void 0) {
|
|
1228
|
+
report(diagnostics, "package/invalid-manifest", `Default "src" ${srcProblem}.`, [...path, "src"]);
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
const addressSpace = readStringField(value, "addressSpace", path, diagnostics, false, { nonEmpty: true });
|
|
1232
|
+
const loadAddress = readIntegerField(value, "loadAddress", path, diagnostics, false);
|
|
1233
|
+
if (loadAddress !== void 0 && loadAddress < 0) {
|
|
1234
|
+
report(diagnostics, "package/invalid-manifest", "Default \"loadAddress\" must be a non-negative safe integer.", [...path, "loadAddress"]);
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
return {
|
|
1238
|
+
src,
|
|
1239
|
+
mode: "package",
|
|
1240
|
+
...addressSpace === void 0 ? {} : { addressSpace },
|
|
1241
|
+
...loadAddress === void 0 ? {} : { loadAddress }
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
//#endregion
|
|
1245
|
+
//#region src/fused/value-type.ts
|
|
1246
|
+
/**
|
|
1247
|
+
* Public payload types of a device model (RFC 0021): declaration reading, reference checking and
|
|
1248
|
+
* value validation.
|
|
1249
|
+
*/
|
|
1250
|
+
var SIMPLE_TYPES = [
|
|
1251
|
+
"null",
|
|
1252
|
+
"boolean",
|
|
1253
|
+
"number",
|
|
1254
|
+
"string",
|
|
1255
|
+
"binary"
|
|
1256
|
+
];
|
|
1257
|
+
/**
|
|
1258
|
+
* Reads the `types` map of a model declaration.
|
|
1259
|
+
*
|
|
1260
|
+
* Names are collected first so a `ref` can address a type declared later in the same map.
|
|
1261
|
+
*/
|
|
1262
|
+
function readTypeDeclarations(value, path, diagnostics) {
|
|
1263
|
+
const names = /* @__PURE__ */ new Set();
|
|
1264
|
+
for (const key of Object.keys(value)) {
|
|
1265
|
+
if (!isMemberKey(key)) {
|
|
1266
|
+
report(diagnostics, "model/invalid-interface", `Public type name ${JSON.stringify(key)} must match [A-Za-z][A-Za-z0-9]*.`, [...path, key]);
|
|
1267
|
+
continue;
|
|
1268
|
+
}
|
|
1269
|
+
names.add(key);
|
|
1270
|
+
}
|
|
1271
|
+
const types = Object.create(null);
|
|
1272
|
+
let failed = false;
|
|
1273
|
+
for (const [key, raw] of Object.entries(value)) {
|
|
1274
|
+
if (!names.has(key)) continue;
|
|
1275
|
+
const type = readValueType(raw, [...path, key], diagnostics, names);
|
|
1276
|
+
if (type === void 0) {
|
|
1277
|
+
failed = true;
|
|
1278
|
+
continue;
|
|
1279
|
+
}
|
|
1280
|
+
types[key] = type;
|
|
1281
|
+
}
|
|
1282
|
+
if (!failed) checkReferenceChains(types, path, diagnostics);
|
|
1283
|
+
return {
|
|
1284
|
+
types,
|
|
1285
|
+
names
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
/** Reads one value type declaration, rejecting unknown members and unknown `ref` targets. */
|
|
1289
|
+
function readValueType(value, path, diagnostics, declared) {
|
|
1290
|
+
if (!isRecord(value)) {
|
|
1291
|
+
report(diagnostics, "schema/invalid-type", "Value type must be an object.", path);
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
const hasRef = hasOwn(value, "ref");
|
|
1295
|
+
const hasType = hasOwn(value, "type");
|
|
1296
|
+
if (hasRef && hasType) {
|
|
1297
|
+
report(diagnostics, "schema/invalid-value", "Value type must declare either \"ref\" or \"type\", not both.", path);
|
|
1298
|
+
return;
|
|
1299
|
+
}
|
|
1300
|
+
if (hasRef) {
|
|
1301
|
+
forbidUnknownFields(value, ["ref"], path, diagnostics);
|
|
1302
|
+
const ref = readStringField(value, "ref", path, diagnostics, true, { nonEmpty: true });
|
|
1303
|
+
if (ref === void 0) return;
|
|
1304
|
+
if (!declared.has(ref)) {
|
|
1305
|
+
report(diagnostics, "type/unknown", `Unknown public type "${ref}".`, [...path, "ref"]);
|
|
1306
|
+
return;
|
|
1307
|
+
}
|
|
1308
|
+
return { ref };
|
|
1309
|
+
}
|
|
1310
|
+
if (!hasType) {
|
|
1311
|
+
report(diagnostics, "schema/missing-field", "Value type must declare \"type\" or \"ref\".", [...path, "type"]);
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
1314
|
+
const type = readStringField(value, "type", path, diagnostics, true, { nonEmpty: true });
|
|
1315
|
+
if (type === void 0) return;
|
|
1316
|
+
if (SIMPLE_TYPES.includes(type)) {
|
|
1317
|
+
forbidUnknownFields(value, ["type"], path, diagnostics);
|
|
1318
|
+
return { type };
|
|
1319
|
+
}
|
|
1320
|
+
switch (type) {
|
|
1321
|
+
case "array": {
|
|
1322
|
+
forbidUnknownFields(value, ["type", "items"], path, diagnostics);
|
|
1323
|
+
const itemsPath = [...path, "items"];
|
|
1324
|
+
if (!hasOwn(value, "items")) {
|
|
1325
|
+
report(diagnostics, "schema/missing-field", "Missing required field \"items\".", itemsPath);
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
const items = readValueType(value["items"], itemsPath, diagnostics, declared);
|
|
1329
|
+
return items === void 0 ? void 0 : {
|
|
1330
|
+
type: "array",
|
|
1331
|
+
items
|
|
1332
|
+
};
|
|
1333
|
+
}
|
|
1334
|
+
case "object": {
|
|
1335
|
+
forbidUnknownFields(value, [
|
|
1336
|
+
"type",
|
|
1337
|
+
"properties",
|
|
1338
|
+
"optional",
|
|
1339
|
+
"additionalProperties"
|
|
1340
|
+
], path, diagnostics);
|
|
1341
|
+
const propertiesField = readObjectField(value, "properties", path, diagnostics);
|
|
1342
|
+
if (propertiesField === void 0) return;
|
|
1343
|
+
const properties = Object.create(null);
|
|
1344
|
+
let failed = false;
|
|
1345
|
+
for (const [key, raw] of Object.entries(propertiesField)) {
|
|
1346
|
+
if (!isMemberKey(key)) {
|
|
1347
|
+
report(diagnostics, "model/invalid-interface", `Property name ${JSON.stringify(key)} must match [A-Za-z][A-Za-z0-9]*.`, [
|
|
1348
|
+
...path,
|
|
1349
|
+
"properties",
|
|
1350
|
+
key
|
|
1351
|
+
]);
|
|
1352
|
+
failed = true;
|
|
1353
|
+
continue;
|
|
1354
|
+
}
|
|
1355
|
+
const propertyType = readValueType(raw, [
|
|
1356
|
+
...path,
|
|
1357
|
+
"properties",
|
|
1358
|
+
key
|
|
1359
|
+
], diagnostics, declared);
|
|
1360
|
+
if (propertyType === void 0) {
|
|
1361
|
+
failed = true;
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1364
|
+
properties[key] = propertyType;
|
|
1365
|
+
}
|
|
1366
|
+
const optional = [];
|
|
1367
|
+
const rawOptional = readArrayField(value, "optional", path, diagnostics, false);
|
|
1368
|
+
if (rawOptional !== void 0) {
|
|
1369
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1370
|
+
rawOptional.forEach((entry, index) => {
|
|
1371
|
+
const entryPath = [
|
|
1372
|
+
...path,
|
|
1373
|
+
"optional",
|
|
1374
|
+
String(index)
|
|
1375
|
+
];
|
|
1376
|
+
if (typeof entry !== "string" || entry.length === 0) {
|
|
1377
|
+
report(diagnostics, "schema/invalid-type", "Optional property names must be non-empty strings.", entryPath);
|
|
1378
|
+
failed = true;
|
|
1379
|
+
return;
|
|
1380
|
+
}
|
|
1381
|
+
if (seen.has(entry)) {
|
|
1382
|
+
report(diagnostics, "schema/invalid-value", `Optional property "${entry}" is listed more than once.`, entryPath);
|
|
1383
|
+
failed = true;
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
seen.add(entry);
|
|
1387
|
+
if (!hasOwn(propertiesField, entry)) {
|
|
1388
|
+
report(diagnostics, "model/invalid-interface", `Optional property "${entry}" is not declared in "properties".`, entryPath);
|
|
1389
|
+
failed = true;
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
optional.push(entry);
|
|
1393
|
+
});
|
|
1394
|
+
}
|
|
1395
|
+
let additionalProperties;
|
|
1396
|
+
if (hasOwn(value, "additionalProperties")) {
|
|
1397
|
+
additionalProperties = readValueType(value["additionalProperties"], [...path, "additionalProperties"], diagnostics, declared);
|
|
1398
|
+
if (additionalProperties === void 0) failed = true;
|
|
1399
|
+
}
|
|
1400
|
+
if (failed) return;
|
|
1401
|
+
return {
|
|
1402
|
+
type: "object",
|
|
1403
|
+
properties,
|
|
1404
|
+
...rawOptional === void 0 ? {} : { optional },
|
|
1405
|
+
...additionalProperties === void 0 ? {} : { additionalProperties }
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
default:
|
|
1409
|
+
report(diagnostics, "model/invalid-interface", `Unknown value type ${JSON.stringify(type)}.`, [...path, "type"]);
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
/**
|
|
1414
|
+
* Rejects `ref` chains that never reach a concrete type, e.g. `A -> B -> A`.
|
|
1415
|
+
*
|
|
1416
|
+
* Recursion through object or array members is legal and is not reported here.
|
|
1417
|
+
*/
|
|
1418
|
+
function checkReferenceChains(types, path, diagnostics) {
|
|
1419
|
+
for (const name of Object.keys(types)) {
|
|
1420
|
+
const chain = [];
|
|
1421
|
+
let current = types[name];
|
|
1422
|
+
while (current !== void 0 && !("type" in current)) {
|
|
1423
|
+
const next = current.ref;
|
|
1424
|
+
if (next === name || chain.includes(next)) {
|
|
1425
|
+
report(diagnostics, "model/invalid-interface", `Public type "${name}" resolves through a cycle of "ref" declarations.`, [...path, name]);
|
|
1426
|
+
break;
|
|
1427
|
+
}
|
|
1428
|
+
chain.push(next);
|
|
1429
|
+
current = types[next];
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
/**
|
|
1434
|
+
* Validates one model value against its declared type (RFC 0021).
|
|
1435
|
+
*
|
|
1436
|
+
* @throws {FusedDocumentError} with the diagnostic code and JSON Pointer of every violation.
|
|
1437
|
+
*/
|
|
1438
|
+
function validateModelValue(value, type, types = {}) {
|
|
1439
|
+
const diagnostics = [];
|
|
1440
|
+
checkValue(value, type, [], diagnostics, types);
|
|
1441
|
+
if (hasErrors(diagnostics)) throw new FusedDocumentError(diagnostics);
|
|
1442
|
+
}
|
|
1443
|
+
function checkValue(value, type, path, diagnostics, types) {
|
|
1444
|
+
const resolved = resolveValueType(type, path, diagnostics, types);
|
|
1445
|
+
if (resolved === void 0) return;
|
|
1446
|
+
switch (resolved.type) {
|
|
1447
|
+
case "null":
|
|
1448
|
+
if (value !== null) mismatch(diagnostics, "null", path);
|
|
1449
|
+
return;
|
|
1450
|
+
case "boolean":
|
|
1451
|
+
if (typeof value !== "boolean") mismatch(diagnostics, "a boolean", path);
|
|
1452
|
+
return;
|
|
1453
|
+
case "number":
|
|
1454
|
+
if (typeof value !== "number" || !Number.isFinite(value)) mismatch(diagnostics, "a finite number", path);
|
|
1455
|
+
return;
|
|
1456
|
+
case "string":
|
|
1457
|
+
if (typeof value !== "string") mismatch(diagnostics, "a string", path);
|
|
1458
|
+
return;
|
|
1459
|
+
case "binary":
|
|
1460
|
+
if (!(value instanceof Uint8Array)) mismatch(diagnostics, "a Uint8Array", path);
|
|
1461
|
+
return;
|
|
1462
|
+
case "array":
|
|
1463
|
+
if (!Array.isArray(value)) {
|
|
1464
|
+
mismatch(diagnostics, "an array", path);
|
|
1465
|
+
return;
|
|
1466
|
+
}
|
|
1467
|
+
value.forEach((item, index) => {
|
|
1468
|
+
checkValue(item, resolved.items, [...path, String(index)], diagnostics, types);
|
|
1469
|
+
});
|
|
1470
|
+
return;
|
|
1471
|
+
case "object": {
|
|
1472
|
+
if (!isPlainValueObject(value)) {
|
|
1473
|
+
mismatch(diagnostics, "an object", path);
|
|
1474
|
+
return;
|
|
1475
|
+
}
|
|
1476
|
+
const optional = resolved.optional ?? [];
|
|
1477
|
+
for (const [key, propertyType] of Object.entries(resolved.properties)) {
|
|
1478
|
+
if (!hasOwn(value, key)) {
|
|
1479
|
+
if (!optional.includes(key)) report(diagnostics, "schema/invalid-value", `Missing required property "${key}".`, [...path, key]);
|
|
1480
|
+
continue;
|
|
1481
|
+
}
|
|
1482
|
+
checkValue(value[key], propertyType, [...path, key], diagnostics, types);
|
|
1483
|
+
}
|
|
1484
|
+
const additional = resolved.additionalProperties;
|
|
1485
|
+
for (const key of Object.keys(value)) {
|
|
1486
|
+
if (hasOwn(resolved.properties, key)) continue;
|
|
1487
|
+
if (additional === void 0) {
|
|
1488
|
+
report(diagnostics, "schema/invalid-value", `Unexpected property "${key}".`, [...path, key]);
|
|
1489
|
+
continue;
|
|
1490
|
+
}
|
|
1491
|
+
checkValue(value[key], additional, [...path, key], diagnostics, types);
|
|
1492
|
+
}
|
|
1493
|
+
return;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
/** Follows a chain of `ref` declarations to the concrete type it names. */
|
|
1498
|
+
function resolveValueType(type, path, diagnostics, types) {
|
|
1499
|
+
const chain = [];
|
|
1500
|
+
let current = type;
|
|
1501
|
+
while ("ref" in current) {
|
|
1502
|
+
const name = current.ref;
|
|
1503
|
+
if (chain.includes(name)) {
|
|
1504
|
+
report(diagnostics, "model/invalid-interface", `Public type "${name}" resolves through a cycle of "ref" declarations.`, path);
|
|
1505
|
+
return;
|
|
1506
|
+
}
|
|
1507
|
+
chain.push(name);
|
|
1508
|
+
const next = types[name];
|
|
1509
|
+
if (next === void 0) {
|
|
1510
|
+
report(diagnostics, "type/unknown", `Unknown public type "${name}".`, path);
|
|
1511
|
+
return;
|
|
1512
|
+
}
|
|
1513
|
+
current = next;
|
|
1514
|
+
}
|
|
1515
|
+
return current;
|
|
1516
|
+
}
|
|
1517
|
+
function mismatch(diagnostics, expected, path) {
|
|
1518
|
+
report(diagnostics, "schema/invalid-value", `Value must be ${expected}.`, path);
|
|
1519
|
+
}
|
|
1520
|
+
/** `Value` objects are plain data: no arrays, no bytes, no class instances. */
|
|
1521
|
+
function isPlainValueObject(value) {
|
|
1522
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
|
|
1523
|
+
if (value instanceof Uint8Array) return false;
|
|
1524
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1525
|
+
return prototype === Object.prototype || prototype === null;
|
|
1526
|
+
}
|
|
1527
|
+
//#endregion
|
|
1528
|
+
//#region src/fused/manifest.ts
|
|
1529
|
+
/** Reading and validation of the unified device manifest (RFC 0020). */
|
|
1530
|
+
var DEVICE_ROOT_FIELDS = [
|
|
1531
|
+
"publisher",
|
|
1532
|
+
"name",
|
|
1533
|
+
"version",
|
|
1534
|
+
"description",
|
|
1535
|
+
"license",
|
|
1536
|
+
"engines",
|
|
1537
|
+
"displayName",
|
|
1538
|
+
"icon",
|
|
1539
|
+
"designatorPrefix",
|
|
1540
|
+
"entries",
|
|
1541
|
+
"properties",
|
|
1542
|
+
"pins",
|
|
1543
|
+
"model",
|
|
1544
|
+
"view2d",
|
|
1545
|
+
"view"
|
|
1546
|
+
];
|
|
1547
|
+
/**
|
|
1548
|
+
* Package-manager fields are tolerated because a device manifest is also a published
|
|
1549
|
+
* `package.json`; they never override a device field.
|
|
1550
|
+
*/
|
|
1551
|
+
var PACKAGE_MANAGER_FIELDS = [
|
|
1552
|
+
"$schema",
|
|
1553
|
+
"author",
|
|
1554
|
+
"bugs",
|
|
1555
|
+
"bundleDependencies",
|
|
1556
|
+
"bundledDependencies",
|
|
1557
|
+
"contributors",
|
|
1558
|
+
"cpu",
|
|
1559
|
+
"dependencies",
|
|
1560
|
+
"devDependencies",
|
|
1561
|
+
"directories",
|
|
1562
|
+
"exports",
|
|
1563
|
+
"files",
|
|
1564
|
+
"funding",
|
|
1565
|
+
"homepage",
|
|
1566
|
+
"imports",
|
|
1567
|
+
"keywords",
|
|
1568
|
+
"main",
|
|
1569
|
+
"man",
|
|
1570
|
+
"module",
|
|
1571
|
+
"optionalDependencies",
|
|
1572
|
+
"os",
|
|
1573
|
+
"packageManager",
|
|
1574
|
+
"peerDependencies",
|
|
1575
|
+
"peerDependenciesMeta",
|
|
1576
|
+
"private",
|
|
1577
|
+
"publishConfig",
|
|
1578
|
+
"repository",
|
|
1579
|
+
"scripts",
|
|
1580
|
+
"sideEffects",
|
|
1581
|
+
"type",
|
|
1582
|
+
"types",
|
|
1583
|
+
"typings",
|
|
1584
|
+
"workspaces"
|
|
1585
|
+
];
|
|
1586
|
+
/**
|
|
1587
|
+
* Parses a unified device manifest (RFC 0020).
|
|
1588
|
+
*
|
|
1589
|
+
* Strings are parsed as JSON text; any other value is validated as-is.
|
|
1590
|
+
*
|
|
1591
|
+
* @throws {FusedDocumentError} with the diagnostic code and JSON Pointer of every violation.
|
|
1592
|
+
*/
|
|
1593
|
+
function parseUnifiedManifest(input) {
|
|
1594
|
+
const diagnostics = [];
|
|
1595
|
+
const parsed = parseJsonInput(input, diagnostics);
|
|
1596
|
+
if (!parsed.ok) throw new FusedDocumentError(diagnostics);
|
|
1597
|
+
const manifest = readManifest(parsed.value, diagnostics);
|
|
1598
|
+
if (manifest === void 0 || hasErrors(diagnostics)) throw new FusedDocumentError(diagnostics);
|
|
1599
|
+
return deepFreeze(manifest);
|
|
1600
|
+
}
|
|
1601
|
+
function readManifest(value, diagnostics) {
|
|
1602
|
+
if (!isRecord(value)) {
|
|
1603
|
+
report(diagnostics, "json/not-object", "Manifest must be a JSON object.", []);
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1606
|
+
forbidUnknownFields(value, [...DEVICE_ROOT_FIELDS, ...PACKAGE_MANAGER_FIELDS], [], diagnostics);
|
|
1607
|
+
const publisher = readIdentity(value, "publisher", diagnostics);
|
|
1608
|
+
const name = readIdentity(value, "name", diagnostics);
|
|
1609
|
+
const version = readVersion$1(value, diagnostics);
|
|
1610
|
+
const description = readStringField(value, "description", [], diagnostics, false);
|
|
1611
|
+
const license = readStringField(value, "license", [], diagnostics, false);
|
|
1612
|
+
const engines = readEngines(value, [], diagnostics);
|
|
1613
|
+
const displayName = readStringField(value, "displayName", [], diagnostics, true, { nonEmpty: true });
|
|
1614
|
+
const icon = readIcon(value, [], diagnostics);
|
|
1615
|
+
const designatorPrefix = readDesignatorPrefix(value, [], diagnostics);
|
|
1616
|
+
const entries = hasOwn(value, "entries") ? readEntries(value["entries"], ["entries"], diagnostics) : void 0;
|
|
1617
|
+
const properties = hasOwn(value, "properties") ? readPropertyDescriptors(value["properties"], ["properties"], diagnostics) : void 0;
|
|
1618
|
+
const pins = hasOwn(value, "pins") ? readPins(value["pins"], ["pins"], diagnostics) : void 0;
|
|
1619
|
+
const modelDeclared = hasOwn(value, "model");
|
|
1620
|
+
const model = modelDeclared ? readModel(value["model"], ["model"], diagnostics) : void 0;
|
|
1621
|
+
const pinIds = Object.keys(pins ?? {});
|
|
1622
|
+
const view2d = hasOwn(value, "view2d") ? readView2d(value["view2d"], ["view2d"], diagnostics, pinIds) : void 0;
|
|
1623
|
+
const view = hasOwn(value, "view") ? readViewMap(value["view"], ["view"], diagnostics) : void 0;
|
|
1624
|
+
if (pinIds.length > 0 && !modelDeclared) report(diagnostics, "package/invalid-manifest", "A device that declares pins must declare its model.", ["model"]);
|
|
1625
|
+
if (publisher === void 0 || name === void 0 || version === void 0 || engines === void 0 || displayName === void 0 || hasOwn(value, "icon") && icon === void 0 || hasOwn(value, "designatorPrefix") && designatorPrefix === void 0 || hasOwn(value, "entries") && entries === void 0 || hasOwn(value, "properties") && properties === void 0 || hasOwn(value, "pins") && pins === void 0 || modelDeclared && model === void 0 || hasOwn(value, "view2d") && view2d === void 0 || hasOwn(value, "view") && view === void 0) return;
|
|
1626
|
+
return {
|
|
1627
|
+
publisher,
|
|
1628
|
+
name,
|
|
1629
|
+
version,
|
|
1630
|
+
...description === void 0 ? {} : { description },
|
|
1631
|
+
...license === void 0 ? {} : { license },
|
|
1632
|
+
engines,
|
|
1633
|
+
displayName,
|
|
1634
|
+
...icon === void 0 ? {} : { icon },
|
|
1635
|
+
...designatorPrefix === void 0 ? {} : { designatorPrefix },
|
|
1636
|
+
...entries === void 0 ? {} : { entries },
|
|
1637
|
+
...properties === void 0 ? {} : { properties },
|
|
1638
|
+
...pins === void 0 ? {} : { pins },
|
|
1639
|
+
...model === void 0 ? {} : { model },
|
|
1640
|
+
...view2d === void 0 ? {} : { view2d },
|
|
1641
|
+
...view === void 0 ? {} : { view }
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
function readIdentity(value, key, diagnostics) {
|
|
1645
|
+
const identity = readStringField(value, key, [], diagnostics, true, { nonEmpty: true });
|
|
1646
|
+
if (identity === void 0) return;
|
|
1647
|
+
if (!isNameSegment(identity)) {
|
|
1648
|
+
report(diagnostics, "package/invalid-manifest", `Field "${key}" must match [a-z0-9]+(?:-[a-z0-9]+)*.`, [key]);
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
return identity;
|
|
1652
|
+
}
|
|
1653
|
+
function readVersion$1(value, diagnostics) {
|
|
1654
|
+
const version = readStringField(value, "version", [], diagnostics, true, { nonEmpty: true });
|
|
1655
|
+
if (version === void 0) return;
|
|
1656
|
+
if (!isSemVer(version)) {
|
|
1657
|
+
report(diagnostics, "package/invalid-manifest", "Field \"version\" must be a full SemVer version.", ["version"]);
|
|
1658
|
+
return;
|
|
1659
|
+
}
|
|
1660
|
+
return version;
|
|
1661
|
+
}
|
|
1662
|
+
function readEngines(value, path, diagnostics) {
|
|
1663
|
+
const engines = readObjectField(value, "engines", path, diagnostics);
|
|
1664
|
+
if (engines === void 0) return;
|
|
1665
|
+
const mica = readStringField(engines, "mica", [...path, "engines"], diagnostics, true, { nonEmpty: true });
|
|
1666
|
+
return mica === void 0 ? void 0 : { mica };
|
|
1667
|
+
}
|
|
1668
|
+
function readIcon(value, path, diagnostics) {
|
|
1669
|
+
return readPackageFilePath(value, "icon", path, diagnostics, [".png", ".svg"], "package/invalid-icon", false);
|
|
1670
|
+
}
|
|
1671
|
+
function readDesignatorPrefix(value, path, diagnostics) {
|
|
1672
|
+
const prefix = readStringField(value, "designatorPrefix", path, diagnostics, false, { nonEmpty: true });
|
|
1673
|
+
if (prefix === void 0) return;
|
|
1674
|
+
if (!isDesignatorPrefix(prefix)) {
|
|
1675
|
+
report(diagnostics, "package/invalid-manifest", "Field \"designatorPrefix\" must be a non-empty alphanumeric prefix.", [...path, "designatorPrefix"]);
|
|
1676
|
+
return;
|
|
1677
|
+
}
|
|
1678
|
+
return prefix;
|
|
1679
|
+
}
|
|
1680
|
+
function readEntries(value, path, diagnostics) {
|
|
1681
|
+
if (!isRecord(value)) {
|
|
1682
|
+
report(diagnostics, "schema/invalid-type", "Field \"entries\" must be an object.", path);
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
forbidUnknownFields(value, ["view3d"], path, diagnostics);
|
|
1686
|
+
const view3d = hasOwn(value, "view3d") ? readPackageFilePath(value, "view3d", path, diagnostics, [".js"], "package/invalid-path") : void 0;
|
|
1687
|
+
if (hasOwn(value, "view3d") && view3d === void 0) return;
|
|
1688
|
+
return { ...view3d === void 0 ? {} : { view3d } };
|
|
1689
|
+
}
|
|
1690
|
+
function readPins(value, path, diagnostics) {
|
|
1691
|
+
if (!isRecord(value)) {
|
|
1692
|
+
report(diagnostics, "schema/invalid-type", "Field \"pins\" must be an object.", path);
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
const pins = Object.create(null);
|
|
1696
|
+
let failed = false;
|
|
1697
|
+
for (const [pinId, raw] of Object.entries(value)) {
|
|
1698
|
+
const pinPath = [...path, pinId];
|
|
1699
|
+
if (!isPinId(pinId)) {
|
|
1700
|
+
report(diagnostics, "package/invalid-manifest", `Pin id ${JSON.stringify(pinId)} is not a legal model pin name.`, pinPath);
|
|
1701
|
+
failed = true;
|
|
1702
|
+
continue;
|
|
1703
|
+
}
|
|
1704
|
+
if (!isRecord(raw)) {
|
|
1705
|
+
report(diagnostics, "schema/invalid-type", "Pin declaration must be an object.", pinPath);
|
|
1706
|
+
failed = true;
|
|
1707
|
+
continue;
|
|
1708
|
+
}
|
|
1709
|
+
forbidUnknownFields(raw, ["label", "description"], pinPath, diagnostics);
|
|
1710
|
+
const label = readStringField(raw, "label", pinPath, diagnostics, false, { nonEmpty: true });
|
|
1711
|
+
const description = readStringField(raw, "description", pinPath, diagnostics, false, { nonEmpty: true });
|
|
1712
|
+
if (hasOwn(raw, "label") && label === void 0) {
|
|
1713
|
+
failed = true;
|
|
1714
|
+
continue;
|
|
1715
|
+
}
|
|
1716
|
+
if (hasOwn(raw, "description") && description === void 0) {
|
|
1717
|
+
failed = true;
|
|
1718
|
+
continue;
|
|
1719
|
+
}
|
|
1720
|
+
pins[pinId] = {
|
|
1721
|
+
...label === void 0 ? {} : { label },
|
|
1722
|
+
...description === void 0 ? {} : { description }
|
|
1723
|
+
};
|
|
1724
|
+
}
|
|
1725
|
+
return failed ? void 0 : pins;
|
|
1726
|
+
}
|
|
1727
|
+
function readModel(value, path, diagnostics) {
|
|
1728
|
+
if (!isRecord(value)) {
|
|
1729
|
+
report(diagnostics, "schema/invalid-type", "Field \"model\" must be an object.", path);
|
|
1730
|
+
return;
|
|
1731
|
+
}
|
|
1732
|
+
forbidUnknownFields(value, [
|
|
1733
|
+
"entry",
|
|
1734
|
+
"types",
|
|
1735
|
+
"methods",
|
|
1736
|
+
"events"
|
|
1737
|
+
], path, diagnostics);
|
|
1738
|
+
const entry = readPackageFilePath(value, "entry", path, diagnostics, [".js"], "package/invalid-path");
|
|
1739
|
+
const declared = /* @__PURE__ */ new Set();
|
|
1740
|
+
let types;
|
|
1741
|
+
let failed = false;
|
|
1742
|
+
const rawTypes = hasOwn(value, "types") ? readObjectField(value, "types", path, diagnostics) : void 0;
|
|
1743
|
+
if (hasOwn(value, "types") && rawTypes === void 0) failed = true;
|
|
1744
|
+
else if (rawTypes !== void 0) {
|
|
1745
|
+
const declarations = readTypeDeclarations(rawTypes, [...path, "types"], diagnostics);
|
|
1746
|
+
types = declarations.types;
|
|
1747
|
+
for (const typeName of declarations.names) declared.add(typeName);
|
|
1748
|
+
}
|
|
1749
|
+
const methods = hasOwn(value, "methods") ? readMethods(value["methods"], [...path, "methods"], diagnostics, declared) : void 0;
|
|
1750
|
+
if (hasOwn(value, "methods") && methods === void 0) failed = true;
|
|
1751
|
+
const events = hasOwn(value, "events") ? readEvents(value["events"], [...path, "events"], diagnostics, declared) : void 0;
|
|
1752
|
+
if (hasOwn(value, "events") && events === void 0) failed = true;
|
|
1753
|
+
if (failed || entry === void 0) return;
|
|
1754
|
+
return {
|
|
1755
|
+
entry,
|
|
1756
|
+
...types === void 0 ? {} : { types },
|
|
1757
|
+
...methods === void 0 ? {} : { methods },
|
|
1758
|
+
...events === void 0 ? {} : { events }
|
|
1759
|
+
};
|
|
1760
|
+
}
|
|
1761
|
+
function readMethods(value, path, diagnostics, declared) {
|
|
1762
|
+
if (!isRecord(value)) {
|
|
1763
|
+
report(diagnostics, "schema/invalid-type", "Field \"methods\" must be an object.", path);
|
|
1764
|
+
return;
|
|
1765
|
+
}
|
|
1766
|
+
const methods = Object.create(null);
|
|
1767
|
+
let failed = false;
|
|
1768
|
+
for (const [key, raw] of Object.entries(value)) {
|
|
1769
|
+
const methodPath = [...path, key];
|
|
1770
|
+
if (!isMemberKey(key)) {
|
|
1771
|
+
report(diagnostics, "model/invalid-interface", `Method name ${JSON.stringify(key)} must match [A-Za-z][A-Za-z0-9]*.`, methodPath);
|
|
1772
|
+
failed = true;
|
|
1773
|
+
continue;
|
|
1774
|
+
}
|
|
1775
|
+
if (!isRecord(raw)) {
|
|
1776
|
+
report(diagnostics, "schema/invalid-type", "Method declaration must be an object.", methodPath);
|
|
1777
|
+
failed = true;
|
|
1778
|
+
continue;
|
|
1779
|
+
}
|
|
1780
|
+
forbidUnknownFields(raw, ["params", "returns"], methodPath, diagnostics);
|
|
1781
|
+
const params = readRequiredValueType(raw, "params", methodPath, diagnostics, declared);
|
|
1782
|
+
const returns = readRequiredValueType(raw, "returns", methodPath, diagnostics, declared);
|
|
1783
|
+
if (params === void 0 || returns === void 0) {
|
|
1784
|
+
failed = true;
|
|
1785
|
+
continue;
|
|
1786
|
+
}
|
|
1787
|
+
methods[key] = {
|
|
1788
|
+
params,
|
|
1789
|
+
returns
|
|
1790
|
+
};
|
|
1791
|
+
}
|
|
1792
|
+
return failed ? void 0 : methods;
|
|
1793
|
+
}
|
|
1794
|
+
function readEvents(value, path, diagnostics, declared) {
|
|
1795
|
+
if (!isRecord(value)) {
|
|
1796
|
+
report(diagnostics, "schema/invalid-type", "Field \"events\" must be an object.", path);
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
const events = Object.create(null);
|
|
1800
|
+
let failed = false;
|
|
1801
|
+
for (const [key, raw] of Object.entries(value)) {
|
|
1802
|
+
const eventPath = [...path, key];
|
|
1803
|
+
if (!isEventKey(key)) {
|
|
1804
|
+
report(diagnostics, "model/invalid-interface", `Event name ${JSON.stringify(key)} must use the onDidXxx form.`, eventPath);
|
|
1805
|
+
failed = true;
|
|
1806
|
+
continue;
|
|
1807
|
+
}
|
|
1808
|
+
const type = readValueType(raw, eventPath, diagnostics, declared);
|
|
1809
|
+
if (type === void 0) {
|
|
1810
|
+
failed = true;
|
|
1811
|
+
continue;
|
|
1812
|
+
}
|
|
1813
|
+
events[key] = type;
|
|
1814
|
+
}
|
|
1815
|
+
return failed ? void 0 : events;
|
|
1816
|
+
}
|
|
1817
|
+
function readRequiredValueType(value, key, path, diagnostics, declared) {
|
|
1818
|
+
const fieldPath = [...path, key];
|
|
1819
|
+
if (!hasOwn(value, key)) {
|
|
1820
|
+
report(diagnostics, "schema/missing-field", `Missing required field "${key}".`, fieldPath);
|
|
1821
|
+
return;
|
|
1822
|
+
}
|
|
1823
|
+
return readValueType(value[key], fieldPath, diagnostics, declared);
|
|
1824
|
+
}
|
|
1825
|
+
function readView2d(value, path, diagnostics, pins) {
|
|
1826
|
+
if (!isRecord(value)) {
|
|
1827
|
+
report(diagnostics, "schema/invalid-type", "Field \"view2d\" must be an object.", path);
|
|
1828
|
+
return;
|
|
1829
|
+
}
|
|
1830
|
+
forbidUnknownFields(value, ["geometry", "entry"], path, diagnostics);
|
|
1831
|
+
let geometry;
|
|
1832
|
+
if (!hasOwn(value, "geometry")) report(diagnostics, "schema/missing-field", "Missing required field \"geometry\".", [...path, "geometry"]);
|
|
1833
|
+
else if (typeof value["geometry"] === "string") geometry = readPackageFilePath(value, "geometry", path, diagnostics, [".json"], "package/invalid-path");
|
|
1834
|
+
else geometry = readGeometry2D(value["geometry"], [...path, "geometry"], diagnostics, pins);
|
|
1835
|
+
const entry = hasOwn(value, "entry") ? readPackageFilePath(value, "entry", path, diagnostics, [".js"], "package/invalid-path") : void 0;
|
|
1836
|
+
if (geometry === void 0 || hasOwn(value, "entry") && entry === void 0) return;
|
|
1837
|
+
return {
|
|
1838
|
+
geometry,
|
|
1839
|
+
...entry === void 0 ? {} : { entry }
|
|
1840
|
+
};
|
|
1841
|
+
}
|
|
1842
|
+
function readViewMap(value, path, diagnostics) {
|
|
1843
|
+
if (!isRecord(value)) {
|
|
1844
|
+
report(diagnostics, "schema/invalid-type", "Field \"view\" must be an object.", path);
|
|
1845
|
+
return;
|
|
1846
|
+
}
|
|
1847
|
+
const views = Object.create(null);
|
|
1848
|
+
let failed = false;
|
|
1849
|
+
for (const [viewId, raw] of Object.entries(value)) {
|
|
1850
|
+
const viewPath = [...path, viewId];
|
|
1851
|
+
if (!isMemberKey(viewId)) {
|
|
1852
|
+
report(diagnostics, "package/invalid-manifest", `View id ${JSON.stringify(viewId)} must match [A-Za-z][A-Za-z0-9]*.`, viewPath);
|
|
1853
|
+
failed = true;
|
|
1854
|
+
continue;
|
|
1855
|
+
}
|
|
1856
|
+
const view = readHtmlView(raw, viewPath, diagnostics);
|
|
1857
|
+
if (view === void 0) {
|
|
1858
|
+
failed = true;
|
|
1859
|
+
continue;
|
|
1860
|
+
}
|
|
1861
|
+
views[viewId] = view;
|
|
1862
|
+
}
|
|
1863
|
+
return failed ? void 0 : views;
|
|
1864
|
+
}
|
|
1865
|
+
function readHtmlView(value, path, diagnostics) {
|
|
1866
|
+
if (!isRecord(value)) {
|
|
1867
|
+
report(diagnostics, "schema/invalid-type", "HTML view declaration must be an object.", path);
|
|
1868
|
+
return;
|
|
1869
|
+
}
|
|
1870
|
+
forbidUnknownFields(value, [
|
|
1871
|
+
"title",
|
|
1872
|
+
"entry",
|
|
1873
|
+
"defaultLocation",
|
|
1874
|
+
"icon"
|
|
1875
|
+
], path, diagnostics);
|
|
1876
|
+
const title = readStringField(value, "title", path, diagnostics, true, { nonEmpty: true });
|
|
1877
|
+
const entry = readPackageFilePath(value, "entry", path, diagnostics, [".html"], "package/invalid-path");
|
|
1878
|
+
const defaultLocation = readStringField(value, "defaultLocation", path, diagnostics, false, { nonEmpty: true });
|
|
1879
|
+
const icon = readStringField(value, "icon", path, diagnostics, false, { nonEmpty: true });
|
|
1880
|
+
let failed = false;
|
|
1881
|
+
let location;
|
|
1882
|
+
if (defaultLocation !== void 0) {
|
|
1883
|
+
if (defaultLocation === "sidebar" || defaultLocation === "floating") location = defaultLocation;
|
|
1884
|
+
else {
|
|
1885
|
+
report(diagnostics, "package/invalid-manifest", "Field \"defaultLocation\" must be \"sidebar\" or \"floating\".", [...path, "defaultLocation"]);
|
|
1886
|
+
failed = true;
|
|
1887
|
+
}
|
|
1888
|
+
}
|
|
1889
|
+
if (icon !== void 0 && !isCodiconName(icon)) {
|
|
1890
|
+
report(diagnostics, "package/invalid-manifest", "Field \"icon\" must be a Codicons icon name.", [...path, "icon"]);
|
|
1891
|
+
failed = true;
|
|
1892
|
+
}
|
|
1893
|
+
if (failed || title === void 0 || entry === void 0) return;
|
|
1894
|
+
return {
|
|
1895
|
+
title,
|
|
1896
|
+
entry,
|
|
1897
|
+
...location === void 0 ? {} : { defaultLocation: location },
|
|
1898
|
+
...icon === void 0 ? {} : { icon }
|
|
1899
|
+
};
|
|
1900
|
+
}
|
|
1901
|
+
function readPackageFilePath(value, key, path, diagnostics, extensions, code, required = true) {
|
|
1902
|
+
const file = readStringField(value, key, path, diagnostics, required, { nonEmpty: true });
|
|
1903
|
+
if (file === void 0) return;
|
|
1904
|
+
const pathProblem = packagePathProblem(file);
|
|
1905
|
+
if (pathProblem !== void 0) {
|
|
1906
|
+
report(diagnostics, code, `Field "${key}" ${pathProblem}.`, [...path, key]);
|
|
1907
|
+
return;
|
|
1908
|
+
}
|
|
1909
|
+
const extensionIssue = extensionProblem(file, extensions);
|
|
1910
|
+
if (extensionIssue !== void 0) {
|
|
1911
|
+
report(diagnostics, code, `Field "${key}" ${extensionIssue}.`, [...path, key]);
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
return file;
|
|
1915
|
+
}
|
|
1916
|
+
//#endregion
|
|
1917
|
+
//#region src/fused/resource.ts
|
|
1918
|
+
var RESOURCE_MODES = ["asset", "package"];
|
|
1919
|
+
/** Reads one instance property value; `undefined` when it does not match the contract. */
|
|
1920
|
+
function readPropertyValue(value, path, diagnostics) {
|
|
1921
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
1922
|
+
if (typeof value === "number") {
|
|
1923
|
+
if (!Number.isFinite(value)) {
|
|
1924
|
+
report(diagnostics, "schema/invalid-value", "Numeric property values must be finite.", path);
|
|
1925
|
+
return;
|
|
1926
|
+
}
|
|
1927
|
+
return value;
|
|
1928
|
+
}
|
|
1929
|
+
if (isRecord(value)) return readReference$1(value, path, diagnostics);
|
|
1930
|
+
report(diagnostics, "schema/invalid-type", "Property value must be a scalar, a resource reference, a firmware reference or null.", path);
|
|
1931
|
+
}
|
|
1932
|
+
function readReference$1(value, path, diagnostics) {
|
|
1933
|
+
const mode = readStringField(value, "mode", path, diagnostics, true, { nonEmpty: true });
|
|
1934
|
+
if (mode === void 0) return;
|
|
1935
|
+
if (mode === "project") return readProjectReference(value, path, diagnostics);
|
|
1936
|
+
if (!RESOURCE_MODES.includes(mode)) {
|
|
1937
|
+
report(diagnostics, "schema/invalid-value", "Field \"mode\" must be \"asset\", \"package\" or \"project\".", [...path, "mode"]);
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1940
|
+
return readResourceReference(value, mode, path, diagnostics);
|
|
1941
|
+
}
|
|
1942
|
+
function readProjectReference(value, path, diagnostics) {
|
|
1943
|
+
forbidUnknownFields(value, [
|
|
1944
|
+
"mode",
|
|
1945
|
+
"projectId",
|
|
1946
|
+
"name",
|
|
1947
|
+
"addressSpace",
|
|
1948
|
+
"loadAddress"
|
|
1949
|
+
], path, diagnostics);
|
|
1950
|
+
const projectId = readStringField(value, "projectId", path, diagnostics, true, { nonEmpty: true });
|
|
1951
|
+
const overrides = readAddressOverrides(value, path, diagnostics);
|
|
1952
|
+
if (projectId === void 0 || overrides === void 0) return;
|
|
1953
|
+
const name = readStringField(value, "name", path, diagnostics, false, { nonEmpty: true });
|
|
1954
|
+
return {
|
|
1955
|
+
mode: "project",
|
|
1956
|
+
projectId,
|
|
1957
|
+
...name === void 0 ? {} : { name },
|
|
1958
|
+
...overrides
|
|
1959
|
+
};
|
|
1960
|
+
}
|
|
1961
|
+
function readResourceReference(value, mode, path, diagnostics) {
|
|
1962
|
+
const hasOverrides = hasOwn(value, "addressSpace") || hasOwn(value, "loadAddress");
|
|
1963
|
+
const fields = mode === "asset" ? [
|
|
1964
|
+
"assetId",
|
|
1965
|
+
"name",
|
|
1966
|
+
"mode"
|
|
1967
|
+
] : ["src", "mode"];
|
|
1968
|
+
forbidUnknownFields(value, hasOverrides ? [
|
|
1969
|
+
...fields,
|
|
1970
|
+
"addressSpace",
|
|
1971
|
+
"loadAddress"
|
|
1972
|
+
] : fields, path, diagnostics);
|
|
1973
|
+
if (mode === "asset") {
|
|
1974
|
+
const assetId = readStringField(value, "assetId", path, diagnostics, true, { nonEmpty: true });
|
|
1975
|
+
const name = readStringField(value, "name", path, diagnostics, true, { nonEmpty: true });
|
|
1976
|
+
const overrides = readAddressOverrides(value, path, diagnostics);
|
|
1977
|
+
if (assetId === void 0 || name === void 0 || overrides === void 0) return void 0;
|
|
1978
|
+
return {
|
|
1979
|
+
mode,
|
|
1980
|
+
assetId,
|
|
1981
|
+
name,
|
|
1982
|
+
...overrides
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
const src = readStringField(value, "src", path, diagnostics, true, { nonEmpty: true });
|
|
1986
|
+
if (src === void 0) return;
|
|
1987
|
+
const srcProblem = resourceSrcProblem(mode, src);
|
|
1988
|
+
if (srcProblem !== void 0) {
|
|
1989
|
+
report(diagnostics, "schema/invalid-value", `Field "src" ${srcProblem}.`, [...path, "src"]);
|
|
1990
|
+
return;
|
|
1991
|
+
}
|
|
1992
|
+
if (!hasOverrides) return {
|
|
1993
|
+
src,
|
|
1994
|
+
mode
|
|
1995
|
+
};
|
|
1996
|
+
const overrides = readAddressOverrides(value, path, diagnostics);
|
|
1997
|
+
if (overrides === void 0) return;
|
|
1998
|
+
return {
|
|
1999
|
+
src,
|
|
2000
|
+
mode: "package",
|
|
2001
|
+
...overrides
|
|
2002
|
+
};
|
|
2003
|
+
}
|
|
2004
|
+
/** Reads optional `addressSpace` / `loadAddress` overrides of a firmware reference. */
|
|
2005
|
+
function readAddressOverrides(value, path, diagnostics) {
|
|
2006
|
+
const addressSpace = readStringField(value, "addressSpace", path, diagnostics, false, { nonEmpty: true });
|
|
2007
|
+
const loadAddress = readIntegerField(value, "loadAddress", path, diagnostics, false);
|
|
2008
|
+
if (loadAddress !== void 0 && loadAddress < 0) {
|
|
2009
|
+
report(diagnostics, "schema/invalid-value", "Field \"loadAddress\" must be a non-negative safe integer.", [...path, "loadAddress"]);
|
|
2010
|
+
return;
|
|
2011
|
+
}
|
|
2012
|
+
if (hasOwn(value, "addressSpace") && addressSpace === void 0) return;
|
|
2013
|
+
if (hasOwn(value, "loadAddress") && loadAddress === void 0) return;
|
|
2014
|
+
return {
|
|
2015
|
+
...addressSpace === void 0 ? {} : { addressSpace },
|
|
2016
|
+
...loadAddress === void 0 ? {} : { loadAddress }
|
|
2017
|
+
};
|
|
2018
|
+
}
|
|
2019
|
+
/** Payload problem of a resource `src` for its mode, or `undefined` when it is well formed. */
|
|
2020
|
+
function resourceSrcProblem(mode, src) {
|
|
2021
|
+
switch (mode) {
|
|
2022
|
+
case "package": return packagePathProblem(src);
|
|
2023
|
+
case "asset": return src.length === 0 ? "must not be empty" : void 0;
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
//#endregion
|
|
2027
|
+
//#region src/fused/types/document.ts
|
|
2028
|
+
/** Version of the fused schematic document contract (RFC 0024). */
|
|
2029
|
+
var FUSED_DOCUMENT_VERSION = 2;
|
|
2030
|
+
//#endregion
|
|
2031
|
+
//#region src/fused/document.ts
|
|
2032
|
+
/** Reading and validation of the fused `.sch` version 2 document (RFC 0024). */
|
|
2033
|
+
var DOCUMENT_FIELDS = [
|
|
2034
|
+
"version",
|
|
2035
|
+
"dependencies",
|
|
2036
|
+
"instances",
|
|
2037
|
+
"nodes",
|
|
2038
|
+
"wires",
|
|
2039
|
+
"labels",
|
|
2040
|
+
"references",
|
|
2041
|
+
"display"
|
|
2042
|
+
];
|
|
2043
|
+
/**
|
|
2044
|
+
* Parses a fused `.sch` version 2 document (RFC 0024).
|
|
2045
|
+
*
|
|
2046
|
+
* Strings are parsed as JSON text; any other value is validated as-is.
|
|
2047
|
+
*
|
|
2048
|
+
* @throws {FusedDocumentError} with the diagnostic code and JSON Pointer of every violation.
|
|
2049
|
+
*/
|
|
2050
|
+
function parseFusedDocument(input) {
|
|
2051
|
+
const diagnostics = [];
|
|
2052
|
+
const parsed = parseJsonInput(input, diagnostics);
|
|
2053
|
+
if (!parsed.ok) throw new FusedDocumentError(diagnostics);
|
|
2054
|
+
const document = readDocument(parsed.value, diagnostics);
|
|
2055
|
+
if (document === void 0 || hasErrors(diagnostics)) throw new FusedDocumentError(diagnostics);
|
|
2056
|
+
return deepFreeze(document);
|
|
2057
|
+
}
|
|
2058
|
+
function readDocument(value, diagnostics) {
|
|
2059
|
+
if (!isRecord(value)) {
|
|
2060
|
+
report(diagnostics, "json/not-object", "Fused schematic must be a JSON object.", []);
|
|
2061
|
+
return;
|
|
2062
|
+
}
|
|
2063
|
+
forbidUnknownFields(value, DOCUMENT_FIELDS, [], diagnostics);
|
|
2064
|
+
const version = readVersion(value, diagnostics);
|
|
2065
|
+
const dependencies = readDependencies(readObjectField(value, "dependencies", [], diagnostics), ["dependencies"], diagnostics);
|
|
2066
|
+
const instances = readCollection(value, "instances", [], diagnostics, readInstance);
|
|
2067
|
+
const nodes = readCollection(value, "nodes", [], diagnostics, readNode);
|
|
2068
|
+
const wires = readCollection(value, "wires", [], diagnostics, readWire);
|
|
2069
|
+
const labels = readCollection(value, "labels", [], diagnostics, readLabel);
|
|
2070
|
+
const references = readCollection(value, "references", [], diagnostics, readReference);
|
|
2071
|
+
const display = hasOwn(value, "display") ? readDisplay(value["display"], ["display"], diagnostics) : void 0;
|
|
2072
|
+
checkDuplicateIds(instances, nodes, wires, labels, references, diagnostics);
|
|
2073
|
+
checkDuplicateDesignators(instances, diagnostics);
|
|
2074
|
+
const instanceIds = new Set(instances.items.map((item) => item.value.id));
|
|
2075
|
+
const nodeIds = new Set(nodes.items.map((item) => item.value.id));
|
|
2076
|
+
if (dependencies !== void 0) {
|
|
2077
|
+
for (const item of instances.items) if (!Object.prototype.hasOwnProperty.call(dependencies, item.value.device)) report(diagnostics, "document/unlocked-device", `Instance device "${item.value.device}" is not locked by "dependencies".`, [...item.path, "device"]);
|
|
2078
|
+
}
|
|
2079
|
+
for (const item of wires.items) {
|
|
2080
|
+
const fromPath = [...item.path, "from"];
|
|
2081
|
+
const toPath = [...item.path, "to"];
|
|
2082
|
+
checkEndpoint(item.value.from, fromPath, instanceIds, nodeIds, diagnostics);
|
|
2083
|
+
checkEndpoint(item.value.to, toPath, instanceIds, nodeIds, diagnostics);
|
|
2084
|
+
if (sameEndpoint(item.value.from, item.value.to)) report(diagnostics, "connection/self-edge", "A wire must not connect an endpoint to itself.", toPath);
|
|
2085
|
+
}
|
|
2086
|
+
for (const item of labels.items) checkEndpoint(item.value.target, [...item.path, "target"], instanceIds, nodeIds, diagnostics);
|
|
2087
|
+
for (const item of references.items) checkEndpoint(item.value.target, [...item.path, "target"], instanceIds, nodeIds, diagnostics);
|
|
2088
|
+
if (version === void 0 || dependencies === void 0 || instances.failed || nodes.failed || wires.failed || labels.failed || references.failed || hasOwn(value, "display") && display === void 0) return;
|
|
2089
|
+
return {
|
|
2090
|
+
version,
|
|
2091
|
+
dependencies,
|
|
2092
|
+
instances: instances.items.map((item) => item.value),
|
|
2093
|
+
nodes: nodes.items.map((item) => item.value),
|
|
2094
|
+
wires: wires.items.map((item) => item.value),
|
|
2095
|
+
labels: labels.items.map((item) => item.value),
|
|
2096
|
+
references: references.items.map((item) => item.value),
|
|
2097
|
+
...display === void 0 ? {} : { display }
|
|
2098
|
+
};
|
|
2099
|
+
}
|
|
2100
|
+
function readVersion(value, diagnostics) {
|
|
2101
|
+
const version = readIntegerField(value, "version", [], diagnostics);
|
|
2102
|
+
if (version === void 0) return;
|
|
2103
|
+
if (version !== 2) {
|
|
2104
|
+
report(diagnostics, "schema/unsupported-version", `Unsupported version ${String(version)}; expected ${String(2)}.`, ["version"]);
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
return 2;
|
|
2108
|
+
}
|
|
2109
|
+
function readCollection(value, key, path, diagnostics, readItem) {
|
|
2110
|
+
const raw = readArrayField(value, key, path, diagnostics);
|
|
2111
|
+
if (raw === void 0) return {
|
|
2112
|
+
items: [],
|
|
2113
|
+
failed: true
|
|
2114
|
+
};
|
|
2115
|
+
const items = [];
|
|
2116
|
+
let failed = false;
|
|
2117
|
+
raw.forEach((entry, index) => {
|
|
2118
|
+
const itemPath = [
|
|
2119
|
+
...path,
|
|
2120
|
+
key,
|
|
2121
|
+
String(index)
|
|
2122
|
+
];
|
|
2123
|
+
const item = readItem(entry, itemPath, diagnostics);
|
|
2124
|
+
if (item === void 0) {
|
|
2125
|
+
failed = true;
|
|
2126
|
+
return;
|
|
2127
|
+
}
|
|
2128
|
+
items.push({
|
|
2129
|
+
value: item,
|
|
2130
|
+
path: itemPath
|
|
2131
|
+
});
|
|
2132
|
+
});
|
|
2133
|
+
return {
|
|
2134
|
+
items,
|
|
2135
|
+
failed
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
function readDependencies(value, path, diagnostics) {
|
|
2139
|
+
if (value === void 0) return;
|
|
2140
|
+
const dependencies = Object.create(null);
|
|
2141
|
+
let failed = false;
|
|
2142
|
+
for (const [deviceId, raw] of Object.entries(value)) {
|
|
2143
|
+
const dependencyPath = [...path, deviceId];
|
|
2144
|
+
const idProblem = deviceIdProblem(deviceId);
|
|
2145
|
+
if (idProblem !== void 0) {
|
|
2146
|
+
report(diagnostics, "schema/invalid-value", `Dependency key ${JSON.stringify(deviceId)} ${idProblem}.`, dependencyPath);
|
|
2147
|
+
failed = true;
|
|
2148
|
+
continue;
|
|
2149
|
+
}
|
|
2150
|
+
if (!isRecord(raw)) {
|
|
2151
|
+
report(diagnostics, "schema/invalid-type", "Dependency must be an object.", dependencyPath);
|
|
2152
|
+
failed = true;
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
forbidUnknownFields(raw, ["version", "integrity"], dependencyPath, diagnostics);
|
|
2156
|
+
const version = readStringField(raw, "version", dependencyPath, diagnostics, true, { nonEmpty: true });
|
|
2157
|
+
if (version !== void 0 && !isSemVer(version)) {
|
|
2158
|
+
report(diagnostics, "schema/invalid-value", "Dependency \"version\" must be a full SemVer version.", [...dependencyPath, "version"]);
|
|
2159
|
+
failed = true;
|
|
2160
|
+
}
|
|
2161
|
+
const integrity = readStringField(raw, "integrity", dependencyPath, diagnostics, true, { nonEmpty: true });
|
|
2162
|
+
const integrityIssue = integrity === void 0 ? void 0 : integrityProblem(integrity);
|
|
2163
|
+
if (integrityIssue !== void 0) {
|
|
2164
|
+
report(diagnostics, "schema/invalid-value", `Dependency "integrity" ${integrityIssue}.`, [...dependencyPath, "integrity"]);
|
|
2165
|
+
failed = true;
|
|
2166
|
+
}
|
|
2167
|
+
if (version === void 0 || integrity === void 0 || integrityIssue !== void 0) {
|
|
2168
|
+
failed = true;
|
|
2169
|
+
continue;
|
|
2170
|
+
}
|
|
2171
|
+
dependencies[deviceId] = {
|
|
2172
|
+
version,
|
|
2173
|
+
integrity
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
return failed ? void 0 : dependencies;
|
|
2177
|
+
}
|
|
2178
|
+
function readInstance(raw, path, diagnostics) {
|
|
2179
|
+
if (!isRecord(raw)) {
|
|
2180
|
+
report(diagnostics, "schema/invalid-type", "Instance must be an object.", path);
|
|
2181
|
+
return;
|
|
2182
|
+
}
|
|
2183
|
+
forbidUnknownFields(raw, [
|
|
2184
|
+
"id",
|
|
2185
|
+
"device",
|
|
2186
|
+
"designator",
|
|
2187
|
+
"designatorOffset",
|
|
2188
|
+
"placement",
|
|
2189
|
+
"properties",
|
|
2190
|
+
"display",
|
|
2191
|
+
"viewLayout"
|
|
2192
|
+
], path, diagnostics);
|
|
2193
|
+
const id = readUuid(raw, "id", path, diagnostics);
|
|
2194
|
+
const device = readStringField(raw, "device", path, diagnostics, true, { nonEmpty: true });
|
|
2195
|
+
const deviceProblem = device === void 0 ? void 0 : deviceIdProblem(device);
|
|
2196
|
+
if (deviceProblem !== void 0) report(diagnostics, "schema/invalid-value", `Field "device" ${deviceProblem}.`, [...path, "device"]);
|
|
2197
|
+
const designator = readStringField(raw, "designator", path, diagnostics, false, { nonEmpty: true });
|
|
2198
|
+
const designatorOffset = hasOwn(raw, "designatorOffset") ? readPoint2D(raw["designatorOffset"], [...path, "designatorOffset"], diagnostics) : void 0;
|
|
2199
|
+
const placement = hasOwn(raw, "placement") ? readPlacement(raw["placement"], [...path, "placement"], diagnostics) : void 0;
|
|
2200
|
+
if (!hasOwn(raw, "placement")) report(diagnostics, "schema/missing-field", "Missing required field \"placement\".", [...path, "placement"]);
|
|
2201
|
+
const properties = hasOwn(raw, "properties") ? readProperties(raw["properties"], [...path, "properties"], diagnostics) : void 0;
|
|
2202
|
+
const bodyDisplay = hasOwn(raw, "display") ? readInstanceDisplay(raw["display"], [...path, "display"], diagnostics) : void 0;
|
|
2203
|
+
const viewLayout = hasOwn(raw, "viewLayout") ? readViewLayout(raw["viewLayout"], [...path, "viewLayout"], diagnostics) : void 0;
|
|
2204
|
+
if (id === void 0 || device === void 0 || deviceProblem !== void 0 || placement === void 0 || hasOwn(raw, "properties") && properties === void 0 || hasOwn(raw, "display") && bodyDisplay === void 0 || hasOwn(raw, "viewLayout") && viewLayout === void 0 || hasOwn(raw, "designatorOffset") && designatorOffset === void 0 || hasOwn(raw, "designator") && designator === void 0) return;
|
|
2205
|
+
return {
|
|
2206
|
+
id,
|
|
2207
|
+
device,
|
|
2208
|
+
...designator === void 0 ? {} : { designator },
|
|
2209
|
+
...designatorOffset === void 0 ? {} : { designatorOffset },
|
|
2210
|
+
placement,
|
|
2211
|
+
...properties === void 0 ? {} : { properties },
|
|
2212
|
+
...bodyDisplay === void 0 ? {} : { display: bodyDisplay },
|
|
2213
|
+
...viewLayout === void 0 ? {} : { viewLayout }
|
|
2214
|
+
};
|
|
2215
|
+
}
|
|
2216
|
+
function readPlacement(raw, path, diagnostics) {
|
|
2217
|
+
if (!isRecord(raw)) {
|
|
2218
|
+
report(diagnostics, "schema/invalid-type", "Placement must be an object.", path);
|
|
2219
|
+
return;
|
|
2220
|
+
}
|
|
2221
|
+
forbidUnknownFields(raw, [
|
|
2222
|
+
"x",
|
|
2223
|
+
"y",
|
|
2224
|
+
"rotation",
|
|
2225
|
+
"width",
|
|
2226
|
+
"height"
|
|
2227
|
+
], path, diagnostics);
|
|
2228
|
+
const x = readNumberField(raw, "x", path, diagnostics);
|
|
2229
|
+
const y = readNumberField(raw, "y", path, diagnostics);
|
|
2230
|
+
const rotation = readNumberField(raw, "rotation", path, diagnostics);
|
|
2231
|
+
const hasWidth = hasOwn(raw, "width");
|
|
2232
|
+
const hasHeight = hasOwn(raw, "height");
|
|
2233
|
+
const width = readNumberField(raw, "width", path, diagnostics, hasWidth);
|
|
2234
|
+
const height = readNumberField(raw, "height", path, diagnostics, hasHeight);
|
|
2235
|
+
let failed = false;
|
|
2236
|
+
if (hasWidth !== hasHeight) {
|
|
2237
|
+
report(diagnostics, "schema/invalid-value", "Flexible placement must declare \"width\" and \"height\" together.", path);
|
|
2238
|
+
failed = true;
|
|
2239
|
+
}
|
|
2240
|
+
if (width !== void 0 && width <= 0) {
|
|
2241
|
+
report(diagnostics, "schema/invalid-value", "Field \"width\" must be positive.", [...path, "width"]);
|
|
2242
|
+
failed = true;
|
|
2243
|
+
}
|
|
2244
|
+
if (height !== void 0 && height <= 0) {
|
|
2245
|
+
report(diagnostics, "schema/invalid-value", "Field \"height\" must be positive.", [...path, "height"]);
|
|
2246
|
+
failed = true;
|
|
2247
|
+
}
|
|
2248
|
+
if (failed || x === void 0 || y === void 0 || rotation === void 0) return;
|
|
2249
|
+
return {
|
|
2250
|
+
x,
|
|
2251
|
+
y,
|
|
2252
|
+
rotation,
|
|
2253
|
+
...width === void 0 ? {} : { width },
|
|
2254
|
+
...height === void 0 ? {} : { height }
|
|
2255
|
+
};
|
|
2256
|
+
}
|
|
2257
|
+
function readProperties(raw, path, diagnostics) {
|
|
2258
|
+
if (!isRecord(raw)) {
|
|
2259
|
+
report(diagnostics, "schema/invalid-type", "Field \"properties\" must be an object.", path);
|
|
2260
|
+
return;
|
|
2261
|
+
}
|
|
2262
|
+
const properties = Object.create(null);
|
|
2263
|
+
let failed = false;
|
|
2264
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
2265
|
+
const entryPath = [...path, key];
|
|
2266
|
+
if (!isMemberKey(key)) {
|
|
2267
|
+
report(diagnostics, "schema/invalid-value", `Property key ${JSON.stringify(key)} must match [A-Za-z][A-Za-z0-9]*.`, entryPath);
|
|
2268
|
+
failed = true;
|
|
2269
|
+
continue;
|
|
2270
|
+
}
|
|
2271
|
+
const property = readPropertyValue(value, entryPath, diagnostics);
|
|
2272
|
+
if (property === void 0) {
|
|
2273
|
+
failed = true;
|
|
2274
|
+
continue;
|
|
2275
|
+
}
|
|
2276
|
+
properties[key] = property;
|
|
2277
|
+
}
|
|
2278
|
+
return failed ? void 0 : properties;
|
|
2279
|
+
}
|
|
2280
|
+
function readInstanceDisplay(raw, path, diagnostics) {
|
|
2281
|
+
if (!isRecord(raw)) {
|
|
2282
|
+
report(diagnostics, "schema/invalid-type", "Field \"display\" must be an object.", path);
|
|
2283
|
+
return;
|
|
2284
|
+
}
|
|
2285
|
+
forbidUnknownFields(raw, ["body"], path, diagnostics);
|
|
2286
|
+
const body = readBooleanField(raw, "body", path, diagnostics, false);
|
|
2287
|
+
if (hasOwn(raw, "body") && body === void 0) return;
|
|
2288
|
+
return { ...body === void 0 ? {} : { body } };
|
|
2289
|
+
}
|
|
2290
|
+
function readDisplay(raw, path, diagnostics) {
|
|
2291
|
+
if (!isRecord(raw)) {
|
|
2292
|
+
report(diagnostics, "schema/invalid-type", "Field \"display\" must be an object.", path);
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
2295
|
+
forbidUnknownFields(raw, ["body", "designators"], path, diagnostics);
|
|
2296
|
+
const body = readBooleanField(raw, "body", path, diagnostics, false);
|
|
2297
|
+
const designators = readBooleanField(raw, "designators", path, diagnostics, false);
|
|
2298
|
+
if (hasOwn(raw, "body") && body === void 0 || hasOwn(raw, "designators") && designators === void 0) return;
|
|
2299
|
+
return {
|
|
2300
|
+
...body === void 0 ? {} : { body },
|
|
2301
|
+
...designators === void 0 ? {} : { designators }
|
|
2302
|
+
};
|
|
2303
|
+
}
|
|
2304
|
+
function readViewLayout(raw, path, diagnostics) {
|
|
2305
|
+
if (!isRecord(raw)) {
|
|
2306
|
+
report(diagnostics, "schema/invalid-type", "Field \"viewLayout\" must be an object.", path);
|
|
2307
|
+
return;
|
|
2308
|
+
}
|
|
2309
|
+
forbidUnknownFields(raw, ["properties", "views"], path, diagnostics);
|
|
2310
|
+
const propertiesPath = [...path, "properties"];
|
|
2311
|
+
const rawProperties = readObjectField(raw, "properties", path, diagnostics);
|
|
2312
|
+
const properties = rawProperties === void 0 ? void 0 : readSidebarLayout(rawProperties, propertiesPath, diagnostics);
|
|
2313
|
+
const rawViews = readArrayField(raw, "views", path, diagnostics);
|
|
2314
|
+
let failed = false;
|
|
2315
|
+
const views = [];
|
|
2316
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2317
|
+
if (rawViews !== void 0) rawViews.forEach((entry, index) => {
|
|
2318
|
+
const viewPath = [
|
|
2319
|
+
...path,
|
|
2320
|
+
"views",
|
|
2321
|
+
String(index)
|
|
2322
|
+
];
|
|
2323
|
+
const view = readHtmlViewLayout(entry, viewPath, diagnostics);
|
|
2324
|
+
if (view === void 0) {
|
|
2325
|
+
failed = true;
|
|
2326
|
+
return;
|
|
2327
|
+
}
|
|
2328
|
+
if (seen.has(view.viewId)) {
|
|
2329
|
+
report(diagnostics, "document/invalid-view-layout", `View id "${view.viewId}" is laid out more than once.`, viewPath);
|
|
2330
|
+
failed = true;
|
|
2331
|
+
return;
|
|
2332
|
+
}
|
|
2333
|
+
seen.add(view.viewId);
|
|
2334
|
+
views.push(view);
|
|
2335
|
+
});
|
|
2336
|
+
if (failed || properties === void 0 || rawViews === void 0) return;
|
|
2337
|
+
return {
|
|
2338
|
+
properties,
|
|
2339
|
+
views
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
function readSidebarLayout(raw, path, diagnostics) {
|
|
2343
|
+
forbidUnknownFields(raw, ["height", "collapsed"], path, diagnostics);
|
|
2344
|
+
const height = readNumberField(raw, "height", path, diagnostics);
|
|
2345
|
+
const collapsed = readBooleanField(raw, "collapsed", path, diagnostics);
|
|
2346
|
+
let failed = false;
|
|
2347
|
+
if (height !== void 0 && height <= 0) {
|
|
2348
|
+
report(diagnostics, "document/invalid-view-layout", "Layout \"height\" must be a finite positive number.", [...path, "height"]);
|
|
2349
|
+
failed = true;
|
|
2350
|
+
}
|
|
2351
|
+
if (failed || height === void 0 || collapsed === void 0) return;
|
|
2352
|
+
return {
|
|
2353
|
+
height,
|
|
2354
|
+
collapsed
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
function readHtmlViewLayout(raw, path, diagnostics) {
|
|
2358
|
+
if (!isRecord(raw)) {
|
|
2359
|
+
report(diagnostics, "schema/invalid-type", "HTML view layout must be an object.", path);
|
|
2360
|
+
return;
|
|
2361
|
+
}
|
|
2362
|
+
forbidUnknownFields(raw, [
|
|
2363
|
+
"viewId",
|
|
2364
|
+
"location",
|
|
2365
|
+
"height",
|
|
2366
|
+
"collapsed",
|
|
2367
|
+
"floatingBounds"
|
|
2368
|
+
], path, diagnostics);
|
|
2369
|
+
const viewId = readStringField(raw, "viewId", path, diagnostics, true, { nonEmpty: true });
|
|
2370
|
+
const location = readStringField(raw, "location", path, diagnostics, true, { nonEmpty: true });
|
|
2371
|
+
let failed = false;
|
|
2372
|
+
let resolvedLocation;
|
|
2373
|
+
if (location !== void 0) {
|
|
2374
|
+
if (location === "sidebar" || location === "floating") resolvedLocation = location;
|
|
2375
|
+
else {
|
|
2376
|
+
report(diagnostics, "document/invalid-view-layout", "Field \"location\" must be \"sidebar\" or \"floating\".", [...path, "location"]);
|
|
2377
|
+
failed = true;
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
const height = readNumberField(raw, "height", path, diagnostics);
|
|
2381
|
+
const collapsed = readBooleanField(raw, "collapsed", path, diagnostics);
|
|
2382
|
+
if (height !== void 0 && height <= 0) {
|
|
2383
|
+
report(diagnostics, "document/invalid-view-layout", "Layout \"height\" must be a finite positive number.", [...path, "height"]);
|
|
2384
|
+
failed = true;
|
|
2385
|
+
}
|
|
2386
|
+
let floatingBounds;
|
|
2387
|
+
if (hasOwn(raw, "floatingBounds")) {
|
|
2388
|
+
const boundsPath = [...path, "floatingBounds"];
|
|
2389
|
+
const rawBounds = raw["floatingBounds"];
|
|
2390
|
+
if (!isRecord(rawBounds)) {
|
|
2391
|
+
report(diagnostics, "document/invalid-view-layout", "Field \"floatingBounds\" must be an object.", boundsPath);
|
|
2392
|
+
failed = true;
|
|
2393
|
+
} else {
|
|
2394
|
+
forbidUnknownFields(rawBounds, [
|
|
2395
|
+
"x",
|
|
2396
|
+
"y",
|
|
2397
|
+
"width",
|
|
2398
|
+
"height"
|
|
2399
|
+
], boundsPath, diagnostics);
|
|
2400
|
+
const x = readNumberField(rawBounds, "x", boundsPath, diagnostics);
|
|
2401
|
+
const y = readNumberField(rawBounds, "y", boundsPath, diagnostics);
|
|
2402
|
+
const width = readNumberField(rawBounds, "width", boundsPath, diagnostics);
|
|
2403
|
+
const boundsHeight = readNumberField(rawBounds, "height", boundsPath, diagnostics);
|
|
2404
|
+
if (width !== void 0 && width <= 0) {
|
|
2405
|
+
report(diagnostics, "document/invalid-view-layout", "Floating bounds \"width\" must be a finite positive number.", [...boundsPath, "width"]);
|
|
2406
|
+
failed = true;
|
|
2407
|
+
}
|
|
2408
|
+
if (boundsHeight !== void 0 && boundsHeight <= 0) {
|
|
2409
|
+
report(diagnostics, "document/invalid-view-layout", "Floating bounds \"height\" must be a finite positive number.", [...boundsPath, "height"]);
|
|
2410
|
+
failed = true;
|
|
2411
|
+
}
|
|
2412
|
+
if (x !== void 0 && y !== void 0 && width !== void 0 && boundsHeight !== void 0) floatingBounds = {
|
|
2413
|
+
x,
|
|
2414
|
+
y,
|
|
2415
|
+
width,
|
|
2416
|
+
height: boundsHeight
|
|
2417
|
+
};
|
|
2418
|
+
else failed = true;
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
if (failed || viewId === void 0 || resolvedLocation === void 0 || height === void 0 || collapsed === void 0) return;
|
|
2422
|
+
return {
|
|
2423
|
+
viewId,
|
|
2424
|
+
location: resolvedLocation,
|
|
2425
|
+
height,
|
|
2426
|
+
collapsed,
|
|
2427
|
+
...floatingBounds === void 0 ? {} : { floatingBounds }
|
|
2428
|
+
};
|
|
2429
|
+
}
|
|
2430
|
+
function readNode(raw, path, diagnostics) {
|
|
2431
|
+
if (!isRecord(raw)) {
|
|
2432
|
+
report(diagnostics, "schema/invalid-type", "Node must be an object.", path);
|
|
2433
|
+
return;
|
|
2434
|
+
}
|
|
2435
|
+
forbidUnknownFields(raw, [
|
|
2436
|
+
"id",
|
|
2437
|
+
"kind",
|
|
2438
|
+
"x",
|
|
2439
|
+
"y"
|
|
2440
|
+
], path, diagnostics);
|
|
2441
|
+
const id = readUuid(raw, "id", path, diagnostics);
|
|
2442
|
+
const kind = readStringField(raw, "kind", path, diagnostics, true, { nonEmpty: true });
|
|
2443
|
+
const x = readNumberField(raw, "x", path, diagnostics);
|
|
2444
|
+
const y = readNumberField(raw, "y", path, diagnostics);
|
|
2445
|
+
let resolvedKind;
|
|
2446
|
+
if (kind !== void 0) {
|
|
2447
|
+
if (kind === "junction" || kind === "free") resolvedKind = kind;
|
|
2448
|
+
else report(diagnostics, "schema/invalid-value", "Field \"kind\" must be \"junction\" or \"free\".", [...path, "kind"]);
|
|
2449
|
+
}
|
|
2450
|
+
if (id === void 0 || resolvedKind === void 0 || x === void 0 || y === void 0) return;
|
|
2451
|
+
return {
|
|
2452
|
+
id,
|
|
2453
|
+
kind: resolvedKind,
|
|
2454
|
+
x,
|
|
2455
|
+
y
|
|
2456
|
+
};
|
|
2457
|
+
}
|
|
2458
|
+
function readWire(raw, path, diagnostics) {
|
|
2459
|
+
if (!isRecord(raw)) {
|
|
2460
|
+
report(diagnostics, "schema/invalid-type", "Wire must be an object.", path);
|
|
2461
|
+
return;
|
|
2462
|
+
}
|
|
2463
|
+
forbidUnknownFields(raw, [
|
|
2464
|
+
"id",
|
|
2465
|
+
"from",
|
|
2466
|
+
"to",
|
|
2467
|
+
"bends"
|
|
2468
|
+
], path, diagnostics);
|
|
2469
|
+
const id = readUuid(raw, "id", path, diagnostics);
|
|
2470
|
+
const from = hasOwn(raw, "from") ? readEndpoint(raw["from"], [...path, "from"], diagnostics) : void 0;
|
|
2471
|
+
const to = hasOwn(raw, "to") ? readEndpoint(raw["to"], [...path, "to"], diagnostics) : void 0;
|
|
2472
|
+
const rawBends = readArrayField(raw, "bends", path, diagnostics);
|
|
2473
|
+
const bends = [];
|
|
2474
|
+
let failed = false;
|
|
2475
|
+
if (rawBends !== void 0) rawBends.forEach((entry, index) => {
|
|
2476
|
+
const point = readPoint2D(entry, [
|
|
2477
|
+
...path,
|
|
2478
|
+
"bends",
|
|
2479
|
+
String(index)
|
|
2480
|
+
], diagnostics);
|
|
2481
|
+
if (point === void 0) {
|
|
2482
|
+
failed = true;
|
|
2483
|
+
return;
|
|
2484
|
+
}
|
|
2485
|
+
bends.push(point);
|
|
2486
|
+
});
|
|
2487
|
+
if (id === void 0 || from === void 0 || to === void 0 || rawBends === void 0) return;
|
|
2488
|
+
if (failed) return;
|
|
2489
|
+
return {
|
|
2490
|
+
id,
|
|
2491
|
+
from,
|
|
2492
|
+
to,
|
|
2493
|
+
bends
|
|
2494
|
+
};
|
|
2495
|
+
}
|
|
2496
|
+
function readEndpoint(raw, path, diagnostics) {
|
|
2497
|
+
if (!isRecord(raw)) {
|
|
2498
|
+
report(diagnostics, "schema/invalid-type", "Endpoint must be an object.", path);
|
|
2499
|
+
return;
|
|
2500
|
+
}
|
|
2501
|
+
const kind = readStringField(raw, "kind", path, diagnostics, true, { nonEmpty: true });
|
|
2502
|
+
if (kind === "pin") {
|
|
2503
|
+
forbidUnknownFields(raw, [
|
|
2504
|
+
"kind",
|
|
2505
|
+
"instance",
|
|
2506
|
+
"pin"
|
|
2507
|
+
], path, diagnostics);
|
|
2508
|
+
const instance = readStringField(raw, "instance", path, diagnostics, true, { nonEmpty: true });
|
|
2509
|
+
const pin = readStringField(raw, "pin", path, diagnostics, true, { nonEmpty: true });
|
|
2510
|
+
return instance === void 0 || pin === void 0 ? void 0 : {
|
|
2511
|
+
kind: "pin",
|
|
2512
|
+
instance,
|
|
2513
|
+
pin
|
|
2514
|
+
};
|
|
2515
|
+
}
|
|
2516
|
+
if (kind === "node") {
|
|
2517
|
+
forbidUnknownFields(raw, ["kind", "node"], path, diagnostics);
|
|
2518
|
+
const node = readStringField(raw, "node", path, diagnostics, true, { nonEmpty: true });
|
|
2519
|
+
return node === void 0 ? void 0 : {
|
|
2520
|
+
kind: "node",
|
|
2521
|
+
node
|
|
2522
|
+
};
|
|
2523
|
+
}
|
|
2524
|
+
if (kind !== void 0) report(diagnostics, "schema/invalid-value", "Field \"kind\" must be \"pin\" or \"node\".", [...path, "kind"]);
|
|
2525
|
+
}
|
|
2526
|
+
function readLabel(raw, path, diagnostics) {
|
|
2527
|
+
if (!isRecord(raw)) {
|
|
2528
|
+
report(diagnostics, "schema/invalid-type", "Net label must be an object.", path);
|
|
2529
|
+
return;
|
|
2530
|
+
}
|
|
2531
|
+
forbidUnknownFields(raw, [
|
|
2532
|
+
"id",
|
|
2533
|
+
"target",
|
|
2534
|
+
"name",
|
|
2535
|
+
"offset"
|
|
2536
|
+
], path, diagnostics);
|
|
2537
|
+
const id = readUuid(raw, "id", path, diagnostics);
|
|
2538
|
+
const target = hasOwn(raw, "target") ? readEndpoint(raw["target"], [...path, "target"], diagnostics) : void 0;
|
|
2539
|
+
if (!hasOwn(raw, "target")) report(diagnostics, "schema/missing-field", "Missing required field \"target\".", [...path, "target"]);
|
|
2540
|
+
const name = readStringField(raw, "name", path, diagnostics, true, { nonEmpty: true });
|
|
2541
|
+
const offset = hasOwn(raw, "offset") ? readPoint2D(raw["offset"], [...path, "offset"], diagnostics) : void 0;
|
|
2542
|
+
if (id === void 0 || target === void 0 || name === void 0 || hasOwn(raw, "offset") && offset === void 0) return;
|
|
2543
|
+
return {
|
|
2544
|
+
id,
|
|
2545
|
+
target,
|
|
2546
|
+
name,
|
|
2547
|
+
...offset === void 0 ? {} : { offset }
|
|
2548
|
+
};
|
|
2549
|
+
}
|
|
2550
|
+
function readReference(raw, path, diagnostics) {
|
|
2551
|
+
if (!isRecord(raw)) {
|
|
2552
|
+
report(diagnostics, "schema/invalid-type", "Voltage reference must be an object.", path);
|
|
2553
|
+
return;
|
|
2554
|
+
}
|
|
2555
|
+
forbidUnknownFields(raw, [
|
|
2556
|
+
"id",
|
|
2557
|
+
"target",
|
|
2558
|
+
"voltage",
|
|
2559
|
+
"symbol",
|
|
2560
|
+
"rotation",
|
|
2561
|
+
"label"
|
|
2562
|
+
], path, diagnostics);
|
|
2563
|
+
const id = readUuid(raw, "id", path, diagnostics);
|
|
2564
|
+
const target = hasOwn(raw, "target") ? readEndpoint(raw["target"], [...path, "target"], diagnostics) : void 0;
|
|
2565
|
+
if (!hasOwn(raw, "target")) report(diagnostics, "schema/missing-field", "Missing required field \"target\".", [...path, "target"]);
|
|
2566
|
+
const voltage = readNumberField(raw, "voltage", path, diagnostics);
|
|
2567
|
+
const rawSymbol = readStringField(raw, "symbol", path, diagnostics, false, { nonEmpty: true });
|
|
2568
|
+
let symbol;
|
|
2569
|
+
let failed = false;
|
|
2570
|
+
if (rawSymbol !== void 0) {
|
|
2571
|
+
if (rawSymbol === "power" || rawSymbol === "ground") symbol = rawSymbol;
|
|
2572
|
+
else {
|
|
2573
|
+
report(diagnostics, "schema/invalid-value", "Field \"symbol\" must be \"power\" or \"ground\".", [...path, "symbol"]);
|
|
2574
|
+
failed = true;
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
const rotation = readNumberField(raw, "rotation", path, diagnostics, false);
|
|
2578
|
+
if (symbol === "ground" && voltage !== void 0 && voltage !== 0) {
|
|
2579
|
+
report(diagnostics, "schema/invalid-value", "A ground symbol requires \"voltage\": 0.", [...path, "voltage"]);
|
|
2580
|
+
failed = true;
|
|
2581
|
+
}
|
|
2582
|
+
let label;
|
|
2583
|
+
if (hasOwn(raw, "label")) {
|
|
2584
|
+
if (symbol === void 0) {
|
|
2585
|
+
report(diagnostics, "schema/invalid-value", "Field \"label\" requires a declared \"symbol\".", [...path, "label"]);
|
|
2586
|
+
failed = true;
|
|
2587
|
+
} else {
|
|
2588
|
+
const labelPath = [...path, "label"];
|
|
2589
|
+
const rawLabel = raw["label"];
|
|
2590
|
+
if (!isRecord(rawLabel)) {
|
|
2591
|
+
report(diagnostics, "schema/invalid-type", "Field \"label\" must be an object.", labelPath);
|
|
2592
|
+
failed = true;
|
|
2593
|
+
} else {
|
|
2594
|
+
forbidUnknownFields(rawLabel, ["name", "offset"], labelPath, diagnostics);
|
|
2595
|
+
const name = readStringField(rawLabel, "name", labelPath, diagnostics, true, { nonEmpty: true });
|
|
2596
|
+
const offset = hasOwn(rawLabel, "offset") ? readPoint2D(rawLabel["offset"], [...labelPath, "offset"], diagnostics) : void 0;
|
|
2597
|
+
if (name === void 0 || hasOwn(rawLabel, "offset") && offset === void 0) failed = true;
|
|
2598
|
+
else label = {
|
|
2599
|
+
name,
|
|
2600
|
+
...offset === void 0 ? {} : { offset }
|
|
2601
|
+
};
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
}
|
|
2605
|
+
if (failed || id === void 0 || target === void 0 || voltage === void 0 || hasOwn(raw, "rotation") && rotation === void 0) return;
|
|
2606
|
+
return {
|
|
2607
|
+
id,
|
|
2608
|
+
target,
|
|
2609
|
+
voltage,
|
|
2610
|
+
...symbol === void 0 ? {} : { symbol },
|
|
2611
|
+
...rotation === void 0 ? {} : { rotation },
|
|
2612
|
+
...label === void 0 ? {} : { label }
|
|
2613
|
+
};
|
|
2614
|
+
}
|
|
2615
|
+
function readUuid(value, key, path, diagnostics) {
|
|
2616
|
+
const id = readStringField(value, key, path, diagnostics, true, { nonEmpty: true });
|
|
2617
|
+
if (id === void 0) return;
|
|
2618
|
+
if (!isUuidV4(id)) {
|
|
2619
|
+
report(diagnostics, "schema/invalid-value", `Field "${key}" must be a UUID v4.`, [...path, key]);
|
|
2620
|
+
return;
|
|
2621
|
+
}
|
|
2622
|
+
return id;
|
|
2623
|
+
}
|
|
2624
|
+
function checkEndpoint(endpoint, path, instanceIds, nodeIds, diagnostics) {
|
|
2625
|
+
if (endpoint.kind === "pin") {
|
|
2626
|
+
if (!instanceIds.has(endpoint.instance)) report(diagnostics, "connection/unknown-endpoint", `Endpoint references unknown instance "${endpoint.instance}".`, [...path, "instance"]);
|
|
2627
|
+
return;
|
|
2628
|
+
}
|
|
2629
|
+
if (!nodeIds.has(endpoint.node)) report(diagnostics, "connection/unknown-endpoint", `Endpoint references unknown node "${endpoint.node}".`, [...path, "node"]);
|
|
2630
|
+
}
|
|
2631
|
+
function sameEndpoint(a, b) {
|
|
2632
|
+
if (a.kind === "pin") return b.kind === "pin" && a.instance === b.instance && a.pin === b.pin;
|
|
2633
|
+
return b.kind === "node" && a.node === b.node;
|
|
2634
|
+
}
|
|
2635
|
+
function checkDuplicateIds(instances, nodes, wires, labels, references, diagnostics) {
|
|
2636
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2637
|
+
const collections = [
|
|
2638
|
+
instances,
|
|
2639
|
+
nodes,
|
|
2640
|
+
wires,
|
|
2641
|
+
labels,
|
|
2642
|
+
references
|
|
2643
|
+
];
|
|
2644
|
+
for (const collection of collections) for (const item of collection.items) {
|
|
2645
|
+
if (seen.has(item.value.id)) {
|
|
2646
|
+
report(diagnostics, "document/duplicate-id", `Id "${item.value.id}" is used more than once in the document.`, [...item.path, "id"]);
|
|
2647
|
+
continue;
|
|
2648
|
+
}
|
|
2649
|
+
seen.add(item.value.id);
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
function checkDuplicateDesignators(instances, diagnostics) {
|
|
2653
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2654
|
+
for (const item of instances.items) {
|
|
2655
|
+
const designator = item.value.designator;
|
|
2656
|
+
if (designator === void 0) continue;
|
|
2657
|
+
if (seen.has(designator)) {
|
|
2658
|
+
report(diagnostics, "document/duplicate-designator", `Designator ${JSON.stringify(designator)} is used more than once.`, [...item.path, "designator"]);
|
|
2659
|
+
continue;
|
|
2660
|
+
}
|
|
2661
|
+
seen.add(designator);
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
//#endregion
|
|
2665
|
+
//#region src/fused/topology.ts
|
|
2666
|
+
/**
|
|
2667
|
+
* Derivation of the electrical nets of a fused document (RFC 0024).
|
|
2668
|
+
*
|
|
2669
|
+
* Nets come from the explicit connection graph: wires and shared endpoints first, then the
|
|
2670
|
+
* equality rules of net names (including names owned by power and ground symbols), and finally
|
|
2671
|
+
* the ideal voltage constraints. Model-internal conduction never merges document nets.
|
|
2672
|
+
*/
|
|
2673
|
+
/**
|
|
2674
|
+
* Derives the nets of `document` using the pinned pins of `manifests`.
|
|
2675
|
+
*
|
|
2676
|
+
* @throws {FusedDocumentError} when an instance has no manifest, a pin endpoint is not declared,
|
|
2677
|
+
* a connection endpoint is missing, or a net has conflicting names or ideal voltages.
|
|
2678
|
+
*/
|
|
2679
|
+
function deriveFusedTopology(document, manifests) {
|
|
2680
|
+
const diagnostics = [];
|
|
2681
|
+
const pinsByInstance = readInstancePins(document, manifests, diagnostics);
|
|
2682
|
+
const parent = /* @__PURE__ */ new Map();
|
|
2683
|
+
const endpoints = /* @__PURE__ */ new Map();
|
|
2684
|
+
const names = /* @__PURE__ */ new Map();
|
|
2685
|
+
const voltages = /* @__PURE__ */ new Map();
|
|
2686
|
+
const find = (key) => {
|
|
2687
|
+
let root = key;
|
|
2688
|
+
for (;;) {
|
|
2689
|
+
const next = parent.get(root);
|
|
2690
|
+
if (next === void 0 || next === root) return root;
|
|
2691
|
+
root = next;
|
|
2692
|
+
}
|
|
2693
|
+
};
|
|
2694
|
+
const add = (key) => {
|
|
2695
|
+
if (!parent.has(key)) parent.set(key, key);
|
|
2696
|
+
};
|
|
2697
|
+
const union = (a, b) => {
|
|
2698
|
+
add(a);
|
|
2699
|
+
add(b);
|
|
2700
|
+
const rootA = find(a);
|
|
2701
|
+
const rootB = find(b);
|
|
2702
|
+
if (rootA !== rootB) parent.set(rootB, rootA);
|
|
2703
|
+
};
|
|
2704
|
+
const knownNodes = new Set(document.nodes.map((node) => node.id));
|
|
2705
|
+
const register = (endpoint, path) => {
|
|
2706
|
+
const key = endpointKey(endpoint);
|
|
2707
|
+
if (!endpoints.has(key)) endpoints.set(key, endpoint);
|
|
2708
|
+
add(key);
|
|
2709
|
+
if (endpoint.kind === "pin") {
|
|
2710
|
+
if (!pinsByInstance.has(endpoint.instance)) {
|
|
2711
|
+
report(diagnostics, "connection/unknown-endpoint", `Pin endpoint references unknown instance "${endpoint.instance}".`, [...path, "instance"]);
|
|
2712
|
+
return key;
|
|
2713
|
+
}
|
|
2714
|
+
const pins = pinsByInstance.get(endpoint.instance);
|
|
2715
|
+
if (pins !== void 0 && !pins.has(endpoint.pin)) report(diagnostics, "device/unknown-pin", `Instance "${endpoint.instance}" does not declare pin "${endpoint.pin}".`, [...path, "pin"]);
|
|
2716
|
+
return key;
|
|
2717
|
+
}
|
|
2718
|
+
if (!knownNodes.has(endpoint.node)) report(diagnostics, "connection/unknown-endpoint", `Endpoint references unknown node "${endpoint.node}".`, [...path, "node"]);
|
|
2719
|
+
return key;
|
|
2720
|
+
};
|
|
2721
|
+
document.wires.forEach((wire, index) => {
|
|
2722
|
+
const path = ["wires", String(index)];
|
|
2723
|
+
const fromKey = register(wire.from, [...path, "from"]);
|
|
2724
|
+
const toKey = register(wire.to, [...path, "to"]);
|
|
2725
|
+
if (fromKey === toKey) {
|
|
2726
|
+
report(diagnostics, "connection/self-edge", "A wire must not connect an endpoint to itself.", [...path, "to"]);
|
|
2727
|
+
return;
|
|
2728
|
+
}
|
|
2729
|
+
union(fromKey, toKey);
|
|
2730
|
+
});
|
|
2731
|
+
document.labels.forEach((label, index) => {
|
|
2732
|
+
const path = ["labels", String(index)];
|
|
2733
|
+
const key = register(label.target, [...path, "target"]);
|
|
2734
|
+
push(names, key, {
|
|
2735
|
+
name: label.name,
|
|
2736
|
+
path: [...path, "name"],
|
|
2737
|
+
order: index
|
|
2738
|
+
});
|
|
2739
|
+
});
|
|
2740
|
+
document.references.forEach((reference, index) => {
|
|
2741
|
+
const path = ["references", String(index)];
|
|
2742
|
+
const key = register(reference.target, [...path, "target"]);
|
|
2743
|
+
push(voltages, key, {
|
|
2744
|
+
voltage: reference.voltage,
|
|
2745
|
+
path: [...path, "voltage"],
|
|
2746
|
+
order: index
|
|
2747
|
+
});
|
|
2748
|
+
if (reference.label !== void 0) push(names, key, {
|
|
2749
|
+
name: reference.label.name,
|
|
2750
|
+
path: [
|
|
2751
|
+
...path,
|
|
2752
|
+
"label",
|
|
2753
|
+
"name"
|
|
2754
|
+
],
|
|
2755
|
+
order: index
|
|
2756
|
+
});
|
|
2757
|
+
});
|
|
2758
|
+
if (hasErrors(diagnostics)) throw new FusedDocumentError(diagnostics);
|
|
2759
|
+
let groups = collectGroups(parent, find);
|
|
2760
|
+
reportNamingConflicts(groups, names, diagnostics);
|
|
2761
|
+
mergeGroupsByName(groups, names, union);
|
|
2762
|
+
groups = collectGroups(parent, find);
|
|
2763
|
+
for (const keys of groups.values()) reportVoltageConflicts(keys, voltages, diagnostics);
|
|
2764
|
+
if (hasErrors(diagnostics)) throw new FusedDocumentError(diagnostics);
|
|
2765
|
+
const nets = [];
|
|
2766
|
+
for (const keys of groups.values()) {
|
|
2767
|
+
const sortedKeys = [...keys].sort(compareStrings);
|
|
2768
|
+
const pins = [];
|
|
2769
|
+
let voltage;
|
|
2770
|
+
let named = false;
|
|
2771
|
+
for (const key of sortedKeys) {
|
|
2772
|
+
const endpoint = endpoints.get(key);
|
|
2773
|
+
if (endpoint !== void 0 && endpoint.kind === "pin") pins.push({
|
|
2774
|
+
instance: endpoint.instance,
|
|
2775
|
+
pin: endpoint.pin
|
|
2776
|
+
});
|
|
2777
|
+
if (names.has(key)) named = true;
|
|
2778
|
+
const constraint = voltages.get(key)?.[0];
|
|
2779
|
+
if (constraint !== void 0) voltage = constraint.voltage;
|
|
2780
|
+
}
|
|
2781
|
+
if (pins.length === 0) continue;
|
|
2782
|
+
if (!named && voltage === void 0 && sortedKeys.length < 2) continue;
|
|
2783
|
+
pins.sort((a, b) => compareStrings(a.instance, b.instance) || compareStrings(a.pin, b.pin));
|
|
2784
|
+
nets.push({
|
|
2785
|
+
id: sortedKeys.join("|"),
|
|
2786
|
+
pins,
|
|
2787
|
+
...voltage === void 0 ? {} : { voltage }
|
|
2788
|
+
});
|
|
2789
|
+
}
|
|
2790
|
+
return { nets: nets.sort((a, b) => compareStrings(a.id, b.id)) };
|
|
2791
|
+
}
|
|
2792
|
+
function endpointKey(endpoint) {
|
|
2793
|
+
return endpoint.kind === "pin" ? `pin:${endpoint.instance}/${endpoint.pin}` : `node:${endpoint.node}`;
|
|
2794
|
+
}
|
|
2795
|
+
function push(map, key, value) {
|
|
2796
|
+
const bucket = map.get(key);
|
|
2797
|
+
if (bucket === void 0) {
|
|
2798
|
+
map.set(key, [value]);
|
|
2799
|
+
return;
|
|
2800
|
+
}
|
|
2801
|
+
bucket.push(value);
|
|
2802
|
+
}
|
|
2803
|
+
function readInstancePins(document, manifests, diagnostics) {
|
|
2804
|
+
const pins = /* @__PURE__ */ new Map();
|
|
2805
|
+
document.instances.forEach((instance, index) => {
|
|
2806
|
+
const manifest = manifests.get(instance.device);
|
|
2807
|
+
if (manifest === void 0) {
|
|
2808
|
+
report(diagnostics, "device/unresolved", `Instance "${instance.id}" references device "${instance.device}", which has no manifest.`, [
|
|
2809
|
+
"instances",
|
|
2810
|
+
String(index),
|
|
2811
|
+
"device"
|
|
2812
|
+
]);
|
|
2813
|
+
return;
|
|
2814
|
+
}
|
|
2815
|
+
pins.set(instance.id, new Set(Object.keys(manifest.pins ?? {})));
|
|
2816
|
+
});
|
|
2817
|
+
return pins;
|
|
2818
|
+
}
|
|
2819
|
+
function collectGroups(parent, find) {
|
|
2820
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2821
|
+
for (const key of parent.keys()) {
|
|
2822
|
+
const root = find(key);
|
|
2823
|
+
const bucket = groups.get(root);
|
|
2824
|
+
if (bucket === void 0) {
|
|
2825
|
+
groups.set(root, /* @__PURE__ */ new Set([key]));
|
|
2826
|
+
continue;
|
|
2827
|
+
}
|
|
2828
|
+
bucket.add(key);
|
|
2829
|
+
}
|
|
2830
|
+
return groups;
|
|
2831
|
+
}
|
|
2832
|
+
function reportNamingConflicts(groups, names, diagnostics) {
|
|
2833
|
+
for (const keys of groups.values()) {
|
|
2834
|
+
const entries = [];
|
|
2835
|
+
for (const key of keys) entries.push(...names.get(key) ?? []);
|
|
2836
|
+
entries.sort((a, b) => a.order - b.order);
|
|
2837
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2838
|
+
for (const entry of entries) {
|
|
2839
|
+
if (seen.has(entry.name)) continue;
|
|
2840
|
+
seen.add(entry.name);
|
|
2841
|
+
if (seen.size > 1) {
|
|
2842
|
+
report(diagnostics, "net/naming-conflict", `Net has conflicting names: ${[...seen].map((name) => JSON.stringify(name)).join(", ")}.`, entry.path);
|
|
2843
|
+
break;
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
function mergeGroupsByName(groups, names, union) {
|
|
2849
|
+
const byName = /* @__PURE__ */ new Map();
|
|
2850
|
+
for (const [root, keys] of groups) {
|
|
2851
|
+
const name = componentName(keys, names);
|
|
2852
|
+
if (name === void 0) continue;
|
|
2853
|
+
const existing = byName.get(name);
|
|
2854
|
+
if (existing === void 0) {
|
|
2855
|
+
byName.set(name, root);
|
|
2856
|
+
continue;
|
|
2857
|
+
}
|
|
2858
|
+
union(existing, root);
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
function componentName(keys, names) {
|
|
2862
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2863
|
+
for (const key of keys) for (const entry of names.get(key) ?? []) seen.add(entry.name);
|
|
2864
|
+
return seen.size === 1 ? [...seen][0] : void 0;
|
|
2865
|
+
}
|
|
2866
|
+
function reportVoltageConflicts(keys, voltages, diagnostics) {
|
|
2867
|
+
const constraints = [];
|
|
2868
|
+
for (const key of keys) constraints.push(...voltages.get(key) ?? []);
|
|
2869
|
+
constraints.sort((a, b) => a.order - b.order);
|
|
2870
|
+
const [first, ...rest] = constraints;
|
|
2871
|
+
if (first === void 0) return;
|
|
2872
|
+
const conflict = rest.find((constraint) => constraint.voltage !== first.voltage);
|
|
2873
|
+
if (conflict !== void 0) report(diagnostics, "net/reference-conflict", `Net has conflicting ideal voltages ${String(first.voltage)} and ${String(conflict.voltage)}.`, conflict.path);
|
|
2874
|
+
}
|
|
2875
|
+
//#endregion
|
|
2876
|
+
export { FUSED_DOCUMENT_VERSION, FusedDocumentError, STRICT_JSON_MAX_DEPTH, decodeStrictJson, deriveFusedTopology, isCodiconName, isDeviceId, isEventKey, isIntegrity, isMemberKey, isNameSegment, isPinId, isSemVer, packagePathProblem, parseFusedDocument, parseGeometry2D, parseStrictJson, parseUnifiedManifest, projectPathProblem, validateModelValue };
|
|
2877
|
+
|
|
2878
|
+
//# sourceMappingURL=index.js.map
|