feat(engine): trace parser (JSON object / JSONL / bare array)#17
Merged
Conversation
Add `src/ocpp/parser.zig` — the third S1 engine module — parsing untrusted
trace input into normalized `Event`s. Mirrors the toolkit's `core/parser.ts`
and `schemas.ts` (the shared conformance contract), not its source.
- Format detection and per-format parsers: JSON object
(`{ traceId?, metadata?, events[] }`), JSONL (one event per line), and bare
array of raw messages, plus the object→JSONL fallback.
- Structural validation of raw messages and event inputs; malformed individual
events warn (with 1-based line/index and a bounded excerpt) while structural
failures abort with a typed error.
- Untrusted-input limits: 10 MB input (checked first) and 10 000 events
(checked after), and parsing into a caller-owned arena with `alloc_always`
so the result borrows only the arena, never the input buffer.
The toolkit's parser test cases are ported.
Closes #13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Third slice of S1 — Engine core:
src/ocpp/parser.zig, which turns anuntrusted trace string into normalized
Events plus warnings. Mirrors thetoolkit's
core/parser.ts+schemas.ts— the shared conformance contract —so both implementations accept the same inputs and reject the same malformations.
Changes
{ traceId?, metadata?, events[] }),JSONL (one event per line, blank lines skipped), and bare array of raw
messages. A
{-leading input that isn't a single JSON value falls back toJSONL (kept only if it yields events).
validateRawMessage/classifyEventInputmirrorrawOcppMessageSchema/traceEventInputSchema(MessageTypeId ∈ {2,3,4},string UniqueId, per-type arity, string action / error positions; optional
string|number|null timestamp; optional enum direction).
line number + bounded excerpt) and are skipped; JSON object and bare array are
all-or-nothing at the structural level, matching the toolkit's Zod schemas.
Security (untrusted input)
MAX_INPUT_SIZE_BYTES= 10 MB, checked before parsing.MAX_EVENT_COUNT= 10 000, checked after.allocate = .alloc_always, so everystring is copied in and the
ParseResultborrows only the arena — never thecaller's input buffer.
__proto__and friends are inert: Zig objects have no prototype, so key"pollution" is a non-issue (covered by a test).
Testing
native test -Dplatform=null→ 25/25 pass (8 new parser tests ported fromthe toolkit: each format, blank-line skipping, per-line + structural warnings,
normalization through the parser, no-reorder, size/count limits, pollution).
native check --strictandzig fmt --checkclean.Closes #13