Skip to content

Commit

Permalink
Add basic pretty-printing for diagnostic format
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Aug 31, 2024
1 parent d024c3c commit 151b5ce
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class CBORcontainer {
dcbor: false,
diagnosticSizes: DiagnosticSizes.PREFERRED,
convertUnsafeIntsToFloat: false,
pretty: false,
preferMap: false,
rejectLargeNegatives: false,
rejectBigInts: false,
Expand Down Expand Up @@ -111,6 +112,7 @@ export class CBORcontainer {
public offset: number;
public count = 0;
public children: Tag | unknown[] = [];
public depth = 0;
#opts: RequiredDecodeOptions;
#encodedChildren: Uint8Array[] | null = null;

Expand All @@ -125,6 +127,9 @@ export class CBORcontainer {
this.left = left;
this.parent = parent;
this.#opts = opts;
if (parent) {
this.depth = parent.depth + 1;
}

if (this.mt === MT.MAP) {
if (this.#opts.sortKeys || this.#opts.rejectDuplicateKeys) {
Expand Down
33 changes: 26 additions & 7 deletions src/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {Simple} from './simple.js';
import {halfToUint} from './float.js';
import {u8toHex} from './utils.js';

const INDENT = ' ';
const TE = new TextEncoder();

/**
Expand Down Expand Up @@ -143,11 +144,21 @@ export function diagnose(

for (const mav of stream) {
const [mt, ai, val] = mav;
if (parent && (parent.count > 0) && (val !== SYMS.BREAK)) {
if ((parent.mt === MT.MAP) && (parent.count % 2)) {
str += ': ';
} else {
str += ', ';
if (parent) {
if ((parent.count > 0) && (val !== SYMS.BREAK)) {
if ((parent.mt === MT.MAP) && (parent.count % 2)) {
str += ': ';
} else {
str += ',';
if (!opts.pretty) {
str += ' ';
}
}
}
if (opts.pretty) {
if ((parent.mt !== MT.MAP) || (parent.count % 2 === 0)) {
str += `\n${INDENT.repeat(parent.depth + 1)}`;
}
}
}
ret = CBORcontainer.create(mav, parent, opts, stream);
Expand Down Expand Up @@ -204,7 +215,11 @@ export function diagnose(
if (s) {
str += ' ';
}
ret.close = ']';
if (opts.pretty && val) {
ret.close = `\n${INDENT.repeat(ret.depth)}]`;
} else {
ret.close = ']';
}
break;
}
case MT.MAP: {
Expand All @@ -214,7 +229,11 @@ export function diagnose(
if (s) {
str += ' ';
}
ret.close = '}';
if (opts.pretty && val) {
ret.close = `\n${INDENT.repeat(ret.depth)}}`;
} else {
ret.close = '}';
}
break;
}
case MT.TAG:
Expand Down
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface Parent {
children: Decodeable | unknown[];
left: number;
offset: number;
depth: number;
get done(): boolean;
get isStreaming(): boolean;
push(child: unknown, stream: Sliceable, offset: number): number;
Expand Down Expand Up @@ -160,6 +161,12 @@ export interface DecodeOptions extends DecodeStreamOptions {
*/
preferMap?: boolean;

/**
* Pretty-print diagnostic format.
* @default false
*/
pretty?: boolean;

/**
* Reject negative integers in the range [CBOR_NEGATIVE_INT_MAX ...
* STANDARD_NEGATIVE_INT_MAX - 1].
Expand Down
19 changes: 19 additions & 0 deletions test/diagnostic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,22 @@ test('never use lengths', () => {
'2'
);
});

test('pretty', () => {
assert.equal(
diagnose('a26474797065f563666f6fa4f40203820204048005a0', {pretty: true}),
`\
{
"type": true,
"foo": {
false: 2,
3: [
2,
4
],
4: [],
5: {}
}
}`
);
});
4 changes: 4 additions & 0 deletions web/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ <h3>Options</h3>
<input type="checkbox" id="preferMap">
<label for="preferMap">preferMap</label>
</li>
<li>
<input type="checkbox" id="pretty">
<label for="pretty">pretty</label>
</li>
<li>
<input type="checkbox" id="rejectLargeNegatives">
<label for="rejectLargeNegatives">rejectLargeNegatives</label>
Expand Down
1 change: 1 addition & 0 deletions web/test/playground.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ test('Conversions', async({page}) => {

await page.locator('#output-fmt').selectOption('diagnostic');
await expect(page.getByRole('button', {name: 'Copy to input'})).toBeEnabled();
await checkOutputs(page);

await page.locator('#output-fmt').selectOption('hex');
await page.getByRole('button', {name: 'Copy to input'}).click();
Expand Down

0 comments on commit 151b5ce

Please sign in to comment.