wpilog-parser 2.2.0 → 2.4.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 +25 -7
- package/dist/catalog-BT6geQr0.mjs +1343 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +81 -0
- package/dist/index.d.mts +83 -69
- package/dist/index.mjs +2 -1070
- package/package.json +24 -17
- package/skills/analyze-wpilog/SKILL.md +15 -26
|
@@ -0,0 +1,1343 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
2
|
+
import { CstParser, Lexer, createToken } from "chevrotain";
|
|
3
|
+
//#region src/byte-offset.ts
|
|
4
|
+
var ByteOffset = class {
|
|
5
|
+
offset;
|
|
6
|
+
constructor(offset = 0) {
|
|
7
|
+
this.offset = offset;
|
|
8
|
+
}
|
|
9
|
+
get() {
|
|
10
|
+
return this.offset;
|
|
11
|
+
}
|
|
12
|
+
advance(bytes) {
|
|
13
|
+
this.offset += bytes;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
advance8() {
|
|
17
|
+
return this.advance(1);
|
|
18
|
+
}
|
|
19
|
+
advance16() {
|
|
20
|
+
return this.advance(2);
|
|
21
|
+
}
|
|
22
|
+
advance32() {
|
|
23
|
+
return this.advance(4);
|
|
24
|
+
}
|
|
25
|
+
advance64() {
|
|
26
|
+
return this.advance(8);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/diagnostics.ts
|
|
31
|
+
/** Stable codes for errors callers can act on. */
|
|
32
|
+
const diagnostics = defineDiagnostics({ codes: {
|
|
33
|
+
WPILOG_C0001: {
|
|
34
|
+
why: (p) => `Unknown command \`${p.command}\``,
|
|
35
|
+
fix: "Run `wpilog --help` to see the available commands."
|
|
36
|
+
},
|
|
37
|
+
WPILOG_C0002: {
|
|
38
|
+
why: "Output flags cannot be combined",
|
|
39
|
+
fix: "Choose one of --json, --jsonl, --ndjson, or --csv."
|
|
40
|
+
},
|
|
41
|
+
WPILOG_C0003: {
|
|
42
|
+
why: (p) => `File not found: ${p.filePath}`,
|
|
43
|
+
fix: "Check the path and try again with an existing WPILOG file."
|
|
44
|
+
},
|
|
45
|
+
WPILOG_R0001: {
|
|
46
|
+
why: (p) => `Invalid control record type ${p.type}`,
|
|
47
|
+
fix: "Check that the input is an uncorrupted WPILOG file."
|
|
48
|
+
},
|
|
49
|
+
WPILOG_R0002: {
|
|
50
|
+
why: "Not a WPILOG file (truncated header)",
|
|
51
|
+
fix: "Provide a complete WPILOG file."
|
|
52
|
+
},
|
|
53
|
+
WPILOG_R0003: {
|
|
54
|
+
why: "Not a WPILOG file (invalid magic bytes)",
|
|
55
|
+
fix: "Provide a file in WPILOG format."
|
|
56
|
+
},
|
|
57
|
+
WPILOG_R0004: {
|
|
58
|
+
why: "Not a WPILOG file (truncated extra header)",
|
|
59
|
+
fix: "Provide a complete WPILOG file."
|
|
60
|
+
},
|
|
61
|
+
WPILOG_R0005: {
|
|
62
|
+
why: (p) => `Invalid boolean value ${p.byte}`,
|
|
63
|
+
fix: "Use 0 or 1 for boolean values in the log."
|
|
64
|
+
},
|
|
65
|
+
WPILOG_R0006: {
|
|
66
|
+
why: (p) => `No type registered for entry ID ${p.entryId}`,
|
|
67
|
+
fix: "Include a Start control record for this entry before its data records."
|
|
68
|
+
},
|
|
69
|
+
WPILOG_R0007: {
|
|
70
|
+
why: "Failed to lex struct specification",
|
|
71
|
+
fix: "Check the struct specification for invalid characters or tokens."
|
|
72
|
+
},
|
|
73
|
+
WPILOG_R0008: {
|
|
74
|
+
why: "Failed to parse struct specification",
|
|
75
|
+
fix: "Check the struct specification syntax."
|
|
76
|
+
},
|
|
77
|
+
WPILOG_R0009: {
|
|
78
|
+
why: "Enums must be integers",
|
|
79
|
+
fix: "Use an integer type for enum members."
|
|
80
|
+
},
|
|
81
|
+
WPILOG_R0010: {
|
|
82
|
+
why: (p) => `Invalid ${p.width}-bit field for ${p.type} (maximum ${p.maxBits} bits)`,
|
|
83
|
+
fix: (p) => `Use a bit width from 1 to ${p.maxBits}.`
|
|
84
|
+
},
|
|
85
|
+
WPILOG_R0011: {
|
|
86
|
+
why: "Bit-field members must be integers or booleans",
|
|
87
|
+
fix: "Use an integer or boolean type for bit-field members."
|
|
88
|
+
},
|
|
89
|
+
WPILOG_R0012: {
|
|
90
|
+
why: (p) => `Cycle detected in the dependency graph for ${p.name}`,
|
|
91
|
+
fix: "Remove the circular struct reference."
|
|
92
|
+
},
|
|
93
|
+
WPILOG_R0013: {
|
|
94
|
+
why: (p) => `Unknown struct definition: ${p.name}`,
|
|
95
|
+
fix: "Register the struct definition before decoding its records."
|
|
96
|
+
}
|
|
97
|
+
} });
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/types.ts
|
|
100
|
+
/** Control record types in the WPILOG format. */
|
|
101
|
+
const ControlRecordType = {
|
|
102
|
+
Start: 0,
|
|
103
|
+
Finish: 1,
|
|
104
|
+
SetMetadata: 2
|
|
105
|
+
};
|
|
106
|
+
/** Discriminated union tag for decoded record types. */
|
|
107
|
+
const RecordType = {
|
|
108
|
+
Control: "control",
|
|
109
|
+
Boolean: "boolean",
|
|
110
|
+
Int64: "int64",
|
|
111
|
+
Float: "float",
|
|
112
|
+
Double: "double",
|
|
113
|
+
String: "string",
|
|
114
|
+
BooleanArray: "boolean[]",
|
|
115
|
+
Int64Array: "int64[]",
|
|
116
|
+
FloatArray: "float[]",
|
|
117
|
+
DoubleArray: "double[]",
|
|
118
|
+
StringArray: "string[]",
|
|
119
|
+
Struct: "struct",
|
|
120
|
+
StructArray: "struct[]",
|
|
121
|
+
Raw: "raw"
|
|
122
|
+
};
|
|
123
|
+
/** Type guard that narrows a {@link DecodedRecord} to a {@link DataRecord}. */
|
|
124
|
+
function isDataRecord(record) {
|
|
125
|
+
return record.type !== RecordType.Control;
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/read-records.ts
|
|
129
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
130
|
+
const TEXT_DECODER$1 = new TextDecoder();
|
|
131
|
+
const MAGIC = TEXT_ENCODER.encode("WPILOG");
|
|
132
|
+
function readString(payload, view, offset) {
|
|
133
|
+
const length = view.getUint32(offset.get(), true);
|
|
134
|
+
offset.advance32();
|
|
135
|
+
const value = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + length));
|
|
136
|
+
offset.advance(length);
|
|
137
|
+
return value;
|
|
138
|
+
}
|
|
139
|
+
function readControlRecordPayload(payload) {
|
|
140
|
+
const offset = new ByteOffset();
|
|
141
|
+
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
142
|
+
const type = view.getUint8(offset.get());
|
|
143
|
+
offset.advance8();
|
|
144
|
+
switch (type) {
|
|
145
|
+
case ControlRecordType.Start: {
|
|
146
|
+
const entryId = view.getUint32(offset.get(), true);
|
|
147
|
+
offset.advance32();
|
|
148
|
+
return {
|
|
149
|
+
controlRecordType: type,
|
|
150
|
+
entryId,
|
|
151
|
+
entryName: readString(payload, view, offset),
|
|
152
|
+
entryType: readString(payload, view, offset),
|
|
153
|
+
entryMetadata: readString(payload, view, offset)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
case ControlRecordType.Finish: {
|
|
157
|
+
const entryId = view.getUint32(offset.get(), true);
|
|
158
|
+
offset.advance32();
|
|
159
|
+
return {
|
|
160
|
+
controlRecordType: type,
|
|
161
|
+
entryId
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
case ControlRecordType.SetMetadata: {
|
|
165
|
+
const entryId = view.getUint32(offset.get(), true);
|
|
166
|
+
offset.advance32();
|
|
167
|
+
return {
|
|
168
|
+
controlRecordType: type,
|
|
169
|
+
entryId,
|
|
170
|
+
entryMetadata: readString(payload, view, offset)
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
default: throw diagnostics.WPILOG_R0001({ type });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function toUint8Array(input) {
|
|
177
|
+
if (input instanceof Uint8Array) return input;
|
|
178
|
+
return new Uint8Array(input);
|
|
179
|
+
}
|
|
180
|
+
function openDataLog(input) {
|
|
181
|
+
const bytes = toUint8Array(input);
|
|
182
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
183
|
+
if (bytes.byteLength < 12) throw diagnostics.WPILOG_R0002();
|
|
184
|
+
for (let i = 0; i < MAGIC.byteLength; i++) if (bytes[i] !== MAGIC[i]) throw diagnostics.WPILOG_R0003();
|
|
185
|
+
const recordsOffset = 12 + view.getUint32(8, true);
|
|
186
|
+
if (recordsOffset > bytes.byteLength) throw diagnostics.WPILOG_R0004();
|
|
187
|
+
return {
|
|
188
|
+
bytes,
|
|
189
|
+
view,
|
|
190
|
+
header: {
|
|
191
|
+
version: {
|
|
192
|
+
major: view.getUint8(7),
|
|
193
|
+
minor: view.getUint8(6)
|
|
194
|
+
},
|
|
195
|
+
extraHeader: TEXT_DECODER$1.decode(bytes.subarray(12, recordsOffset))
|
|
196
|
+
},
|
|
197
|
+
recordsOffset
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function readVarInt(view, offset, length) {
|
|
201
|
+
switch (length) {
|
|
202
|
+
case 1: return view.getUint8(offset);
|
|
203
|
+
case 2: return view.getUint16(offset, true);
|
|
204
|
+
case 3: return view.getUint8(offset + 2) << 16 | view.getUint8(offset + 1) << 8 | view.getUint8(offset);
|
|
205
|
+
case 4: return view.getUint32(offset, true);
|
|
206
|
+
default: throw new RangeError(`Invalid varint length ${length}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function readTimestamp(view, offset, length) {
|
|
210
|
+
switch (length) {
|
|
211
|
+
case 1: return BigInt(view.getUint8(offset));
|
|
212
|
+
case 2: return BigInt(view.getUint16(offset, true));
|
|
213
|
+
case 3: {
|
|
214
|
+
const b0 = BigInt(view.getUint8(offset));
|
|
215
|
+
const b1 = BigInt(view.getUint8(offset + 1));
|
|
216
|
+
return BigInt(view.getUint8(offset + 2)) << 16n | b1 << 8n | b0;
|
|
217
|
+
}
|
|
218
|
+
case 4: return BigInt(view.getUint32(offset, true));
|
|
219
|
+
case 5: {
|
|
220
|
+
const b0 = BigInt(view.getUint8(offset));
|
|
221
|
+
const b1 = BigInt(view.getUint8(offset + 1));
|
|
222
|
+
const b2 = BigInt(view.getUint8(offset + 2));
|
|
223
|
+
const b3 = BigInt(view.getUint8(offset + 3));
|
|
224
|
+
return BigInt(view.getUint8(offset + 4)) << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
|
|
225
|
+
}
|
|
226
|
+
case 6: {
|
|
227
|
+
const b0 = BigInt(view.getUint8(offset));
|
|
228
|
+
const b1 = BigInt(view.getUint8(offset + 1));
|
|
229
|
+
const b2 = BigInt(view.getUint8(offset + 2));
|
|
230
|
+
const b3 = BigInt(view.getUint8(offset + 3));
|
|
231
|
+
const b4 = BigInt(view.getUint8(offset + 4));
|
|
232
|
+
return BigInt(view.getUint8(offset + 5)) << 40n | b4 << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
|
|
233
|
+
}
|
|
234
|
+
case 7: {
|
|
235
|
+
const b0 = BigInt(view.getUint8(offset));
|
|
236
|
+
const b1 = BigInt(view.getUint8(offset + 1));
|
|
237
|
+
const b2 = BigInt(view.getUint8(offset + 2));
|
|
238
|
+
const b3 = BigInt(view.getUint8(offset + 3));
|
|
239
|
+
const b4 = BigInt(view.getUint8(offset + 4));
|
|
240
|
+
const b5 = BigInt(view.getUint8(offset + 5));
|
|
241
|
+
return BigInt(view.getUint8(offset + 6)) << 48n | b5 << 40n | b4 << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
|
|
242
|
+
}
|
|
243
|
+
case 8: return view.getBigUint64(offset, true);
|
|
244
|
+
default: throw new RangeError(`Invalid timestamp length ${length}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/** Mutable record cursor used by the fused decoder to avoid intermediate record allocations. */
|
|
248
|
+
var RecordCursor = class {
|
|
249
|
+
bytes;
|
|
250
|
+
view;
|
|
251
|
+
header;
|
|
252
|
+
entryId = 0;
|
|
253
|
+
timestamp = 0n;
|
|
254
|
+
payloadOffset = 0;
|
|
255
|
+
payloadSize = 0;
|
|
256
|
+
offset;
|
|
257
|
+
constructor(input) {
|
|
258
|
+
const source = openDataLog(input);
|
|
259
|
+
this.bytes = source.bytes;
|
|
260
|
+
this.view = source.view;
|
|
261
|
+
this.header = source.header;
|
|
262
|
+
this.offset = source.recordsOffset;
|
|
263
|
+
}
|
|
264
|
+
next() {
|
|
265
|
+
const total = this.bytes.byteLength;
|
|
266
|
+
let offset = this.offset;
|
|
267
|
+
if (offset >= total) return false;
|
|
268
|
+
const view = this.view;
|
|
269
|
+
const bitfield = view.getUint8(offset++);
|
|
270
|
+
const entryIdLength = 1 + (bitfield & 3);
|
|
271
|
+
const payloadSizeLength = 1 + (bitfield >> 2 & 3);
|
|
272
|
+
const timestampLength = 1 + (bitfield >> 4 & 7);
|
|
273
|
+
if (offset + entryIdLength + payloadSizeLength + timestampLength > total) {
|
|
274
|
+
this.offset = total;
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
const entryId = readVarInt(view, offset, entryIdLength);
|
|
278
|
+
offset += entryIdLength;
|
|
279
|
+
const payloadSize = readVarInt(view, offset, payloadSizeLength);
|
|
280
|
+
offset += payloadSizeLength;
|
|
281
|
+
const timestamp = readTimestamp(view, offset, timestampLength);
|
|
282
|
+
offset += timestampLength;
|
|
283
|
+
if (offset + payloadSize > total) {
|
|
284
|
+
this.offset = total;
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
this.entryId = entryId;
|
|
288
|
+
this.timestamp = timestamp;
|
|
289
|
+
this.payloadOffset = offset;
|
|
290
|
+
this.payloadSize = payloadSize;
|
|
291
|
+
this.offset = offset + payloadSize;
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
getPayload() {
|
|
295
|
+
return this.bytes.subarray(this.payloadOffset, this.payloadOffset + this.payloadSize);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
/**
|
|
299
|
+
* Read raw WPILOG records from an in-memory buffer.
|
|
300
|
+
*
|
|
301
|
+
* Yields a header record first, then control and data records in order.
|
|
302
|
+
* Data record payloads are not decoded. Prefer `parseDataLog` when raw records
|
|
303
|
+
* or an intermediate transformation are not needed.
|
|
304
|
+
*/
|
|
305
|
+
function* readRecords(input) {
|
|
306
|
+
const { bytes, view, header, recordsOffset } = openDataLog(input);
|
|
307
|
+
const total = bytes.byteLength;
|
|
308
|
+
let offset = recordsOffset;
|
|
309
|
+
yield {
|
|
310
|
+
kind: "header",
|
|
311
|
+
header
|
|
312
|
+
};
|
|
313
|
+
while (offset < total) {
|
|
314
|
+
const bitfield = view.getUint8(offset++);
|
|
315
|
+
const entryIdLength = 1 + (bitfield & 3);
|
|
316
|
+
const payloadSizeLength = 1 + (bitfield >> 2 & 3);
|
|
317
|
+
const timestampLength = 1 + (bitfield >> 4 & 7);
|
|
318
|
+
if (offset + entryIdLength + payloadSizeLength + timestampLength > total) return;
|
|
319
|
+
const entryId = readVarInt(view, offset, entryIdLength);
|
|
320
|
+
offset += entryIdLength;
|
|
321
|
+
const payloadSize = readVarInt(view, offset, payloadSizeLength);
|
|
322
|
+
offset += payloadSizeLength;
|
|
323
|
+
const timestamp = readTimestamp(view, offset, timestampLength);
|
|
324
|
+
offset += timestampLength;
|
|
325
|
+
if (offset + payloadSize > total) return;
|
|
326
|
+
const payload = bytes.subarray(offset, offset + payloadSize);
|
|
327
|
+
offset += payloadSize;
|
|
328
|
+
if (entryId === 0) yield {
|
|
329
|
+
kind: "control",
|
|
330
|
+
entryId,
|
|
331
|
+
timestamp,
|
|
332
|
+
payload: readControlRecordPayload(payload)
|
|
333
|
+
};
|
|
334
|
+
else yield {
|
|
335
|
+
kind: "data",
|
|
336
|
+
record: {
|
|
337
|
+
entryId,
|
|
338
|
+
timestamp,
|
|
339
|
+
payload
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
//#endregion
|
|
345
|
+
//#region src/struct/types.ts
|
|
346
|
+
const KnownStructTypeName = {
|
|
347
|
+
Boolean: "bool",
|
|
348
|
+
Character: "char",
|
|
349
|
+
Int8: "int8",
|
|
350
|
+
Int16: "int16",
|
|
351
|
+
Int32: "int32",
|
|
352
|
+
Int64: "int64",
|
|
353
|
+
Uint8: "uint8",
|
|
354
|
+
Uint16: "uint16",
|
|
355
|
+
Uint32: "uint32",
|
|
356
|
+
Uint64: "uint64",
|
|
357
|
+
Float32: "float32",
|
|
358
|
+
/** Equivalent to {@link KnownStructTypeName.Float32} */
|
|
359
|
+
Float: "float",
|
|
360
|
+
Float64: "float64",
|
|
361
|
+
/** Equivalent to {@link KnownStructTypeName.Float64} */
|
|
362
|
+
Double: "double"
|
|
363
|
+
};
|
|
364
|
+
//#endregion
|
|
365
|
+
//#region src/struct/struct-dependency-graph.ts
|
|
366
|
+
const EMPTY_SET = /* @__PURE__ */ new Set();
|
|
367
|
+
var StructDependencyGraph = class {
|
|
368
|
+
allDependencies = new Map(Object.values(KnownStructTypeName).map((type) => [type, EMPTY_SET]));
|
|
369
|
+
registerSchema(name, innerStructNames) {
|
|
370
|
+
const existing = this.allDependencies.get(name);
|
|
371
|
+
if (existing) for (const innerStructName of innerStructNames) existing.add(innerStructName);
|
|
372
|
+
else this.allDependencies.set(name, new Set(innerStructNames));
|
|
373
|
+
if (this.hasCycle(name)) throw diagnostics.WPILOG_R0012({ name });
|
|
374
|
+
}
|
|
375
|
+
hasCycle(current, path = /* @__PURE__ */ new Set()) {
|
|
376
|
+
if (path.has(current)) return true;
|
|
377
|
+
path.add(current);
|
|
378
|
+
const directDependencies = this.allDependencies.get(current);
|
|
379
|
+
if (directDependencies) {
|
|
380
|
+
for (const dep of directDependencies) if (this.hasCycle(dep, path)) return true;
|
|
381
|
+
}
|
|
382
|
+
path.delete(current);
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
getDependencies(name) {
|
|
386
|
+
const result = /* @__PURE__ */ new Set();
|
|
387
|
+
const queue = [name];
|
|
388
|
+
while (queue.length > 0) {
|
|
389
|
+
const current = queue.pop();
|
|
390
|
+
if (result.has(current)) continue;
|
|
391
|
+
result.add(current);
|
|
392
|
+
const dependencies = this.allDependencies.get(current);
|
|
393
|
+
if (dependencies) queue.push(...dependencies);
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
getDecodableStructs() {
|
|
398
|
+
const result = [];
|
|
399
|
+
for (const structName of this.allDependencies.keys()) {
|
|
400
|
+
const dependencies = this.getDependencies(structName);
|
|
401
|
+
let allResolved = true;
|
|
402
|
+
for (const dep of dependencies) if (!this.allDependencies.has(dep)) {
|
|
403
|
+
allResolved = false;
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
if (allResolved) result.push(structName);
|
|
407
|
+
}
|
|
408
|
+
return result;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
//#endregion
|
|
412
|
+
//#region src/struct/struct-decode-queue.ts
|
|
413
|
+
var StructDecodeQueue = class {
|
|
414
|
+
graph = new StructDependencyGraph();
|
|
415
|
+
queuedRecords = /* @__PURE__ */ new Map();
|
|
416
|
+
onStructDefined;
|
|
417
|
+
constructor(onStructDefined) {
|
|
418
|
+
this.onStructDefined = onStructDefined;
|
|
419
|
+
}
|
|
420
|
+
queueStructRecord(structName, record) {
|
|
421
|
+
const existing = this.queuedRecords.get(structName);
|
|
422
|
+
if (existing) existing.push(record);
|
|
423
|
+
else this.queuedRecords.set(structName, [record]);
|
|
424
|
+
}
|
|
425
|
+
registerSchema(structName, innerStructNames) {
|
|
426
|
+
this.graph.registerSchema(structName, innerStructNames);
|
|
427
|
+
this.checkDecodableStructs();
|
|
428
|
+
}
|
|
429
|
+
checkDecodableStructs() {
|
|
430
|
+
for (const structName of this.graph.getDecodableStructs()) {
|
|
431
|
+
const records = this.queuedRecords.get(structName);
|
|
432
|
+
if (records) {
|
|
433
|
+
this.onStructDefined(structName, records);
|
|
434
|
+
this.queuedRecords.delete(structName);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/struct/bit-field.ts
|
|
441
|
+
var BitFieldTracker = class {
|
|
442
|
+
storageByteLength = 0;
|
|
443
|
+
bitShift = 0;
|
|
444
|
+
next(member, memberByteLength) {
|
|
445
|
+
const bitWidth = member.bitWidth;
|
|
446
|
+
if (bitWidth === void 0 || bitWidth === 0 || bitWidth === memberByteLength * 8) return;
|
|
447
|
+
let bytesToAdvance = 0;
|
|
448
|
+
let storageByteLength = memberByteLength;
|
|
449
|
+
if (member.value === KnownStructTypeName.Boolean && this.storageByteLength !== 0 && this.bitShift + 1 <= this.storageByteLength * 8) storageByteLength = this.storageByteLength;
|
|
450
|
+
else if (storageByteLength !== this.storageByteLength || this.bitShift + bitWidth > storageByteLength * 8) bytesToAdvance = this.flush();
|
|
451
|
+
const layout = {
|
|
452
|
+
bytesToAdvance,
|
|
453
|
+
storageByteLength,
|
|
454
|
+
bitShift: this.bitShift,
|
|
455
|
+
bitWidth
|
|
456
|
+
};
|
|
457
|
+
this.storageByteLength = storageByteLength;
|
|
458
|
+
this.bitShift += bitWidth;
|
|
459
|
+
return layout;
|
|
460
|
+
}
|
|
461
|
+
flush() {
|
|
462
|
+
const bytesToAdvance = this.storageByteLength;
|
|
463
|
+
this.storageByteLength = 0;
|
|
464
|
+
this.bitShift = 0;
|
|
465
|
+
return bytesToAdvance;
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
function decodeBitField(view, offset, member, layout) {
|
|
469
|
+
let value = readUnsignedInteger(view, offset, layout.storageByteLength);
|
|
470
|
+
value = value >> BigInt(layout.bitShift) & (1n << BigInt(layout.bitWidth)) - 1n;
|
|
471
|
+
if (member.value === KnownStructTypeName.Boolean) return value !== 0n;
|
|
472
|
+
if (isSignedInteger(member.value) && (value & 1n << BigInt(layout.bitWidth - 1)) !== 0n) value -= 1n << BigInt(layout.bitWidth);
|
|
473
|
+
return member.value === KnownStructTypeName.Int64 || member.value === KnownStructTypeName.Uint64 ? value : Number(value);
|
|
474
|
+
}
|
|
475
|
+
function readUnsignedInteger(view, offset, byteLength) {
|
|
476
|
+
switch (byteLength) {
|
|
477
|
+
case 1: return BigInt(view.getUint8(offset));
|
|
478
|
+
case 2: return BigInt(view.getUint16(offset, true));
|
|
479
|
+
case 4: return BigInt(view.getUint32(offset, true));
|
|
480
|
+
case 8: return view.getBigUint64(offset, true);
|
|
481
|
+
default: throw new TypeError(`Invalid bit-field storage size: ${byteLength}`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
function isSignedInteger(type) {
|
|
485
|
+
switch (type) {
|
|
486
|
+
case KnownStructTypeName.Int8:
|
|
487
|
+
case KnownStructTypeName.Int16:
|
|
488
|
+
case KnownStructTypeName.Int32:
|
|
489
|
+
case KnownStructTypeName.Int64: return true;
|
|
490
|
+
default: return false;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
//#endregion
|
|
494
|
+
//#region src/struct/lexer.ts
|
|
495
|
+
const Comma = createToken({
|
|
496
|
+
name: "Comma",
|
|
497
|
+
pattern: ","
|
|
498
|
+
});
|
|
499
|
+
const Semicolon = createToken({
|
|
500
|
+
name: "Semicolon",
|
|
501
|
+
pattern: ";"
|
|
502
|
+
});
|
|
503
|
+
const Colon = createToken({
|
|
504
|
+
name: "Colon",
|
|
505
|
+
pattern: ":"
|
|
506
|
+
});
|
|
507
|
+
const Equals = createToken({
|
|
508
|
+
name: "Equals",
|
|
509
|
+
pattern: "="
|
|
510
|
+
});
|
|
511
|
+
const WhiteSpace = createToken({
|
|
512
|
+
name: "WhiteSpace",
|
|
513
|
+
pattern: /\s+/
|
|
514
|
+
});
|
|
515
|
+
const Integer = createToken({
|
|
516
|
+
name: "Integer",
|
|
517
|
+
pattern: /0|[1-9]\d*/
|
|
518
|
+
});
|
|
519
|
+
const LeftSquareBrace = createToken({
|
|
520
|
+
name: "LeftSquareBrace",
|
|
521
|
+
pattern: "["
|
|
522
|
+
});
|
|
523
|
+
const RightSquareBrace = createToken({
|
|
524
|
+
name: "RightSquareBrace",
|
|
525
|
+
pattern: "]"
|
|
526
|
+
});
|
|
527
|
+
const EnumKeyword = createToken({
|
|
528
|
+
name: "EnumKeyword",
|
|
529
|
+
pattern: "enum"
|
|
530
|
+
});
|
|
531
|
+
const LeftCurlyBrace = createToken({
|
|
532
|
+
name: "LeftCurlyBrace",
|
|
533
|
+
pattern: "{"
|
|
534
|
+
});
|
|
535
|
+
const RightCurlyBrace = createToken({
|
|
536
|
+
name: "RightCurlyBrace",
|
|
537
|
+
pattern: "}"
|
|
538
|
+
});
|
|
539
|
+
const Identifier = createToken({
|
|
540
|
+
name: "Identifier",
|
|
541
|
+
pattern: /\w+/
|
|
542
|
+
});
|
|
543
|
+
const allTokens = [
|
|
544
|
+
WhiteSpace,
|
|
545
|
+
Semicolon,
|
|
546
|
+
Equals,
|
|
547
|
+
Comma,
|
|
548
|
+
Colon,
|
|
549
|
+
Integer,
|
|
550
|
+
LeftSquareBrace,
|
|
551
|
+
RightSquareBrace,
|
|
552
|
+
EnumKeyword,
|
|
553
|
+
LeftCurlyBrace,
|
|
554
|
+
RightCurlyBrace,
|
|
555
|
+
Identifier
|
|
556
|
+
];
|
|
557
|
+
const lexer = new Lexer(allTokens);
|
|
558
|
+
//#endregion
|
|
559
|
+
//#region src/struct/parser.ts
|
|
560
|
+
var StructParser = class extends CstParser {
|
|
561
|
+
structSpecification = this.RULE("structSpecification", () => {
|
|
562
|
+
this.MANY_SEP({
|
|
563
|
+
SEP: Semicolon,
|
|
564
|
+
DEF: () => this.OPTION(() => this.SUBRULE(this.declaration))
|
|
565
|
+
});
|
|
566
|
+
});
|
|
567
|
+
declaration = this.RULE("declaration", () => {
|
|
568
|
+
this.OPTION(() => this.SUBRULE(this.enumSpecification));
|
|
569
|
+
this.SUBRULE(this.optionalWhitespace);
|
|
570
|
+
this.CONSUME(Identifier);
|
|
571
|
+
this.SUBRULE1(this.optionalWhitespace);
|
|
572
|
+
this.CONSUME1(Identifier);
|
|
573
|
+
this.OPTION1(() => this.OR({ DEF: [{ ALT: () => this.SUBRULE(this.bitFieldDeclaration) }, { ALT: () => this.SUBRULE(this.standardDeclarationArray) }] }));
|
|
574
|
+
});
|
|
575
|
+
standardDeclarationArray = this.RULE("standardDeclarationArray", () => {
|
|
576
|
+
this.SUBRULE(this.optionalWhitespace);
|
|
577
|
+
this.SUBRULE(this.arraySize);
|
|
578
|
+
});
|
|
579
|
+
bitFieldDeclaration = this.RULE("bitFieldDeclaration", () => {
|
|
580
|
+
this.SUBRULE(this.optionalWhitespace);
|
|
581
|
+
this.CONSUME(Colon);
|
|
582
|
+
this.SUBRULE1(this.optionalWhitespace);
|
|
583
|
+
this.CONSUME(Integer);
|
|
584
|
+
});
|
|
585
|
+
arraySize = this.RULE("arraySize", () => {
|
|
586
|
+
this.CONSUME(LeftSquareBrace);
|
|
587
|
+
this.SUBRULE(this.optionalWhitespace);
|
|
588
|
+
this.CONSUME(Integer);
|
|
589
|
+
this.SUBRULE1(this.optionalWhitespace);
|
|
590
|
+
this.CONSUME(RightSquareBrace);
|
|
591
|
+
});
|
|
592
|
+
enumSpecification = this.RULE("enumSpecification", () => {
|
|
593
|
+
this.OPTION(() => this.CONSUME(EnumKeyword));
|
|
594
|
+
this.SUBRULE(this.optionalWhitespace);
|
|
595
|
+
this.CONSUME(LeftCurlyBrace);
|
|
596
|
+
this.MANY_SEP({
|
|
597
|
+
SEP: Comma,
|
|
598
|
+
DEF: () => this.SUBRULE(this.enumMember)
|
|
599
|
+
});
|
|
600
|
+
this.SUBRULE1(this.optionalWhitespace);
|
|
601
|
+
this.CONSUME(RightCurlyBrace);
|
|
602
|
+
});
|
|
603
|
+
enumMember = this.RULE("enumMember", () => {
|
|
604
|
+
this.SUBRULE(this.optionalWhitespace);
|
|
605
|
+
this.CONSUME(Identifier);
|
|
606
|
+
this.SUBRULE1(this.optionalWhitespace);
|
|
607
|
+
this.CONSUME(Equals);
|
|
608
|
+
this.SUBRULE2(this.optionalWhitespace);
|
|
609
|
+
this.CONSUME(Integer);
|
|
610
|
+
this.SUBRULE3(this.optionalWhitespace);
|
|
611
|
+
});
|
|
612
|
+
optionalWhitespace = this.RULE("optionalWhitespace", () => {
|
|
613
|
+
this.OPTION(() => this.CONSUME(WhiteSpace));
|
|
614
|
+
});
|
|
615
|
+
constructor() {
|
|
616
|
+
super(allTokens);
|
|
617
|
+
this.performSelfAnalysis();
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
const parser = new StructParser();
|
|
621
|
+
parser.getBaseCstVisitorConstructor();
|
|
622
|
+
const BaseStructVisitorWithDefaults = parser.getBaseCstVisitorConstructorWithDefaults();
|
|
623
|
+
//#endregion
|
|
624
|
+
//#region src/struct/struct-ast-visitor.ts
|
|
625
|
+
var StructAstVisitor = class extends BaseStructVisitorWithDefaults {
|
|
626
|
+
constructor() {
|
|
627
|
+
super();
|
|
628
|
+
this.validateVisitor();
|
|
629
|
+
}
|
|
630
|
+
structSpecification(children) {
|
|
631
|
+
return children.declaration?.map((declaration) => this.declaration(declaration.children)) ?? [];
|
|
632
|
+
}
|
|
633
|
+
declaration(children) {
|
|
634
|
+
const [typeName, name] = children.Identifier;
|
|
635
|
+
const enumSpecification = children.enumSpecification ? this.enumSpecification(children.enumSpecification[0].children, typeName.image) : void 0;
|
|
636
|
+
const arraySize = children.standardDeclarationArray ? this.standardDeclarationArray(children.standardDeclarationArray[0].children) : void 0;
|
|
637
|
+
const bitWidth = children.bitFieldDeclaration ? this.bitFieldDeclaration(children.bitFieldDeclaration[0].children, typeName.image) : void 0;
|
|
638
|
+
return {
|
|
639
|
+
name: name.image,
|
|
640
|
+
value: typeName.image,
|
|
641
|
+
enumSpecification,
|
|
642
|
+
arraySize,
|
|
643
|
+
bitWidth
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
enumSpecification(children, dataType) {
|
|
647
|
+
switch (dataType) {
|
|
648
|
+
case KnownStructTypeName.Int8:
|
|
649
|
+
case KnownStructTypeName.Uint8:
|
|
650
|
+
case KnownStructTypeName.Int16:
|
|
651
|
+
case KnownStructTypeName.Uint16:
|
|
652
|
+
case KnownStructTypeName.Int32:
|
|
653
|
+
case KnownStructTypeName.Uint32:
|
|
654
|
+
case KnownStructTypeName.Int64:
|
|
655
|
+
case KnownStructTypeName.Uint64: break;
|
|
656
|
+
default: throw diagnostics.WPILOG_R0009();
|
|
657
|
+
}
|
|
658
|
+
const entries = children.enumMember?.map((member) => this.enumMember(member.children));
|
|
659
|
+
return new Map(entries);
|
|
660
|
+
}
|
|
661
|
+
enumMember(children) {
|
|
662
|
+
const [name] = children.Identifier;
|
|
663
|
+
const [value] = children.Integer;
|
|
664
|
+
return [name.image, BigInt(value.image)];
|
|
665
|
+
}
|
|
666
|
+
bitFieldDeclaration(children, dataType) {
|
|
667
|
+
const [value] = children.Integer;
|
|
668
|
+
const parsed = Number(value.image);
|
|
669
|
+
switch (dataType) {
|
|
670
|
+
case KnownStructTypeName.Boolean:
|
|
671
|
+
if (parsed !== 1) throw diagnostics.WPILOG_R0010({
|
|
672
|
+
type: dataType,
|
|
673
|
+
width: parsed,
|
|
674
|
+
maxBits: 1
|
|
675
|
+
});
|
|
676
|
+
break;
|
|
677
|
+
case KnownStructTypeName.Int8:
|
|
678
|
+
case KnownStructTypeName.Uint8:
|
|
679
|
+
if (parsed > 8) throw diagnostics.WPILOG_R0010({
|
|
680
|
+
type: dataType,
|
|
681
|
+
width: parsed,
|
|
682
|
+
maxBits: 8
|
|
683
|
+
});
|
|
684
|
+
break;
|
|
685
|
+
case KnownStructTypeName.Int16:
|
|
686
|
+
case KnownStructTypeName.Uint16:
|
|
687
|
+
if (parsed > 16) throw diagnostics.WPILOG_R0010({
|
|
688
|
+
type: dataType,
|
|
689
|
+
width: parsed,
|
|
690
|
+
maxBits: 16
|
|
691
|
+
});
|
|
692
|
+
break;
|
|
693
|
+
case KnownStructTypeName.Int32:
|
|
694
|
+
case KnownStructTypeName.Uint32:
|
|
695
|
+
if (parsed > 32) throw diagnostics.WPILOG_R0010({
|
|
696
|
+
type: dataType,
|
|
697
|
+
width: parsed,
|
|
698
|
+
maxBits: 32
|
|
699
|
+
});
|
|
700
|
+
break;
|
|
701
|
+
case KnownStructTypeName.Int64:
|
|
702
|
+
case KnownStructTypeName.Uint64:
|
|
703
|
+
if (parsed > 64) throw diagnostics.WPILOG_R0010({
|
|
704
|
+
type: dataType,
|
|
705
|
+
width: parsed,
|
|
706
|
+
maxBits: 64
|
|
707
|
+
});
|
|
708
|
+
break;
|
|
709
|
+
default: throw diagnostics.WPILOG_R0011();
|
|
710
|
+
}
|
|
711
|
+
return parsed;
|
|
712
|
+
}
|
|
713
|
+
standardDeclarationArray(children) {
|
|
714
|
+
return this.arraySize(children.arraySize[0].children);
|
|
715
|
+
}
|
|
716
|
+
arraySize(children) {
|
|
717
|
+
const [value] = children.Integer;
|
|
718
|
+
return Number(value.image);
|
|
719
|
+
}
|
|
720
|
+
};
|
|
721
|
+
//#endregion
|
|
722
|
+
//#region src/struct/parse-struct.ts
|
|
723
|
+
const structAstVisitor = new StructAstVisitor();
|
|
724
|
+
const cache = /* @__PURE__ */ new Map();
|
|
725
|
+
/** Parse a WPILib struct specification string into declarations. */
|
|
726
|
+
function parseStructSpecification(declaration) {
|
|
727
|
+
const existing = cache.get(declaration);
|
|
728
|
+
if (existing) return existing;
|
|
729
|
+
const lexingResult = lexer.tokenize(declaration);
|
|
730
|
+
if (lexingResult.errors.length > 0) throw diagnostics.WPILOG_R0007({ cause: new AggregateError(lexingResult.errors) });
|
|
731
|
+
parser.input = lexingResult.tokens;
|
|
732
|
+
const cstNode = parser.structSpecification();
|
|
733
|
+
if (parser.errors.length > 0) throw diagnostics.WPILOG_R0008({ cause: new AggregateError(parser.errors) });
|
|
734
|
+
const created = structAstVisitor.structSpecification(cstNode.children);
|
|
735
|
+
cache.set(declaration, created);
|
|
736
|
+
return created;
|
|
737
|
+
}
|
|
738
|
+
//#endregion
|
|
739
|
+
//#region src/struct/struct-registry.ts
|
|
740
|
+
var StructRegistry = class StructRegistry {
|
|
741
|
+
static TEXT_DECODER = new TextDecoder("utf-8");
|
|
742
|
+
definitions = /* @__PURE__ */ new Map();
|
|
743
|
+
byteLengths = /* @__PURE__ */ new Map();
|
|
744
|
+
structDecodeQueue;
|
|
745
|
+
constructor(structDecodeQueue) {
|
|
746
|
+
this.structDecodeQueue = structDecodeQueue;
|
|
747
|
+
}
|
|
748
|
+
register(name, definition) {
|
|
749
|
+
const specification = parseStructSpecification(definition);
|
|
750
|
+
this.definitions.set(name, specification);
|
|
751
|
+
this.structDecodeQueue.registerSchema(name, specification.map((member) => member.value));
|
|
752
|
+
}
|
|
753
|
+
getDefinition(name) {
|
|
754
|
+
const definition = this.definitions.get(name);
|
|
755
|
+
if (!definition) throw diagnostics.WPILOG_R0013({ name });
|
|
756
|
+
return definition;
|
|
757
|
+
}
|
|
758
|
+
decodeArray(structName, payload) {
|
|
759
|
+
const structNameWithoutSuffix = structName.slice(0, -2);
|
|
760
|
+
const structByteLengthOrBlocker = this.getByteLength(structNameWithoutSuffix);
|
|
761
|
+
if (typeof structByteLengthOrBlocker === "string") return structByteLengthOrBlocker;
|
|
762
|
+
const elements = payload.byteLength / structByteLengthOrBlocker;
|
|
763
|
+
const offset = new ByteOffset();
|
|
764
|
+
const result = [];
|
|
765
|
+
for (let i = 0; i < elements; i++) {
|
|
766
|
+
const decoded = this.decode(structNameWithoutSuffix, payload, offset);
|
|
767
|
+
if (typeof decoded === "string") throw new TypeError(`Expected struct ${structNameWithoutSuffix} to be defined if the byte length calculation succeeded`);
|
|
768
|
+
result.push(decoded);
|
|
769
|
+
}
|
|
770
|
+
return result;
|
|
771
|
+
}
|
|
772
|
+
decode(structName, payload, offset = new ByteOffset()) {
|
|
773
|
+
if (!this.definitions.has(structName)) return structName;
|
|
774
|
+
const specification = this.getDefinition(structName);
|
|
775
|
+
const result = /* @__PURE__ */ new Map();
|
|
776
|
+
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
777
|
+
const bitFields = new BitFieldTracker();
|
|
778
|
+
for (const member of specification) {
|
|
779
|
+
const memberByteLength = this.calculateByteLength(member);
|
|
780
|
+
const bitField = typeof memberByteLength === "number" ? bitFields.next(member, memberByteLength) : void 0;
|
|
781
|
+
if (bitField) {
|
|
782
|
+
offset.advance(bitField.bytesToAdvance);
|
|
783
|
+
result.set(member.name, decodeBitField(view, offset.get(), member, bitField));
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
offset.advance(bitFields.flush());
|
|
787
|
+
switch (member.value) {
|
|
788
|
+
case KnownStructTypeName.Boolean:
|
|
789
|
+
if (member.arraySize !== void 0) {
|
|
790
|
+
const array = [];
|
|
791
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
792
|
+
array.push(Boolean(view.getUint8(offset.get())));
|
|
793
|
+
offset.advance8();
|
|
794
|
+
}
|
|
795
|
+
result.set(member.name, array);
|
|
796
|
+
} else {
|
|
797
|
+
result.set(member.name, Boolean(view.getUint8(offset.get())));
|
|
798
|
+
offset.advance8();
|
|
799
|
+
}
|
|
800
|
+
break;
|
|
801
|
+
case KnownStructTypeName.Int8:
|
|
802
|
+
if (member.arraySize !== void 0) {
|
|
803
|
+
const array = [];
|
|
804
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
805
|
+
array.push(view.getInt8(offset.get()));
|
|
806
|
+
offset.advance8();
|
|
807
|
+
}
|
|
808
|
+
result.set(member.name, array);
|
|
809
|
+
} else {
|
|
810
|
+
result.set(member.name, view.getInt8(offset.get()));
|
|
811
|
+
offset.advance8();
|
|
812
|
+
}
|
|
813
|
+
break;
|
|
814
|
+
case KnownStructTypeName.Int16:
|
|
815
|
+
if (member.arraySize !== void 0) {
|
|
816
|
+
const array = [];
|
|
817
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
818
|
+
array.push(view.getInt16(offset.get(), true));
|
|
819
|
+
offset.advance16();
|
|
820
|
+
}
|
|
821
|
+
result.set(member.name, array);
|
|
822
|
+
} else {
|
|
823
|
+
result.set(member.name, view.getInt16(offset.get(), true));
|
|
824
|
+
offset.advance16();
|
|
825
|
+
}
|
|
826
|
+
break;
|
|
827
|
+
case KnownStructTypeName.Int32:
|
|
828
|
+
if (member.arraySize !== void 0) {
|
|
829
|
+
const array = [];
|
|
830
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
831
|
+
array.push(view.getInt32(offset.get(), true));
|
|
832
|
+
offset.advance32();
|
|
833
|
+
}
|
|
834
|
+
result.set(member.name, array);
|
|
835
|
+
} else {
|
|
836
|
+
result.set(member.name, view.getInt32(offset.get(), true));
|
|
837
|
+
offset.advance32();
|
|
838
|
+
}
|
|
839
|
+
break;
|
|
840
|
+
case KnownStructTypeName.Int64:
|
|
841
|
+
if (member.arraySize !== void 0) {
|
|
842
|
+
const array = [];
|
|
843
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
844
|
+
array.push(view.getBigInt64(offset.get(), true));
|
|
845
|
+
offset.advance64();
|
|
846
|
+
}
|
|
847
|
+
result.set(member.name, array);
|
|
848
|
+
} else {
|
|
849
|
+
result.set(member.name, view.getBigInt64(offset.get(), true));
|
|
850
|
+
offset.advance64();
|
|
851
|
+
}
|
|
852
|
+
break;
|
|
853
|
+
case KnownStructTypeName.Uint8:
|
|
854
|
+
if (member.arraySize !== void 0) {
|
|
855
|
+
const array = [];
|
|
856
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
857
|
+
array.push(view.getUint8(offset.get()));
|
|
858
|
+
offset.advance8();
|
|
859
|
+
}
|
|
860
|
+
result.set(member.name, array);
|
|
861
|
+
} else {
|
|
862
|
+
result.set(member.name, view.getUint8(offset.get()));
|
|
863
|
+
offset.advance8();
|
|
864
|
+
}
|
|
865
|
+
break;
|
|
866
|
+
case KnownStructTypeName.Uint16:
|
|
867
|
+
if (member.arraySize !== void 0) {
|
|
868
|
+
const array = [];
|
|
869
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
870
|
+
array.push(view.getUint16(offset.get(), true));
|
|
871
|
+
offset.advance16();
|
|
872
|
+
}
|
|
873
|
+
result.set(member.name, array);
|
|
874
|
+
} else {
|
|
875
|
+
result.set(member.name, view.getUint16(offset.get(), true));
|
|
876
|
+
offset.advance16();
|
|
877
|
+
}
|
|
878
|
+
break;
|
|
879
|
+
case KnownStructTypeName.Uint32:
|
|
880
|
+
if (member.arraySize !== void 0) {
|
|
881
|
+
const array = [];
|
|
882
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
883
|
+
array.push(view.getUint32(offset.get(), true));
|
|
884
|
+
offset.advance32();
|
|
885
|
+
}
|
|
886
|
+
result.set(member.name, array);
|
|
887
|
+
} else {
|
|
888
|
+
result.set(member.name, view.getUint32(offset.get(), true));
|
|
889
|
+
offset.advance32();
|
|
890
|
+
}
|
|
891
|
+
break;
|
|
892
|
+
case KnownStructTypeName.Uint64:
|
|
893
|
+
if (member.arraySize !== void 0) {
|
|
894
|
+
const array = [];
|
|
895
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
896
|
+
array.push(view.getBigUint64(offset.get(), true));
|
|
897
|
+
offset.advance64();
|
|
898
|
+
}
|
|
899
|
+
result.set(member.name, array);
|
|
900
|
+
} else {
|
|
901
|
+
result.set(member.name, view.getBigUint64(offset.get(), true));
|
|
902
|
+
offset.advance64();
|
|
903
|
+
}
|
|
904
|
+
break;
|
|
905
|
+
case KnownStructTypeName.Float32:
|
|
906
|
+
case KnownStructTypeName.Float:
|
|
907
|
+
if (member.arraySize !== void 0) {
|
|
908
|
+
const array = [];
|
|
909
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
910
|
+
array.push(view.getFloat32(offset.get(), true));
|
|
911
|
+
offset.advance32();
|
|
912
|
+
}
|
|
913
|
+
result.set(member.name, array);
|
|
914
|
+
} else {
|
|
915
|
+
result.set(member.name, view.getFloat32(offset.get(), true));
|
|
916
|
+
offset.advance32();
|
|
917
|
+
}
|
|
918
|
+
break;
|
|
919
|
+
case KnownStructTypeName.Float64:
|
|
920
|
+
case KnownStructTypeName.Double:
|
|
921
|
+
if (member.arraySize !== void 0) {
|
|
922
|
+
const array = [];
|
|
923
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
924
|
+
array.push(view.getFloat64(offset.get(), true));
|
|
925
|
+
offset.advance64();
|
|
926
|
+
}
|
|
927
|
+
result.set(member.name, array);
|
|
928
|
+
} else {
|
|
929
|
+
result.set(member.name, view.getFloat64(offset.get(), true));
|
|
930
|
+
offset.advance64();
|
|
931
|
+
}
|
|
932
|
+
break;
|
|
933
|
+
case KnownStructTypeName.Character:
|
|
934
|
+
if (member.arraySize !== void 0) result.set(member.name, StructRegistry.TEXT_DECODER.decode(payload.subarray(offset.get(), offset.advance(member.arraySize).get())));
|
|
935
|
+
else result.set(member.name, StructRegistry.TEXT_DECODER.decode(payload.subarray(offset.get(), offset.advance8().get())));
|
|
936
|
+
break;
|
|
937
|
+
default: if (member.arraySize !== void 0) {
|
|
938
|
+
const array = [];
|
|
939
|
+
for (let i = 0; i < member.arraySize; i++) {
|
|
940
|
+
const decoded = this.decode(member.value, payload, offset);
|
|
941
|
+
if (typeof decoded === "string") return decoded;
|
|
942
|
+
array.push(decoded);
|
|
943
|
+
}
|
|
944
|
+
result.set(member.name, array);
|
|
945
|
+
} else {
|
|
946
|
+
const decoded = this.decode(member.value, payload, offset);
|
|
947
|
+
if (typeof decoded === "string") return decoded;
|
|
948
|
+
result.set(member.name, decoded);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
offset.advance(bitFields.flush());
|
|
953
|
+
return result;
|
|
954
|
+
}
|
|
955
|
+
getByteLength(name) {
|
|
956
|
+
const existing = this.byteLengths.get(name);
|
|
957
|
+
if (existing !== void 0) return existing;
|
|
958
|
+
const definition = this.getDefinition(name);
|
|
959
|
+
let totalByteLength = 0;
|
|
960
|
+
const bitFields = new BitFieldTracker();
|
|
961
|
+
for (const member of definition) {
|
|
962
|
+
const memberByteLengthOrBlocker = this.calculateByteLength(member);
|
|
963
|
+
if (typeof memberByteLengthOrBlocker === "string") return memberByteLengthOrBlocker;
|
|
964
|
+
const bitField = bitFields.next(member, memberByteLengthOrBlocker);
|
|
965
|
+
if (bitField) {
|
|
966
|
+
totalByteLength += bitField.bytesToAdvance;
|
|
967
|
+
continue;
|
|
968
|
+
}
|
|
969
|
+
totalByteLength += bitFields.flush() + memberByteLengthOrBlocker;
|
|
970
|
+
}
|
|
971
|
+
totalByteLength += bitFields.flush();
|
|
972
|
+
this.byteLengths.set(name, totalByteLength);
|
|
973
|
+
return totalByteLength;
|
|
974
|
+
}
|
|
975
|
+
calculateByteLength(member) {
|
|
976
|
+
let byteLengthForOne = 0;
|
|
977
|
+
switch (member.value) {
|
|
978
|
+
case KnownStructTypeName.Boolean:
|
|
979
|
+
case KnownStructTypeName.Character:
|
|
980
|
+
case KnownStructTypeName.Int8:
|
|
981
|
+
case KnownStructTypeName.Uint8:
|
|
982
|
+
byteLengthForOne = 1;
|
|
983
|
+
break;
|
|
984
|
+
case KnownStructTypeName.Int16:
|
|
985
|
+
case KnownStructTypeName.Uint16:
|
|
986
|
+
byteLengthForOne = 2;
|
|
987
|
+
break;
|
|
988
|
+
case KnownStructTypeName.Int32:
|
|
989
|
+
case KnownStructTypeName.Uint32:
|
|
990
|
+
byteLengthForOne = 4;
|
|
991
|
+
break;
|
|
992
|
+
case KnownStructTypeName.Int64:
|
|
993
|
+
case KnownStructTypeName.Uint64:
|
|
994
|
+
byteLengthForOne = 8;
|
|
995
|
+
break;
|
|
996
|
+
case KnownStructTypeName.Float32:
|
|
997
|
+
case KnownStructTypeName.Float:
|
|
998
|
+
byteLengthForOne = 4;
|
|
999
|
+
break;
|
|
1000
|
+
case KnownStructTypeName.Float64:
|
|
1001
|
+
case KnownStructTypeName.Double:
|
|
1002
|
+
byteLengthForOne = 8;
|
|
1003
|
+
break;
|
|
1004
|
+
default: {
|
|
1005
|
+
const structByteLengthOrName = this.getByteLength(member.value);
|
|
1006
|
+
if (typeof structByteLengthOrName === "string") return structByteLengthOrName;
|
|
1007
|
+
byteLengthForOne = structByteLengthOrName;
|
|
1008
|
+
break;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
if (member.arraySize !== void 0) return member.arraySize * byteLengthForOne;
|
|
1012
|
+
return byteLengthForOne;
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
//#endregion
|
|
1016
|
+
//#region src/decode-records.ts
|
|
1017
|
+
const TEXT_DECODER = new TextDecoder();
|
|
1018
|
+
const STRUCT_PREFIX = "struct:";
|
|
1019
|
+
const STRUCT_ARRAY_SUFFIX = "[]";
|
|
1020
|
+
const STRUCT_SCHEMA_NAME_PREFIX = "/.schema/struct:";
|
|
1021
|
+
function byteToBoolean(byte) {
|
|
1022
|
+
switch (byte) {
|
|
1023
|
+
case 0: return false;
|
|
1024
|
+
case 1: return true;
|
|
1025
|
+
default: throw diagnostics.WPILOG_R0005({ byte });
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
function normalizeEntryName(rawName) {
|
|
1029
|
+
if (rawName.startsWith("/")) return rawName;
|
|
1030
|
+
return `/${rawName}`;
|
|
1031
|
+
}
|
|
1032
|
+
var RecordDecoder = class {
|
|
1033
|
+
delayedRecords = [];
|
|
1034
|
+
context = /* @__PURE__ */ new Map();
|
|
1035
|
+
recordContexts = /* @__PURE__ */ new WeakMap();
|
|
1036
|
+
structDecodeQueue;
|
|
1037
|
+
structRegistry;
|
|
1038
|
+
options;
|
|
1039
|
+
constructor(options) {
|
|
1040
|
+
this.options = options;
|
|
1041
|
+
this.structDecodeQueue = new StructDecodeQueue((_structName, queuedRecords) => {
|
|
1042
|
+
for (const raw of queuedRecords) {
|
|
1043
|
+
const context = this.recordContexts.get(raw);
|
|
1044
|
+
if (!context) continue;
|
|
1045
|
+
const { entryType, name, metadata } = context;
|
|
1046
|
+
const structName = entryType.slice(7);
|
|
1047
|
+
const base = {
|
|
1048
|
+
entryId: raw.entryId,
|
|
1049
|
+
timestamp: raw.timestamp,
|
|
1050
|
+
name,
|
|
1051
|
+
metadata,
|
|
1052
|
+
structName
|
|
1053
|
+
};
|
|
1054
|
+
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
1055
|
+
const payload = this.structRegistry.decodeArray(structName, raw.payload);
|
|
1056
|
+
if (typeof payload !== "string") this.delayedRecords.push({
|
|
1057
|
+
...base,
|
|
1058
|
+
type: RecordType.StructArray,
|
|
1059
|
+
payload
|
|
1060
|
+
});
|
|
1061
|
+
} else {
|
|
1062
|
+
const payload = this.structRegistry.decode(structName, raw.payload);
|
|
1063
|
+
if (typeof payload !== "string") this.delayedRecords.push({
|
|
1064
|
+
...base,
|
|
1065
|
+
type: RecordType.Struct,
|
|
1066
|
+
payload
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
});
|
|
1071
|
+
this.structRegistry = new StructRegistry(this.structDecodeQueue);
|
|
1072
|
+
}
|
|
1073
|
+
decodeControl(entryId, timestamp, payload) {
|
|
1074
|
+
switch (payload.controlRecordType) {
|
|
1075
|
+
case ControlRecordType.Start:
|
|
1076
|
+
this.context.set(payload.entryId, {
|
|
1077
|
+
name: normalizeEntryName(payload.entryName),
|
|
1078
|
+
entryType: payload.entryType,
|
|
1079
|
+
metadata: payload.entryMetadata
|
|
1080
|
+
});
|
|
1081
|
+
break;
|
|
1082
|
+
case ControlRecordType.Finish:
|
|
1083
|
+
this.context.delete(payload.entryId);
|
|
1084
|
+
break;
|
|
1085
|
+
case ControlRecordType.SetMetadata: {
|
|
1086
|
+
const existing = this.context.get(payload.entryId);
|
|
1087
|
+
if (existing) this.context.set(payload.entryId, {
|
|
1088
|
+
...existing,
|
|
1089
|
+
metadata: payload.entryMetadata
|
|
1090
|
+
});
|
|
1091
|
+
break;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
return {
|
|
1095
|
+
entryId,
|
|
1096
|
+
timestamp,
|
|
1097
|
+
type: RecordType.Control,
|
|
1098
|
+
payload
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
decodeData(entryId, timestamp, bytes, view, payloadOffset, payloadSize) {
|
|
1102
|
+
const context = this.context.get(entryId);
|
|
1103
|
+
if (!context) {
|
|
1104
|
+
if (this.options.strict) throw diagnostics.WPILOG_R0006({ entryId });
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
const base = {
|
|
1108
|
+
entryId,
|
|
1109
|
+
timestamp,
|
|
1110
|
+
name: context.name,
|
|
1111
|
+
metadata: context.metadata
|
|
1112
|
+
};
|
|
1113
|
+
switch (context.entryType) {
|
|
1114
|
+
case "boolean": return {
|
|
1115
|
+
...base,
|
|
1116
|
+
type: RecordType.Boolean,
|
|
1117
|
+
payload: byteToBoolean(view.getUint8(payloadOffset))
|
|
1118
|
+
};
|
|
1119
|
+
case "int64": return {
|
|
1120
|
+
...base,
|
|
1121
|
+
type: RecordType.Int64,
|
|
1122
|
+
payload: view.getBigInt64(payloadOffset, true)
|
|
1123
|
+
};
|
|
1124
|
+
case "float": return {
|
|
1125
|
+
...base,
|
|
1126
|
+
type: RecordType.Float,
|
|
1127
|
+
payload: view.getFloat32(payloadOffset, true)
|
|
1128
|
+
};
|
|
1129
|
+
case "double": return {
|
|
1130
|
+
...base,
|
|
1131
|
+
type: RecordType.Double,
|
|
1132
|
+
payload: view.getFloat64(payloadOffset, true)
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
const payload = payloadOffset === 0 && payloadSize === bytes.byteLength ? bytes : bytes.subarray(payloadOffset, payloadOffset + payloadSize);
|
|
1136
|
+
const raw = {
|
|
1137
|
+
entryId,
|
|
1138
|
+
timestamp,
|
|
1139
|
+
payload
|
|
1140
|
+
};
|
|
1141
|
+
const payloadView = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
1142
|
+
const decoded = decodeNonScalarPayload(raw, context.entryType, context.name, context.metadata, payloadView, this.structRegistry, this.structDecodeQueue);
|
|
1143
|
+
if (!decoded) this.recordContexts.set(raw, context);
|
|
1144
|
+
return decoded;
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
/**
|
|
1148
|
+
* Decode raw WPILOG records into typed values.
|
|
1149
|
+
*
|
|
1150
|
+
* Accepts the output of {@link readRecords} and yields fully decoded records,
|
|
1151
|
+
* including struct decoding with dependency resolution.
|
|
1152
|
+
*
|
|
1153
|
+
* By default, data records that reference an entry ID with no preceding Start
|
|
1154
|
+
* control record are silently skipped. Pass `{ strict: true }` to throw instead.
|
|
1155
|
+
*/
|
|
1156
|
+
function* decodeRecords(records, options = { strict: false }) {
|
|
1157
|
+
const decoder = new RecordDecoder(options);
|
|
1158
|
+
for (const readRecord of records) {
|
|
1159
|
+
if (readRecord.kind === "header") continue;
|
|
1160
|
+
if (readRecord.kind === "control") {
|
|
1161
|
+
yield decoder.decodeControl(readRecord.entryId, readRecord.timestamp, readRecord.payload);
|
|
1162
|
+
continue;
|
|
1163
|
+
}
|
|
1164
|
+
const raw = readRecord.record;
|
|
1165
|
+
const view = new DataView(raw.payload.buffer, raw.payload.byteOffset, raw.payload.byteLength);
|
|
1166
|
+
const decoded = decoder.decodeData(raw.entryId, raw.timestamp, raw.payload, view, 0, raw.payload.byteLength);
|
|
1167
|
+
if (decoded) yield decoded;
|
|
1168
|
+
if (decoder.delayedRecords.length > 0) {
|
|
1169
|
+
yield* decoder.delayedRecords;
|
|
1170
|
+
decoder.delayedRecords.length = 0;
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
/**
|
|
1175
|
+
* Read and decode a WPILOG buffer in a single pass.
|
|
1176
|
+
*
|
|
1177
|
+
* This is the preferred API for fully decoded records. It avoids allocating the
|
|
1178
|
+
* intermediate records produced by composing {@link readRecords} and
|
|
1179
|
+
* {@link decodeRecords} while preserving the same output and strict-mode behavior.
|
|
1180
|
+
*/
|
|
1181
|
+
function* parseDataLog(input, options = { strict: false }) {
|
|
1182
|
+
const cursor = new RecordCursor(input);
|
|
1183
|
+
const decoder = new RecordDecoder(options);
|
|
1184
|
+
while (cursor.next()) {
|
|
1185
|
+
if (cursor.entryId === 0) {
|
|
1186
|
+
yield decoder.decodeControl(cursor.entryId, cursor.timestamp, readControlRecordPayload(cursor.getPayload()));
|
|
1187
|
+
continue;
|
|
1188
|
+
}
|
|
1189
|
+
const decoded = decoder.decodeData(cursor.entryId, cursor.timestamp, cursor.bytes, cursor.view, cursor.payloadOffset, cursor.payloadSize);
|
|
1190
|
+
if (decoded) yield decoded;
|
|
1191
|
+
if (decoder.delayedRecords.length > 0) {
|
|
1192
|
+
yield* decoder.delayedRecords;
|
|
1193
|
+
decoder.delayedRecords.length = 0;
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* Decode non-scalar payloads that require a byte slice or payload-local view.
|
|
1199
|
+
* Fixed-width scalar types return from {@link RecordDecoder#decodeData} before this function so their payloads can be read directly from the input buffer.
|
|
1200
|
+
*/
|
|
1201
|
+
function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegistry, structDecodeQueue) {
|
|
1202
|
+
const base = {
|
|
1203
|
+
entryId: raw.entryId,
|
|
1204
|
+
timestamp: raw.timestamp,
|
|
1205
|
+
name,
|
|
1206
|
+
metadata
|
|
1207
|
+
};
|
|
1208
|
+
switch (entryType) {
|
|
1209
|
+
case "string": return {
|
|
1210
|
+
...base,
|
|
1211
|
+
type: RecordType.String,
|
|
1212
|
+
payload: TEXT_DECODER.decode(raw.payload)
|
|
1213
|
+
};
|
|
1214
|
+
case "boolean[]": {
|
|
1215
|
+
const payload = [];
|
|
1216
|
+
for (let i = 0; i < raw.payload.byteLength; i++) payload.push(byteToBoolean(view.getUint8(i)));
|
|
1217
|
+
return {
|
|
1218
|
+
...base,
|
|
1219
|
+
type: RecordType.BooleanArray,
|
|
1220
|
+
payload
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
1223
|
+
case "int64[]": {
|
|
1224
|
+
const payload = [];
|
|
1225
|
+
for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getBigUint64(i, true));
|
|
1226
|
+
return {
|
|
1227
|
+
...base,
|
|
1228
|
+
type: RecordType.Int64Array,
|
|
1229
|
+
payload
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
case "float[]": {
|
|
1233
|
+
const payload = [];
|
|
1234
|
+
for (let i = 0; i < raw.payload.byteLength; i += 4) payload.push(view.getFloat32(i, true));
|
|
1235
|
+
return {
|
|
1236
|
+
...base,
|
|
1237
|
+
type: RecordType.FloatArray,
|
|
1238
|
+
payload
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
case "double[]": {
|
|
1242
|
+
const payload = [];
|
|
1243
|
+
for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getFloat64(i, true));
|
|
1244
|
+
return {
|
|
1245
|
+
...base,
|
|
1246
|
+
type: RecordType.DoubleArray,
|
|
1247
|
+
payload
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
case "string[]": {
|
|
1251
|
+
const payload = [];
|
|
1252
|
+
const offset = new ByteOffset();
|
|
1253
|
+
const arrayLength = view.getUint32(offset.get(), true);
|
|
1254
|
+
offset.advance32();
|
|
1255
|
+
for (let i = 0; i < arrayLength; i++) {
|
|
1256
|
+
const stringLength = view.getUint32(offset.get(), true);
|
|
1257
|
+
offset.advance32();
|
|
1258
|
+
const string = TEXT_DECODER.decode(raw.payload.subarray(offset.get(), offset.get() + stringLength));
|
|
1259
|
+
offset.advance(stringLength);
|
|
1260
|
+
payload.push(string);
|
|
1261
|
+
}
|
|
1262
|
+
return {
|
|
1263
|
+
...base,
|
|
1264
|
+
type: RecordType.StringArray,
|
|
1265
|
+
payload
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
case "structschema": {
|
|
1269
|
+
const structName = name.slice(name.indexOf(STRUCT_SCHEMA_NAME_PREFIX) + 16);
|
|
1270
|
+
const payload = TEXT_DECODER.decode(raw.payload);
|
|
1271
|
+
structRegistry.register(structName, payload);
|
|
1272
|
+
return {
|
|
1273
|
+
...base,
|
|
1274
|
+
type: RecordType.String,
|
|
1275
|
+
payload
|
|
1276
|
+
};
|
|
1277
|
+
}
|
|
1278
|
+
default:
|
|
1279
|
+
if (entryType.startsWith(STRUCT_PREFIX)) {
|
|
1280
|
+
const structName = entryType.slice(7);
|
|
1281
|
+
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
1282
|
+
const decoded = structRegistry.decodeArray(structName, raw.payload);
|
|
1283
|
+
if (typeof decoded === "string") {
|
|
1284
|
+
structDecodeQueue.queueStructRecord(decoded, raw);
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
return {
|
|
1288
|
+
...base,
|
|
1289
|
+
type: RecordType.StructArray,
|
|
1290
|
+
structName,
|
|
1291
|
+
payload: decoded
|
|
1292
|
+
};
|
|
1293
|
+
}
|
|
1294
|
+
const decoded = structRegistry.decode(structName, raw.payload);
|
|
1295
|
+
if (typeof decoded === "string") {
|
|
1296
|
+
structDecodeQueue.queueStructRecord(decoded, raw);
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
return {
|
|
1300
|
+
...base,
|
|
1301
|
+
type: RecordType.Struct,
|
|
1302
|
+
structName,
|
|
1303
|
+
payload: decoded
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
return {
|
|
1307
|
+
...base,
|
|
1308
|
+
type: RecordType.Raw,
|
|
1309
|
+
payload: raw.payload
|
|
1310
|
+
};
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
//#endregion
|
|
1314
|
+
//#region src/catalog.ts
|
|
1315
|
+
/**
|
|
1316
|
+
* Collect all entry definitions from a WPILOG record stream.
|
|
1317
|
+
*
|
|
1318
|
+
* Iterates through records, collecting Start control records.
|
|
1319
|
+
* Data record bytes are still read from the stream but not decoded,
|
|
1320
|
+
* making this much cheaper than full decoding via {@link parseDataLog}.
|
|
1321
|
+
*/
|
|
1322
|
+
function* catalogEntries(records) {
|
|
1323
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1324
|
+
for (const record of records) {
|
|
1325
|
+
if (record.kind !== "control") continue;
|
|
1326
|
+
if (record.payload.controlRecordType === ControlRecordType.Start) {
|
|
1327
|
+
const entry = {
|
|
1328
|
+
entryId: record.payload.entryId,
|
|
1329
|
+
name: normalizeEntryName(record.payload.entryName),
|
|
1330
|
+
type: record.payload.entryType,
|
|
1331
|
+
metadata: record.payload.entryMetadata
|
|
1332
|
+
};
|
|
1333
|
+
entries.set(record.payload.entryId, entry);
|
|
1334
|
+
yield entry;
|
|
1335
|
+
}
|
|
1336
|
+
if (record.payload.controlRecordType === ControlRecordType.SetMetadata) {
|
|
1337
|
+
const existing = entries.get(record.payload.entryId);
|
|
1338
|
+
if (existing) existing.metadata = record.payload.entryMetadata;
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
//#endregion
|
|
1343
|
+
export { KnownStructTypeName as a, RecordType as c, parseStructSpecification as i, isDataRecord as l, decodeRecords as n, readRecords as o, parseDataLog as r, ControlRecordType as s, catalogEntries as t, diagnostics as u };
|