commit 3b2fec17a4dfe034c28f528d003bf09b5e44c2b6 (tree)
parent 1b62a22268117340ee7a17f019df01cd39ec1421
Author: Mason Remaley <mason@anthropicstudios.com>
Date: Fri, 21 Feb 2025 21:55:36 -0800
std.zon: populate `Zoir.Node.Index` values with corresponding ZOIR node
This allows using `std.zon` to parse schemas which are not directly
representable in the Zig type system; for instance, `build.zig.zon`.
Diffstat:
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/lib/std/zon/parse.zig b/lib/std/zon/parse.zig
@@ -436,6 +436,10 @@ const Parser = struct {
T: type,
node: Zoir.Node.Index,
) error{ ParseZon, OutOfMemory, WrongType }!T {
+ if (T == Zoir.Node.Index) {
+ return node;
+ }
+
switch (@typeInfo(T)) {
.optional => |optional| if (node.get(self.zoir) == .null) {
return null;
@@ -3441,3 +3445,27 @@ test "std.zon add pointers" {
try std.testing.expectFmt("1:1: error: expected optional enum literal\n", "{}", .{status});
}
}
+
+test "std.zon stop on node" {
+ const gpa = std.testing.allocator;
+
+ {
+ const Vec2 = struct {
+ x: Zoir.Node.Index,
+ y: f32,
+ };
+
+ var status: Status = .{};
+ defer status.deinit(gpa);
+ const result = try fromSlice(Vec2, gpa, ".{ .x = 1.5, .y = 2.5 }", &status, .{});
+ try std.testing.expectEqual(result.y, 2.5);
+ try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.5 }, result.x.get(status.zoir.?));
+ }
+
+ {
+ var status: Status = .{};
+ defer status.deinit(gpa);
+ const result = try fromSlice(Zoir.Node.Index, gpa, "1.23", &status, .{});
+ try std.testing.expectEqual(Zoir.Node{ .float_literal = 1.23 }, result.get(status.zoir.?));
+ }
+}