// SPDX-FileCopyrightText: 2026 Jeffrey C. Ollie // // SPDX-License-Identifier: MIT const std = @import("std"); const json = @import("json.zig"); address: u16, value: u8, /// Parses a memory location, as an [address, value] pair. The corpus stores /// these as arrays rather than objects, so the default object based parser /// cannot be used. /// /// Read straight off the token stream rather than through std.json.Value. The /// corpus holds about nineteen of these per case and there are two and a half /// million cases, so building a dynamic tree for each one and immediately /// throwing it away was the single most expensive thing the runner did. pub fn jsonParse( alloc: std.mem.Allocator, source: anytype, options: std.json.ParseOptions, ) std.json.ParseError(@TypeOf(source.*))!@This() { _ = alloc; _ = options; try json.expect(source, .array_begin); const address = try json.number(source, u16); const value = try json.number(source, u8); try json.expect(source, .array_end); return .{ .address = address, .value = value }; } /// Parses a memory location from an already parsed JSON value. Nothing in the /// runner takes this path, but a caller that has a tree in hand can. pub fn jsonParseFromValue(_: std.mem.Allocator, value: std.json.Value, _: std.json.ParseOptions) std.json.ParseFromValueError!@This() { const items = switch (value) { .array => |v| v.items, else => return error.UnexpectedToken, }; if (items.len != 2) return error.LengthMismatch; return .{ .address = try json.cast(u16, items[0]), .value = try json.cast(u8, items[1]), }; }