From d2c3b2c325ab8a70a0ad0317d7e2463ed3f8cbcb Mon Sep 17 00:00:00 2001 From: Nic Dorman Date: Wed, 13 May 2026 17:24:04 +0000 Subject: [PATCH] fix(antd-zig): align stdlib API usage with Zig 0.14.x (the declared minimum) The source mixed APIs from different Zig versions and did not compile on any released Zig (see WithAutonomi/ant-sdk#78): - json_helpers.zig used 0.15-dev ArrayList style: var list: std.ArrayList(u8) = .empty; list.append(allocator, c); list.deinit(allocator); list.toOwnedSlice(allocator); These are not present in Zig 0.14.x, which is what build.zig.zon declares as minimum_zig_version. Switch to the 0.14 style: var list = std.ArrayList(u8).init(allocator); list.append(c); list.deinit(); list.toOwnedSlice(); - antd.zig used req.status which is a 0.15-dev shortcut. In 0.14.x the response struct sits one level deeper: @intFromEnum(req.status) -> @intFromEnum(req.response.status) - tests.zig referenced a json_helpers.parseCost helper that no longer exists. Rename the test to use parseCostEstimate (the actual helper) and adjust the expected JSON body + assertion. After this change: $ zig version 0.14.1 $ zig build # builds clean $ zig build test # all unit tests pass $ zig build run-01-connect # connects to a live antd daemon $ zig build run-03-chunks # stores a chunk on the local devnet Examples 02 + 06 still need the arity fix tracked in #70 (PR #79) before they can run end-to-end; 04-files fails at runtime when /tmp/example.txt is absent (test data, not a compile concern); 05-graph references SDK methods (graphEntryPut/Get/Exists/Cost) that do not exist in src/antd.zig - a separate issue worth filing. Closes #78 --- antd-zig/src/antd.zig | 2 +- antd-zig/src/json_helpers.zig | 52 +++++++++++++++++------------------ antd-zig/src/tests.zig | 10 +++---- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/antd-zig/src/antd.zig b/antd-zig/src/antd.zig index 32cc496..36c7500 100644 --- a/antd-zig/src/antd.zig +++ b/antd-zig/src/antd.zig @@ -121,7 +121,7 @@ pub const Client = struct { req.wait() catch return error.HttpError; - const status_code = @intFromEnum(req.status); + const status_code = @intFromEnum(req.response.status); // For HEAD requests, just check status if (method == .HEAD) { diff --git a/antd-zig/src/json_helpers.zig b/antd-zig/src/json_helpers.zig index 1f91292..ad63a55 100644 --- a/antd-zig/src/json_helpers.zig +++ b/antd-zig/src/json_helpers.zig @@ -210,27 +210,27 @@ fn dupeU64(v: std.json.Value) u64 { /// Escape a string for JSON output. fn jsonEscapeString(allocator: Allocator, s: []const u8) ![]const u8 { - var list: std.ArrayList(u8) = .empty; - errdefer list.deinit(allocator); - try list.append(allocator, '"'); + var list = std.ArrayList(u8).init(allocator); + errdefer list.deinit(); + try list.append('"'); for (s) |c| { switch (c) { - '"' => try list.appendSlice(allocator, "\\\""), - '\\' => try list.appendSlice(allocator, "\\\\"), - '\n' => try list.appendSlice(allocator, "\\n"), - '\r' => try list.appendSlice(allocator, "\\r"), - '\t' => try list.appendSlice(allocator, "\\t"), + '"' => try list.appendSlice("\\\""), + '\\' => try list.appendSlice("\\\\"), + '\n' => try list.appendSlice("\\n"), + '\r' => try list.appendSlice("\\r"), + '\t' => try list.appendSlice("\\t"), else => { if (c < 0x20) { - try list.writer(allocator).print("\\u{x:0>4}", .{c}); + try list.writer().print("\\u{x:0>4}", .{c}); } else { - try list.append(allocator, c); + try list.append(c); } }, } } - try list.append(allocator, '"'); - return list.toOwnedSlice(allocator); + try list.append('"'); + return list.toOwnedSlice(); } /// Build a JSON object string with a single base64-encoded "data" field. @@ -271,45 +271,45 @@ pub const JsonValue = union(enum) { /// Build a JSON request body from key-value pairs. pub fn buildJsonBody(allocator: Allocator, fields: []const struct { key: []const u8, value: JsonValue }) ![]const u8 { - var buf: std.ArrayList(u8) = .empty; - errdefer buf.deinit(allocator); + var buf = std.ArrayList(u8).init(allocator); + errdefer buf.deinit(); - try buf.append(allocator, '{'); + try buf.append('{'); for (fields, 0..) |field, i| { - if (i > 0) try buf.append(allocator, ','); + if (i > 0) try buf.append(','); // Write key const escaped_key = jsonEscapeString(allocator, field.key) catch return error.JsonError; defer allocator.free(escaped_key); - try buf.appendSlice(allocator, escaped_key); - try buf.append(allocator, ':'); + try buf.appendSlice(escaped_key); + try buf.append(':'); // Write value switch (field.value) { .string => |s| { const escaped_val = jsonEscapeString(allocator, s) catch return error.JsonError; defer allocator.free(escaped_val); - try buf.appendSlice(allocator, escaped_val); + try buf.appendSlice(escaped_val); }, .boolean => |b| { - try buf.appendSlice(allocator, if (b) "true" else "false"); + try buf.appendSlice(if (b) "true" else "false"); }, .string_array => |arr| { - try buf.append(allocator, '['); + try buf.append('['); for (arr, 0..) |item, j| { - if (j > 0) try buf.append(allocator, ','); + if (j > 0) try buf.append(','); const escaped_item = jsonEscapeString(allocator, item) catch return error.JsonError; defer allocator.free(escaped_item); - try buf.appendSlice(allocator, escaped_item); + try buf.appendSlice(escaped_item); } - try buf.append(allocator, ']'); + try buf.append(']'); }, } } - try buf.append(allocator, '}'); - return buf.toOwnedSlice(allocator); + try buf.append('}'); + return buf.toOwnedSlice(); } /// Parse a WalletAddress from a JSON response body. diff --git a/antd-zig/src/tests.zig b/antd-zig/src/tests.zig index 5f9b54a..d514bd4 100644 --- a/antd-zig/src/tests.zig +++ b/antd-zig/src/tests.zig @@ -134,14 +134,14 @@ test "parseBase64Data decodes data" { try testing.expectEqualStrings("hello", result); } -test "parseCost extracts cost" { +test "parseCostEstimate extracts cost" { const body = - \\{"cost":"500"} + \\{"cost":"500","file_size":0,"chunk_count":0,"estimated_gas_cost_wei":"0","payment_mode":""} ; - const result = try json_helpers.parseCost(testing.allocator, body); - defer testing.allocator.free(result); + const result = try json_helpers.parseCostEstimate(testing.allocator, body); + defer result.deinit(testing.allocator); - try testing.expectEqualStrings("500", result); + try testing.expectEqualStrings("500", result.cost); } // =============================================================================