Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/wrapped-parsers-0-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
"@cosyte/cli": patch
---

The command now installs `@cosyte/astm`, `@cosyte/dicom`, `@cosyte/ncpdp`, `@cosyte/terminology` and `@cosyte/x12` at `^0.2.0`. No command, option, exit code or `CLI_*` code changes.

`cosyte fmt` still writes the canonical form for ASTM and NCPDP (Telecom and SCRIPT). Those parsers now write a message back as it was read by default, so `fmt` asks them for the canonical form explicitly.

A code the CLI passes through from a wrapped parser, in parentheses after `CLI_PARSE_FAILED` on stderr when an input cannot be parsed, is now that parser's 0.2 code. The exit code (`65`) and the `CLI_*` code do not change. The renamed codes are:

- `EMPTY_INPUT` from ASTM is now `ASTM_EMPTY_INPUT`.
- `EMPTY_INPUT` from NCPDP is now `NCPDP_SCRIPT_EMPTY_INPUT` (SCRIPT) or `NCPDP_TELECOM_EMPTY_INPUT` (Telecom).
- `EMPTY_INPUT` from DICOM is now `DICOM_EMPTY_INPUT`.
- `NOT_DICOM_PART_10` is now `DICOM_NOT_PART_10`.
- `INVALID_FILE_META` is now `DICOM_INVALID_FILE_META`.
- `UNSUPPORTED_TRANSFER_SYNTAX` from DICOM is now `DICOM_UNSUPPORTED_TRANSFER_SYNTAX`.

A script that matched one of the earlier strings should match the new one.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@
},
"dependencies": {
"@cosyte/hl7": "^0.1.10",
"@cosyte/terminology": "^0.1.6"
"@cosyte/terminology": "^0.2.0"
},
"optionalDependencies": {
"@cosyte/astm": "^0.1.7",
"@cosyte/astm": "^0.2.0",
"@cosyte/ccda": "^0.1.6",
"@cosyte/deid": "^0.2.4",
"@cosyte/dicom": "^0.1.13",
"@cosyte/dicom": "^0.2.0",
"@cosyte/fhir": "^0.1.11",
"@cosyte/mllp": "^0.1.9",
"@cosyte/ncpdp": "^0.1.7",
"@cosyte/ncpdp": "^0.2.0",
"@cosyte/transform": "^0.2.2",
"@cosyte/x12": "^0.1.12",
"@cosyte/x12": "^0.2.0",
"@modelcontextprotocol/sdk": "^1.32.2"
}
}
86 changes: 50 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions src/core/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ export async function inspectFormat(
return {
format: "dicom",
sopClassUid: ds.fileMeta?.mediaStorageSOPClassUID ?? null,
/* v8 ignore next -- parseDicom refuses an input with no File Meta Transfer Syntax UID (INVALID_FILE_META or NOT_DICOM_PART_10), so a parsed file always carries one */
/* v8 ignore next -- parseDicom refuses an input with no File Meta Transfer Syntax UID (DICOM_INVALID_FILE_META or DICOM_NOT_PART_10), so a parsed file always carries one */
transferSyntaxUid: ds.fileMeta?.transferSyntaxUID ?? null,
elementCount: ds.elements().length,
warningCount,
Expand Down Expand Up @@ -674,7 +674,10 @@ export async function fmtFormat(format: CosyteFormat, bytes: Uint8Array): Promis
() => import("@cosyte/astm"),
);
const msg = parseAstmRecords(decode(bytes));
return { output: serializeAstmRecords(msg), warningCount: msg.warnings.length };
return {
output: serializeAstmRecords(msg, { normalize: true }),
warningCount: msg.warnings.length,
};
}
case "ccda": {
const { parseCcda, serializeCcda } = await loadOptional("ccda", () => import("@cosyte/ccda"));
Expand All @@ -688,10 +691,16 @@ export async function fmtFormat(format: CosyteFormat, bytes: Uint8Array): Promis
);
if (!isNcpdpScript(bytes)) {
const tx = parseTelecom(Buffer.from(bytes));
return { output: serializeTelecom(tx), warningCount: tx.warnings.length };
return {
output: serializeTelecom(tx, { normalize: true }),
warningCount: tx.warnings.length,
};
}
const msg = parseScript(decode(bytes));
return { output: serializeScript(msg), warningCount: msg.warnings.length };
return {
output: serializeScript(msg, { normalize: true }),
warningCount: msg.warnings.length,
};
}
case "dicom":
case "mllp":
Expand Down
34 changes: 34 additions & 0 deletions test/formats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ describe("astm: parse / inspect / fmt / validate", () => {
expect(r.stdout.startsWith("H")).toBe(true);
});

it("fmt writes the canonical delimiter set, not the one the input was read with", async () => {
// The same two records with `!` as the field delimiter: fmt is the normalizer, so it must write
// them back with `|`, exactly as the library's canonical form.
const input = enc.encode("H!\\^&\rL!1!N\r");
const r = await run(["fmt", "f.astm"], bytesDeps(input));
expect(r.exit).toBe(EXIT.OK);
expect(r.stdout).toBe("H|\\^&\rL|1|N\r\n");
});

it("validate returns a verdict", async () => {
const r = await run(["validate", "f.astm"], bytesDeps(ASTM));
expect([EXIT.OK, EXIT.INVALID]).toContain(r.exit);
Expand Down Expand Up @@ -172,6 +181,18 @@ describe("ncpdp: parse / inspect / fmt / validate (SCRIPT)", () => {
expect(r.stdout).toContain("Message");
});

it("fmt writes one canonical form for inputs that differ only in markup", async () => {
// Whitespace inside a tag changes the text but not the message, so both format alike.
const spaced = enc.encode(
Buffer.from(NCPDP).toString("utf8").replace("<Gender>F</Gender>", "<Gender >F</Gender >"),
);
const a = await run(["fmt", "rx.xml"], bytesDeps(NCPDP));
const b = await run(["fmt", "rx.xml"], bytesDeps(spaced));
expect(a.exit).toBe(EXIT.OK);
expect(b.exit).toBe(EXIT.OK);
expect(b.stdout).toBe(a.stdout);
});

it("validate returns a verdict", async () => {
const r = await run(["validate", "rx.xml"], bytesDeps(NCPDP));
expect([EXIT.OK, EXIT.INVALID]).toContain(r.exit);
Expand Down Expand Up @@ -233,6 +254,19 @@ describe("ncpdp: parse / inspect / fmt / validate (Telecom vD.0, a B1 billing cl
expect(r.stdout).toBe(`${Buffer.from(TELECOM).toString("latin1")}\n`);
});

it("fmt normalizes a Telecom input that is not in canonical form", async () => {
// An empty field separator before the AM04 segment separator: the canonical emit drops it, so fmt
// must give back the fixture, not the input it read.
const loose = Buffer.from(
Buffer.from(TELECOM).toString("latin1").replace("\x1eAM04", "\x1c\x1eAM04"),
"latin1",
);
expect(loose.equals(Buffer.from(TELECOM))).toBe(false);
const r = await run(["fmt", "b1.ncpdp"], bytesDeps(loose));
expect(r.exit).toBe(EXIT.OK);
expect(r.stdout).toBe(`${Buffer.from(TELECOM).toString("latin1")}\n`);
});

it("validate returns a clean verdict", async () => {
const r = await run(["validate", "b1.ncpdp", "--json"], bytesDeps(TELECOM));
expect(r.exit).toBe(EXIT.OK);
Expand Down
10 changes: 5 additions & 5 deletions test/sibling-releases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { describe, expect, it } from "vitest";
*/
const FLOORS: Readonly<Record<string, string>> = {
"@cosyte/hl7": "0.1.10",
"@cosyte/terminology": "0.1.6",
"@cosyte/astm": "0.1.7",
"@cosyte/terminology": "0.2.0",
"@cosyte/astm": "0.2.0",
"@cosyte/ccda": "0.1.6",
"@cosyte/dicom": "0.1.13",
"@cosyte/dicom": "0.2.0",
"@cosyte/fhir": "0.1.11",
"@cosyte/mllp": "0.1.9",
"@cosyte/ncpdp": "0.1.7",
"@cosyte/x12": "0.1.12",
"@cosyte/ncpdp": "0.2.0",
"@cosyte/x12": "0.2.0",
"@cosyte/transform": "0.2.2",
"@cosyte/deid": "0.2.4",
};
Expand Down
Loading