|
| 1 | +/** |
| 2 | + * Shared canonical-reader factory for the IE binary formats (ITM, SPL, EFF). |
| 3 | + * |
| 4 | + * Each format's `<format>/canonical-reader.ts` calls `createIeCanonicalReader` |
| 5 | + * to get the three operations every IE format needs: |
| 6 | + * - `getDocument(result)` — pull the canonical doc off `result.document` |
| 7 | + * with permissive schema validation, undefined |
| 8 | + * if absent. |
| 9 | + * - `rebuildDocument(result)` — same plus a non-null assertion; the parser |
| 10 | + * always sets `result.document`, so a missing |
| 11 | + * doc here is a programming error. |
| 12 | + * - `createSnapshot(result)` — wrap the doc in the IE snapshot envelope |
| 13 | + * (`schemaVersion: 1`, `format`, `formatName`, |
| 14 | + * optional `opaqueRanges` / `warnings`). |
| 15 | + * |
| 16 | + * The body of each operation was byte-identical across the three formats |
| 17 | + * (`s/itm/spl/eff/`); only the schema types and the format-id discriminant |
| 18 | + * varied. Hand-rolling the same shape per format guaranteed they'd drift — |
| 19 | + * one format could grow a snapshot field the others didn't get. |
| 20 | + */ |
| 21 | + |
| 22 | +import { z } from "zod"; |
| 23 | +import { parseWithSchemaValidation } from "../schema-validation"; |
| 24 | +import type { ParseResult } from "../types"; |
| 25 | + |
| 26 | +export type IeFormatId = "itm" | "spl" | "eff"; |
| 27 | + |
| 28 | +interface IeSnapshotEnvelope<Doc> { |
| 29 | + readonly schemaVersion: 1; |
| 30 | + readonly format: IeFormatId; |
| 31 | + readonly formatName?: string; |
| 32 | + readonly document: Doc; |
| 33 | +} |
| 34 | + |
| 35 | +export interface IeCanonicalReaderConfig<Doc, Snap extends IeSnapshotEnvelope<Doc>> { |
| 36 | + readonly formatId: IeFormatId; |
| 37 | + /** Display label used in error messages (`"ITM"`, `"SPL"`, `"EFF"`). */ |
| 38 | + readonly formatLabel: string; |
| 39 | + readonly documentSchemaPermissive: z.ZodType<Doc>; |
| 40 | + readonly snapshotSchemaPermissive: z.ZodType<Snap>; |
| 41 | +} |
| 42 | + |
| 43 | +export interface IeCanonicalReader<Doc, Snap extends IeSnapshotEnvelope<Doc>> { |
| 44 | + readonly getDocument: (result: ParseResult) => Doc | undefined; |
| 45 | + readonly rebuildDocument: (result: ParseResult) => Doc; |
| 46 | + readonly createSnapshot: (result: ParseResult) => Snap; |
| 47 | +} |
| 48 | + |
| 49 | +export function createIeCanonicalReader<Doc, Snap extends IeSnapshotEnvelope<Doc>>( |
| 50 | + config: IeCanonicalReaderConfig<Doc, Snap>, |
| 51 | +): IeCanonicalReader<Doc, Snap> { |
| 52 | + const { formatId, formatLabel, documentSchemaPermissive, snapshotSchemaPermissive } = config; |
| 53 | + |
| 54 | + const getDocument = (result: ParseResult): Doc | undefined => { |
| 55 | + if (!result.document) return undefined; |
| 56 | + return parseWithSchemaValidation( |
| 57 | + documentSchemaPermissive, |
| 58 | + result.document, |
| 59 | + `Invalid ${formatLabel} canonical document`, |
| 60 | + ); |
| 61 | + }; |
| 62 | + |
| 63 | + const rebuildDocument = (result: ParseResult): Doc => { |
| 64 | + const doc = getDocument(result); |
| 65 | + if (!doc) { |
| 66 | + throw new Error( |
| 67 | + `${formatLabel} canonical document missing from ParseResult; display-tree-only rebuild is not implemented`, |
| 68 | + ); |
| 69 | + } |
| 70 | + return doc; |
| 71 | + }; |
| 72 | + |
| 73 | + const createSnapshot = (result: ParseResult): Snap => { |
| 74 | + const document = rebuildDocument(result); |
| 75 | + // Build the wire-shape envelope every IE snapshot has, then promote |
| 76 | + // through the snapshot zod for variants that carry `opaqueRanges` |
| 77 | + // (validates the shape) or attach `warnings` directly. The cast is |
| 78 | + // safe by construction: Snap extends IeSnapshotEnvelope<Doc>, and |
| 79 | + // we only ever produce the union of {base, base+opaqueRanges, |
| 80 | + // base+warnings} which the per-format snapshot schema enumerates. |
| 81 | + const envelope: IeSnapshotEnvelope<Doc> = { |
| 82 | + schemaVersion: 1, |
| 83 | + format: formatId, |
| 84 | + formatName: result.formatName, |
| 85 | + document, |
| 86 | + }; |
| 87 | + if (result.opaqueRanges && result.opaqueRanges.length > 0) { |
| 88 | + return parseWithSchemaValidation( |
| 89 | + snapshotSchemaPermissive, |
| 90 | + { ...envelope, opaqueRanges: result.opaqueRanges }, |
| 91 | + `Invalid ${formatLabel} canonical snapshot`, |
| 92 | + ); |
| 93 | + } |
| 94 | + if (result.warnings) { |
| 95 | + return { ...envelope, warnings: result.warnings } as unknown as Snap; |
| 96 | + } |
| 97 | + return envelope as unknown as Snap; |
| 98 | + }; |
| 99 | + |
| 100 | + return { getDocument, rebuildDocument, createSnapshot }; |
| 101 | +} |
0 commit comments