add new data type, Node
This commit is contained in:
parent
b9153e2708
commit
907d71a19a
33
src/hncore/Node.zig
Normal file
33
src/hncore/Node.zig
Normal file
@ -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"}
|
||||
);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user