authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-30 02:47:18 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-12-30 02:47:18 -08:00
log9c1edaafed430d8e85175781781eba6214e4cb13
tree48ed82501a12ca36bf38200583c7f1bbc95e54b0
parent9140fae343679307bc106f39ee9f4a2e51ac5f4c

add test and fix leaks


3 files changed, 85 insertions(+), 1 deletions(-)

build.zig created+19
......@@ -0,0 +1,19 @@
1const std = @import("std");
2const deps = @import("./deps.zig");
3
4pub fn build(b: *std.Build) void {
5 const target = b.standardTargetOptions(.{});
6 const mode = b.option(std.builtin.Mode, "mode", "") orelse .Debug;
7
8 const tests = b.addTest(.{
9 .root_source_file = b.path("test.zig"),
10 .target = target,
11 .optimize = mode,
12 });
13 deps.addAllTo(tests);
14
15 const test_step = b.step("test", "Run all library tests");
16 const tests_run = b.addRunArtifact(tests);
17 tests_run.has_side_effects = true;
18 test_step.dependOn(&tests_run.step);
19}
test.zig created+11
......@@ -0,0 +1,11 @@
1const std = @import("std");
2const yaml = @import("yaml");
3
4const file = @embedFile("./zigmod.yml");
5
6test {
7 const doc = try yaml.parse(std.testing.allocator, file);
8 defer doc.deinit(std.testing.allocator);
9
10 try std.testing.expectEqualStrings("g982zq6e8wsvnmduerpbf8787hu85brugmngn8wf", doc.mapping.get_string("id").?);
11}
yaml.zig+55-1
......@@ -11,10 +11,19 @@ const c = @cImport({
1111
1212pub const Stream = struct {
1313 docs: []const Document,
14
15 pub fn deinit(self: *const Stream, alloc: std.mem.Allocator) void {
16 for (self.docs) |*item| item.deinit(alloc);
17 alloc.free(self.docs);
18 }
1419};
1520
1621pub const Document = struct {
1722 mapping: Mapping,
23
24 pub fn deinit(self: *const Document, alloc: std.mem.Allocator) void {
25 self.mapping.deinit(alloc);
26 }
1827};
1928
2029pub const Item = union(enum) {
......@@ -25,6 +34,22 @@ pub const Item = union(enum) {
2534 string: string,
2635 stream: Stream,
2736
37 pub fn deinit(self: *const Item, alloc: std.mem.Allocator) void {
38 switch (self.*) {
39 .event => {},
40 .kv => |kv| kv.deinit(alloc),
41 .mapping => |m| m.deinit(alloc),
42 .sequence => |s| {
43 for (s) |*item| item.deinit(alloc);
44 alloc.free(s);
45 },
46 .string => |s| {
47 _ = s;
48 },
49 .stream => |s| s.deinit(alloc),
50 }
51 }
52
2853 pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
2954 _ = fmt;
3055 _ = options;
......@@ -60,6 +85,11 @@ pub const Sequence = []const Item;
6085pub const Key = struct {
6186 key: string,
6287 value: Value,
88
89 pub fn deinit(self: *const Key, alloc: std.mem.Allocator) void {
90 // alloc.free(self.key);
91 self.value.deinit(alloc);
92 }
6393};
6494
6595pub const Value = union(enum) {
......@@ -67,6 +97,22 @@ pub const Value = union(enum) {
6797 mapping: Mapping,
6898 sequence: Sequence,
6999
100 pub fn deinit(self: *const Value, alloc: std.mem.Allocator) void {
101 switch (self.*) {
102 .string => |s| {
103 _ = s;
104 // alloc.free(s);
105 },
106 .mapping => |*m| {
107 m.deinit(alloc);
108 },
109 .sequence => |s| {
110 for (s) |*item| item.deinit(alloc);
111 alloc.free(s);
112 },
113 }
114 }
115
70116 pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
71117 _ = fmt;
72118 _ = options;
......@@ -94,6 +140,11 @@ pub const Value = union(enum) {
94140pub const Mapping = struct {
95141 items: []const Key,
96142
143 pub fn deinit(self: *const Mapping, alloc: std.mem.Allocator) void {
144 for (self.items) |*item| item.deinit(alloc);
145 alloc.free(self.items);
146 }
147
97148 pub fn get(self: Mapping, k: string) ?Value {
98149 for (self.items) |item| {
99150 if (std.mem.eql(u8, item.key, k)) {
......@@ -118,7 +169,7 @@ pub const Mapping = struct {
118169
119170 pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string {
120171 var list = std.ArrayList(string).init(alloc);
121 defer list.deinit();
172 errdefer list.deinit();
122173 if (self.get(k)) |val| {
123174 if (val == .sequence) {
124175 for (val.sequence) |item| {
......@@ -161,10 +212,12 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document {
161212 defer c.yaml_parser_delete(&parser);
162213
163214 const lines = try split(alloc, input, "\n");
215 defer alloc.free(lines);
164216
165217 _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len);
166218
167219 var all_events = std.ArrayList(Token).init(alloc);
220 defer all_events.deinit();
168221 var event: Token = undefined;
169222 while (true) {
170223 const p = c.yaml_parser_parse(&parser, &event);
......@@ -188,6 +241,7 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document {
188241 .index = 0,
189242 };
190243 const stream = try p.parse();
244 defer alloc.free(stream.docs);
191245 return stream.docs[0];
192246}
193247