@bcts/dcbor-cli 1.0.0-alpha.13
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/LICENSE +48 -0
- package/README.md +105 -0
- package/dist/cli.cjs +132 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +133 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.d.cts +202 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +202 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +3 -0
- package/dist/src-Ce085uR8.mjs +407 -0
- package/dist/src-Ce085uR8.mjs.map +1 -0
- package/dist/src-M5HM-SCU.cjs +490 -0
- package/dist/src-M5HM-SCU.cjs.map +1 -0
- package/package.json +87 -0
- package/src/cli.ts +239 -0
- package/src/cmd/array.ts +41 -0
- package/src/cmd/default.ts +108 -0
- package/src/cmd/index.ts +19 -0
- package/src/cmd/map.ts +41 -0
- package/src/cmd/match.ts +196 -0
- package/src/format.ts +82 -0
- package/src/index.ts +41 -0
- package/src/run.ts +134 -0
package/src/format.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format utilities for dcbor-cli
|
|
3
|
+
* Contains InputFormat, OutputFormat enums and formatOutput function
|
|
4
|
+
* Equivalent to the format-related code in Rust's main.rs
|
|
5
|
+
*/
|
|
6
|
+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
type Cbor,
|
|
10
|
+
type Result,
|
|
11
|
+
diagnosticOpt,
|
|
12
|
+
hexOpt,
|
|
13
|
+
bytesToHex,
|
|
14
|
+
cborData,
|
|
15
|
+
errorMsg,
|
|
16
|
+
} from "@bcts/dcbor";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Input format options
|
|
20
|
+
*/
|
|
21
|
+
export type InputFormat = "diag" | "hex" | "bin";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Output format options
|
|
25
|
+
*/
|
|
26
|
+
export type OutputFormat = "diag" | "hex" | "bin" | "none";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Format CBOR output in the specified format
|
|
30
|
+
* Equivalent to Rust's format_output function
|
|
31
|
+
*/
|
|
32
|
+
export function formatOutput(
|
|
33
|
+
cbor: Cbor,
|
|
34
|
+
outFormat: OutputFormat,
|
|
35
|
+
annotate: boolean,
|
|
36
|
+
): Result<string> {
|
|
37
|
+
try {
|
|
38
|
+
switch (outFormat) {
|
|
39
|
+
case "diag":
|
|
40
|
+
// Use flat: true for compact single-line output (matching Rust CLI behavior)
|
|
41
|
+
if (annotate) {
|
|
42
|
+
return { ok: true, value: diagnosticOpt(cbor, { annotate: true, flat: true }) };
|
|
43
|
+
} else {
|
|
44
|
+
return { ok: true, value: diagnosticOpt(cbor, { flat: true }) };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
case "hex":
|
|
48
|
+
if (annotate) {
|
|
49
|
+
return { ok: true, value: hexOpt(cbor, { annotate: true }) };
|
|
50
|
+
} else {
|
|
51
|
+
return { ok: true, value: bytesToHex(cborData(cbor)) };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
case "bin":
|
|
55
|
+
// For binary output, return hex representation
|
|
56
|
+
// The caller will handle converting to actual binary
|
|
57
|
+
return { ok: true, value: bytesToHex(cborData(cbor)) };
|
|
58
|
+
|
|
59
|
+
case "none":
|
|
60
|
+
return { ok: true, value: "" };
|
|
61
|
+
|
|
62
|
+
default:
|
|
63
|
+
return { ok: false, error: errorMsg(`Unknown output format: ${outFormat}`) };
|
|
64
|
+
}
|
|
65
|
+
} catch (e) {
|
|
66
|
+
return { ok: false, error: errorMsg(e instanceof Error ? e.message : String(e)) };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read binary data from a buffer
|
|
72
|
+
*/
|
|
73
|
+
export function readData(data: Uint8Array): Uint8Array {
|
|
74
|
+
return data;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Read string data from a buffer
|
|
79
|
+
*/
|
|
80
|
+
export function readString(data: Uint8Array): string {
|
|
81
|
+
return new TextDecoder().decode(data);
|
|
82
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @bcts/dcbor-cli - Command line parser/validator for deterministic CBOR (dCBOR)
|
|
3
|
+
*
|
|
4
|
+
* A command line tool for composing, parsing and validating Gordian dCBOR.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const VERSION = "1.0.0-alpha.13";
|
|
10
|
+
|
|
11
|
+
// Export format utilities
|
|
12
|
+
export { formatOutput, readData, readString } from "./format.js";
|
|
13
|
+
export type { InputFormat, OutputFormat } from "./format.js";
|
|
14
|
+
|
|
15
|
+
// Export command modules
|
|
16
|
+
export {
|
|
17
|
+
// Exec interface
|
|
18
|
+
type Exec,
|
|
19
|
+
// Array command
|
|
20
|
+
type ArrayCommandArgs,
|
|
21
|
+
execArray,
|
|
22
|
+
createArrayCommand,
|
|
23
|
+
// Map command
|
|
24
|
+
type MapCommandArgs,
|
|
25
|
+
execMap,
|
|
26
|
+
createMapCommand,
|
|
27
|
+
// Default command
|
|
28
|
+
type DefaultCommandArgs,
|
|
29
|
+
execDefault,
|
|
30
|
+
execDefaultWithReader,
|
|
31
|
+
createDefaultCommand,
|
|
32
|
+
// Match command
|
|
33
|
+
type MatchOutputFormat,
|
|
34
|
+
type MatchCommandArgs,
|
|
35
|
+
execMatch,
|
|
36
|
+
createMatchCommand,
|
|
37
|
+
} from "./cmd/index.js";
|
|
38
|
+
|
|
39
|
+
// Export the run function for programmatic use
|
|
40
|
+
export { run } from "./run.js";
|
|
41
|
+
export type { RunOptions, RunResult, Command } from "./run.js";
|
package/src/run.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main run function for dcbor-cli
|
|
3
|
+
* Equivalent to Rust's run function in main.rs
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { registerTags, errorToString } from "@bcts/dcbor";
|
|
7
|
+
import type { InputFormat, OutputFormat } from "./format.js";
|
|
8
|
+
import { execArray, execDefault, execMap, execMatch, type MatchOutputFormat } from "./cmd/index.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Command type discriminator
|
|
12
|
+
*/
|
|
13
|
+
export type Command =
|
|
14
|
+
| { type: "array"; elements: string[]; out: OutputFormat; annotate: boolean }
|
|
15
|
+
| { type: "map"; kvPairs: string[]; out: OutputFormat; annotate: boolean }
|
|
16
|
+
| {
|
|
17
|
+
type: "match";
|
|
18
|
+
pattern: string;
|
|
19
|
+
input?: string | undefined;
|
|
20
|
+
in: InputFormat;
|
|
21
|
+
out: MatchOutputFormat;
|
|
22
|
+
noIndent: boolean;
|
|
23
|
+
lastOnly: boolean;
|
|
24
|
+
annotate: boolean;
|
|
25
|
+
captures: boolean;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
type: "default";
|
|
29
|
+
input?: string | undefined;
|
|
30
|
+
in: InputFormat;
|
|
31
|
+
out: OutputFormat;
|
|
32
|
+
annotate: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export interface RunOptions {
|
|
36
|
+
command: Command;
|
|
37
|
+
stdinContent?: string | undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface RunResult {
|
|
41
|
+
output: string;
|
|
42
|
+
isBinary: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Main execution function
|
|
47
|
+
* Equivalent to Rust's run<I, T, R, W> function
|
|
48
|
+
*/
|
|
49
|
+
export function run(
|
|
50
|
+
options: RunOptions,
|
|
51
|
+
): { ok: true; value: RunResult } | { ok: false; error: Error } {
|
|
52
|
+
// Register BC components tags
|
|
53
|
+
registerTags();
|
|
54
|
+
|
|
55
|
+
const { command, stdinContent } = options;
|
|
56
|
+
|
|
57
|
+
let output: string;
|
|
58
|
+
let isBinary = false;
|
|
59
|
+
|
|
60
|
+
switch (command.type) {
|
|
61
|
+
case "array": {
|
|
62
|
+
const result = execArray({
|
|
63
|
+
elements: command.elements,
|
|
64
|
+
out: command.out,
|
|
65
|
+
annotate: command.annotate,
|
|
66
|
+
});
|
|
67
|
+
if (!result.ok) {
|
|
68
|
+
return { ok: false, error: new Error(errorToString(result.error)) };
|
|
69
|
+
}
|
|
70
|
+
output = result.value;
|
|
71
|
+
isBinary = command.out === "bin";
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
case "map": {
|
|
76
|
+
const result = execMap({
|
|
77
|
+
kvPairs: command.kvPairs,
|
|
78
|
+
out: command.out,
|
|
79
|
+
annotate: command.annotate,
|
|
80
|
+
});
|
|
81
|
+
if (!result.ok) {
|
|
82
|
+
return { ok: false, error: new Error(errorToString(result.error)) };
|
|
83
|
+
}
|
|
84
|
+
output = result.value;
|
|
85
|
+
isBinary = command.out === "bin";
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
case "match": {
|
|
90
|
+
const result = execMatch(
|
|
91
|
+
{
|
|
92
|
+
pattern: command.pattern,
|
|
93
|
+
input: command.input,
|
|
94
|
+
in: command.in,
|
|
95
|
+
out: command.out,
|
|
96
|
+
noIndent: command.noIndent,
|
|
97
|
+
lastOnly: command.lastOnly,
|
|
98
|
+
annotate: command.annotate,
|
|
99
|
+
captures: command.captures,
|
|
100
|
+
},
|
|
101
|
+
stdinContent,
|
|
102
|
+
);
|
|
103
|
+
if (!result.ok) {
|
|
104
|
+
return { ok: false, error: new Error(errorToString(result.error)) };
|
|
105
|
+
}
|
|
106
|
+
output = result.value;
|
|
107
|
+
isBinary = command.out === "bin";
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
case "default": {
|
|
112
|
+
const result = execDefault(
|
|
113
|
+
{
|
|
114
|
+
input: command.input,
|
|
115
|
+
in: command.in,
|
|
116
|
+
out: command.out,
|
|
117
|
+
annotate: command.annotate,
|
|
118
|
+
},
|
|
119
|
+
stdinContent,
|
|
120
|
+
);
|
|
121
|
+
if (!result.ok) {
|
|
122
|
+
return { ok: false, error: new Error(errorToString(result.error)) };
|
|
123
|
+
}
|
|
124
|
+
output = result.value;
|
|
125
|
+
isBinary = command.out === "bin";
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
default:
|
|
130
|
+
return { ok: false, error: new Error("Unknown command type") };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return { ok: true, value: { output, isBinary } };
|
|
134
|
+
}
|