@zpl-toolchain/core 0.1.1

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 ADDED
@@ -0,0 +1,67 @@
1
+ # @zpl-toolchain/core
2
+
3
+ TypeScript wrapper for the ZPL toolchain WASM bindings. Provides full TypeScript types and an ergonomic API for parsing, validating, formatting, and explaining ZPL code.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @zpl-toolchain/core
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { init, parse, format, validate, explain } from "@zpl-toolchain/core";
15
+
16
+ // Initialize WASM module (required once before calling any function)
17
+ await init();
18
+
19
+ // Parse ZPL
20
+ const result = parse("^XA^FDHello^FS^XZ");
21
+ console.log(result.ast.labels.length); // 1
22
+
23
+ // Format ZPL
24
+ const formatted = format("^XA^FD Hello ^FS^XZ", "label");
25
+
26
+ // Validate ZPL
27
+ const validation = validate("^XA^FDHello^FS^XZ");
28
+ console.log(validation.ok); // true
29
+
30
+ // Explain a diagnostic code
31
+ const explanation = explain("ZPL1201");
32
+ ```
33
+
34
+ ## API
35
+
36
+ | Function | Signature | Description |
37
+ |---|---|---|
38
+ | `init()` | `() → Promise<void>` | Initialize WASM module (call once) |
39
+ | `parse(input)` | `(string) → ParseResult` | Parse ZPL, return AST + diagnostics |
40
+ | `parseWithTables(input, tablesJson)` | `(string, string) → ParseResult` | Parse with explicit parser tables |
41
+ | `validate(input, profileJson?)` | `(string, string?) → ValidationResult` | Parse + validate |
42
+ | `format(input, indent?)` | `(string, IndentStyle?) → string` | Format ZPL |
43
+ | `explain(id)` | `(string) → string \| null` | Explain a diagnostic code |
44
+
45
+ ## Types
46
+
47
+ All types are exported and match the Rust AST serialization format:
48
+
49
+ - **`Node`** — discriminated union on `kind`: `CommandNode | FieldDataNode | RawDataNode | TriviaNode`
50
+ - **`Severity`** — `"error" | "warn" | "info"` (lowercase, matching Rust serde)
51
+ - **`Presence`** — `"unset" | "empty" | "value"` (lowercase)
52
+ - **`IndentStyle`** — `"none" | "label" | "field"`
53
+
54
+ See `src/index.ts` for the full type definitions.
55
+
56
+ ## Build from source
57
+
58
+ ```bash
59
+ # Install dependencies
60
+ npm install
61
+
62
+ # Build WASM artifacts (requires wasm-pack)
63
+ npm run build:wasm
64
+
65
+ # Build TypeScript package
66
+ npm run build
67
+ ```
@@ -0,0 +1,139 @@
1
+ /**
2
+ * @zpl-toolchain/core — TypeScript bindings for the ZPL toolchain.
3
+ *
4
+ * This package wraps the WASM build of the Rust ZPL toolchain, exposing
5
+ * parse, validate, format, and explain functions with full TypeScript types.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { init, parse, format } from "@zpl-toolchain/core";
10
+ *
11
+ * // Initialize WASM (required before calling any function)
12
+ * await init();
13
+ *
14
+ * const result = parse("^XA^FDHello^FS^XZ");
15
+ * console.log(result.ast.labels);
16
+ *
17
+ * const formatted = format("^XA^FD Hello ^FS^XZ");
18
+ * console.log(formatted);
19
+ * ```
20
+ */
21
+ /** Byte range in the source input. */
22
+ export interface Span {
23
+ start: number;
24
+ end: number;
25
+ }
26
+ /** Argument presence state. Serialized as lowercase by Rust. */
27
+ export type Presence = "unset" | "empty" | "value";
28
+ /** A parsed argument slot. */
29
+ export interface ArgSlot {
30
+ /** Parameter key name (absent when not defined by spec). */
31
+ key?: string | null;
32
+ /** Whether the argument was provided, empty, or not present. */
33
+ presence: Presence;
34
+ /** The argument value (absent when presence is "unset" or "empty"). */
35
+ value?: string | null;
36
+ }
37
+ /**
38
+ * AST node — discriminated on the `kind` field.
39
+ *
40
+ * Rust serializes `Node` with `#[serde(tag = "kind")]` (internally tagged),
41
+ * producing JSON like `{"kind": "Command", "code": "^XA", ...}`.
42
+ */
43
+ export type Node = CommandNode | FieldDataNode | RawDataNode | TriviaNode;
44
+ export interface CommandNode {
45
+ kind: "Command";
46
+ code: string;
47
+ args: ArgSlot[];
48
+ span: Span;
49
+ }
50
+ export interface FieldDataNode {
51
+ kind: "FieldData";
52
+ content: string;
53
+ /** Whether `^FH` hex escapes have been applied. */
54
+ hex_escaped: boolean;
55
+ span: Span;
56
+ }
57
+ export interface RawDataNode {
58
+ kind: "RawData";
59
+ /** The command that initiated raw data collection (e.g., "^GF"). */
60
+ command: string;
61
+ /** Raw payload data (absent if command header had no trailing data). */
62
+ data?: string | null;
63
+ span: Span;
64
+ }
65
+ export interface TriviaNode {
66
+ kind: "Trivia";
67
+ text: string;
68
+ span: Span;
69
+ }
70
+ /** A single ZPL label (^XA ... ^XZ block). */
71
+ export interface Label {
72
+ nodes: Node[];
73
+ }
74
+ /** Top-level AST for a ZPL document. */
75
+ export interface Ast {
76
+ labels: Label[];
77
+ }
78
+ /** Diagnostic severity level. Serialized as lowercase by Rust. */
79
+ export type Severity = "error" | "warn" | "info";
80
+ /** A diagnostic message from the parser or validator. */
81
+ export interface Diagnostic {
82
+ id: string;
83
+ severity: Severity;
84
+ message: string;
85
+ span?: Span;
86
+ context?: Record<string, string>;
87
+ }
88
+ /** Result of parsing a ZPL string. */
89
+ export interface ParseResult {
90
+ ast: Ast;
91
+ diagnostics: Diagnostic[];
92
+ }
93
+ /** Result of validating a ZPL string. */
94
+ export interface ValidationResult {
95
+ ok: boolean;
96
+ issues: Diagnostic[];
97
+ }
98
+ /** Indentation style for the formatter. */
99
+ export type IndentStyle = "none" | "label" | "field";
100
+ /**
101
+ * Initialize the WASM module. Must be called once before using any other
102
+ * function. Safe to call multiple times (subsequent calls are no-ops).
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * await init();
107
+ * ```
108
+ */
109
+ export declare function init(): Promise<void>;
110
+ /**
111
+ * Parse a ZPL string and return the AST with diagnostics.
112
+ *
113
+ * Uses embedded parser tables for spec-driven parsing.
114
+ */
115
+ export declare function parse(input: string): ParseResult;
116
+ /**
117
+ * Parse a ZPL string with explicitly provided parser tables (JSON string).
118
+ */
119
+ export declare function parseWithTables(input: string, tablesJson: string): ParseResult;
120
+ /**
121
+ * Parse and validate a ZPL string.
122
+ *
123
+ * @param input ZPL source code.
124
+ * @param profileJson Optional printer profile JSON string.
125
+ */
126
+ export declare function validate(input: string, profileJson?: string): ValidationResult;
127
+ /**
128
+ * Format a ZPL string (normalize whitespace, one command per line).
129
+ *
130
+ * @param input ZPL source code.
131
+ * @param indent Indentation style: "none" (default), "label", or "field".
132
+ */
133
+ export declare function format(input: string, indent?: IndentStyle): string;
134
+ /**
135
+ * Explain a diagnostic code (e.g., "ZPL1201").
136
+ *
137
+ * @returns The explanation string, or null if the code is unknown.
138
+ */
139
+ export declare function explain(id: string): string | null;
package/dist/index.js ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @zpl-toolchain/core — TypeScript bindings for the ZPL toolchain.
3
+ *
4
+ * This package wraps the WASM build of the Rust ZPL toolchain, exposing
5
+ * parse, validate, format, and explain functions with full TypeScript types.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * import { init, parse, format } from "@zpl-toolchain/core";
10
+ *
11
+ * // Initialize WASM (required before calling any function)
12
+ * await init();
13
+ *
14
+ * const result = parse("^XA^FDHello^FS^XZ");
15
+ * console.log(result.ast.labels);
16
+ *
17
+ * const formatted = format("^XA^FD Hello ^FS^XZ");
18
+ * console.log(formatted);
19
+ * ```
20
+ */
21
+ // ── WASM Module ─────────────────────────────────────────────────────────
22
+ // The WASM module is loaded lazily. In a bundler environment (webpack, vite,
23
+ // etc.), the WASM file is typically handled as an asset. For Node.js, the
24
+ // WASM file must be on disk.
25
+ //
26
+ // We use dynamic import so the WASM binary is only fetched when init() is
27
+ // called, keeping the initial bundle lightweight.
28
+ let wasmModule = null;
29
+ /**
30
+ * Initialize the WASM module. Must be called once before using any other
31
+ * function. Safe to call multiple times (subsequent calls are no-ops).
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * await init();
36
+ * ```
37
+ */
38
+ export async function init() {
39
+ if (wasmModule)
40
+ return;
41
+ // Dynamic import — bundlers will resolve the WASM package
42
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
43
+ wasmModule = await import("../wasm/pkg/zpl_toolchain_wasm");
44
+ }
45
+ function ensureInit() {
46
+ if (!wasmModule) {
47
+ throw new Error("@zpl-toolchain/core: WASM not initialized. Call `await init()` first.");
48
+ }
49
+ return wasmModule;
50
+ }
51
+ // ── Public API ──────────────────────────────────────────────────────────
52
+ /**
53
+ * Parse a ZPL string and return the AST with diagnostics.
54
+ *
55
+ * Uses embedded parser tables for spec-driven parsing.
56
+ */
57
+ export function parse(input) {
58
+ const wasm = ensureInit();
59
+ return wasm.parse(input);
60
+ }
61
+ /**
62
+ * Parse a ZPL string with explicitly provided parser tables (JSON string).
63
+ */
64
+ export function parseWithTables(input, tablesJson) {
65
+ const wasm = ensureInit();
66
+ return wasm.parseWithTables(input, tablesJson);
67
+ }
68
+ /**
69
+ * Parse and validate a ZPL string.
70
+ *
71
+ * @param input ZPL source code.
72
+ * @param profileJson Optional printer profile JSON string.
73
+ */
74
+ export function validate(input, profileJson) {
75
+ const wasm = ensureInit();
76
+ return wasm.validate(input, profileJson);
77
+ }
78
+ /**
79
+ * Format a ZPL string (normalize whitespace, one command per line).
80
+ *
81
+ * @param input ZPL source code.
82
+ * @param indent Indentation style: "none" (default), "label", or "field".
83
+ */
84
+ export function format(input, indent) {
85
+ const wasm = ensureInit();
86
+ return wasm.format(input, indent);
87
+ }
88
+ /**
89
+ * Explain a diagnostic code (e.g., "ZPL1201").
90
+ *
91
+ * @returns The explanation string, or null if the code is unknown.
92
+ */
93
+ export function explain(id) {
94
+ const wasm = ensureInit();
95
+ return wasm.explain(id) ?? null;
96
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@zpl-toolchain/core",
3
+ "version": "0.1.1",
4
+ "description": "ZPL II toolchain — parse, validate, and format Zebra Programming Language files",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js"
10
+ }
11
+ },
12
+ "scripts": {
13
+ "build:wasm": "wasm-pack build ../../crates/wasm --target bundler --out-dir ../../packages/ts/core/wasm/pkg",
14
+ "build": "npx tsc",
15
+ "typecheck": "npx tsc --noEmit",
16
+ "test": "node --test"
17
+ },
18
+ "keywords": [
19
+ "zpl",
20
+ "zebra",
21
+ "label",
22
+ "printer",
23
+ "parser",
24
+ "formatter"
25
+ ],
26
+ "license": "MIT OR Apache-2.0",
27
+ "author": "zpl-toolchain contributors",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/trevordcampbell/zpl-toolchain.git",
31
+ "directory": "packages/ts/core"
32
+ },
33
+ "homepage": "https://github.com/trevordcampbell/zpl-toolchain",
34
+ "bugs": {
35
+ "url": "https://github.com/trevordcampbell/zpl-toolchain/issues"
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "wasm/pkg",
40
+ "README.md"
41
+ ],
42
+ "publishConfig": {
43
+ "access": "public"
44
+ },
45
+ "devDependencies": {
46
+ "typescript": "^5.0.0"
47
+ },
48
+ "dependencies": {}
49
+ }