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({...@@ -11,10 +11,19 @@ const c = @cImport({
1111
12pub const Stream = struct {12pub const Stream = struct {
13 docs: []const Document,13 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 }
14};19};
1520
16pub const Document = struct {21pub const Document = struct {
17 mapping: Mapping,22 mapping: Mapping,
23
24 pub fn deinit(self: *const Document, alloc: std.mem.Allocator) void {
25 self.mapping.deinit(alloc);
26 }
18};27};
1928
20pub const Item = union(enum) {29pub const Item = union(enum) {
...@@ -25,6 +34,22 @@ pub const Item = union(enum) {...@@ -25,6 +34,22 @@ pub const Item = union(enum) {
25 string: string,34 string: string,
26 stream: Stream,35 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
28 pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {53 pub fn format(self: Item, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
29 _ = fmt;54 _ = fmt;
30 _ = options;55 _ = options;
...@@ -60,6 +85,11 @@ pub const Sequence = []const Item;...@@ -60,6 +85,11 @@ pub const Sequence = []const Item;
60pub const Key = struct {85pub const Key = struct {
61 key: string,86 key: string,
62 value: Value,87 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 }
63};93};
6494
65pub const Value = union(enum) {95pub const Value = union(enum) {
...@@ -67,6 +97,22 @@ pub const Value = union(enum) {...@@ -67,6 +97,22 @@ pub const Value = union(enum) {
67 mapping: Mapping,97 mapping: Mapping,
68 sequence: Sequence,98 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
70 pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {116 pub fn format(self: Value, comptime fmt: string, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
71 _ = fmt;117 _ = fmt;
72 _ = options;118 _ = options;
...@@ -94,6 +140,11 @@ pub const Value = union(enum) {...@@ -94,6 +140,11 @@ pub const Value = union(enum) {
94pub const Mapping = struct {140pub const Mapping = struct {
95 items: []const Key,141 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
97 pub fn get(self: Mapping, k: string) ?Value {148 pub fn get(self: Mapping, k: string) ?Value {
98 for (self.items) |item| {149 for (self.items) |item| {
99 if (std.mem.eql(u8, item.key, k)) {150 if (std.mem.eql(u8, item.key, k)) {
...@@ -118,7 +169,7 @@ pub const Mapping = struct {...@@ -118,7 +169,7 @@ pub const Mapping = struct {
118169
119 pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string {170 pub fn get_string_array(self: Mapping, alloc: std.mem.Allocator, k: string) ![]string {
120 var list = std.ArrayList(string).init(alloc);171 var list = std.ArrayList(string).init(alloc);
121 defer list.deinit();172 errdefer list.deinit();
122 if (self.get(k)) |val| {173 if (self.get(k)) |val| {
123 if (val == .sequence) {174 if (val == .sequence) {
124 for (val.sequence) |item| {175 for (val.sequence) |item| {
...@@ -161,10 +212,12 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document {...@@ -161,10 +212,12 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document {
161 defer c.yaml_parser_delete(&parser);212 defer c.yaml_parser_delete(&parser);
162213
163 const lines = try split(alloc, input, "\n");214 const lines = try split(alloc, input, "\n");
215 defer alloc.free(lines);
164216
165 _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len);217 _ = c.yaml_parser_set_input_string(&parser, input.ptr, input.len);
166218
167 var all_events = std.ArrayList(Token).init(alloc);219 var all_events = std.ArrayList(Token).init(alloc);
220 defer all_events.deinit();
168 var event: Token = undefined;221 var event: Token = undefined;
169 while (true) {222 while (true) {
170 const p = c.yaml_parser_parse(&parser, &event);223 const p = c.yaml_parser_parse(&parser, &event);
...@@ -188,6 +241,7 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document {...@@ -188,6 +241,7 @@ pub fn parse(alloc: std.mem.Allocator, input: string) !Document {
188 .index = 0,241 .index = 0,
189 };242 };
190 const stream = try p.parse();243 const stream = try p.parse();
244 defer alloc.free(stream.docs);
191 return stream.docs[0];245 return stream.docs[0];
192}246}
193247