From 907d71a19acd46511bcfe66c63b7526381ef335b Mon Sep 17 00:00:00 2001 From: falsycat Date: Wed, 2 Apr 2025 22:30:39 +0900 Subject: [PATCH] add new data type, Node --- src/hncore/Node.zig | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/hncore/Node.zig diff --git a/src/hncore/Node.zig b/src/hncore/Node.zig new file mode 100644 index 0000000..2f19868 --- /dev/null +++ b/src/hncore/Node.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +pub const Node = struct { + id : usize, + summary: []const u8, + + /// + pub fn init(alloc: std.mem.Allocator, id: usize, summary: []const u8) !Node { + return .{ + .id = id, + .summary = try alloc.dupe(u8, summary), + }; + } + /// pass the same allocator as init() call + pub fn deinit(self: *@This(), alloc: std.mem.Allocator) void { + alloc.free(self.summary); + } +}; + +test "serialize" { + const alloc = std.testing.allocator; + var node = try Node.init(alloc, 0, "helloworld"); + defer node.deinit(alloc); + + var json = std.ArrayList(u8).init(alloc); + defer json.deinit(); + try std.json.stringify(node, .{}, json.writer()); + + try std.testing.expectEqualStrings( + json.items, + \\{"id":0,"summary":"helloworld"} + ); +}