authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-04-08 16:57:46 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-04-08 16:57:46 -07:00
log5d364ed5b994a50318d4350670451eeb09d40324
treed190c05bd027f4c68f5b19acfcd1085b9cb2be42
parentafda0846c28f23dbb53b677538f8215f4764f4f4
signaturelock-open Commit is signed but in an unrecognized format.

util/yaml: add custom formatters


1 files changed, 52 insertions(+), 0 deletions(-)

src/util/yaml.zig+52
......@@ -21,6 +21,29 @@ pub const Item = union(enum) {
2121 sequence: []Item,
2222 document: Document,
2323 string: []const u8,
24
25 pub fn format(self: Item, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
26 try writer.writeAll("Item{");
27 switch (self) {
28 .event, .kv, .document => {
29 unreachable;
30 },
31 .mapping => {
32 try std.fmt.format(writer, "{}", .{self.mapping});
33 },
34 .sequence => {
35 try writer.writeAll("[ ");
36 for (self.sequence) |it| {
37 try std.fmt.format(writer, "{}, ", .{it});
38 }
39 try writer.writeAll("]");
40 },
41 .string => {
42 try std.fmt.format(writer, "{s}", .{self.string});
43 },
44 }
45 try writer.writeAll("}");
46 }
2447};
2548
2649pub const Key = struct {
......@@ -32,6 +55,26 @@ pub const Value = union(enum) {
3255 string: []const u8,
3356 mapping: Mapping,
3457 sequence: []Item,
58
59 pub fn format(self: Value, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
60 try writer.writeAll("Value{");
61 switch (self) {
62 .string => {
63 try std.fmt.format(writer, "{s}", .{self.string});
64 },
65 .mapping => {
66 try std.fmt.format(writer, "{}", .{self.mapping});
67 },
68 .sequence => {
69 try writer.writeAll("[ ");
70 for (self.sequence) |it| {
71 try std.fmt.format(writer, "{}, ", .{it});
72 }
73 try writer.writeAll("]");
74 },
75 }
76 try writer.writeAll("}");
77 }
3578};
3679
3780pub const Mapping = struct {
......@@ -64,6 +107,15 @@ pub const Mapping = struct {
64107 }
65108 return list.items;
66109 }
110
111 pub fn format(self: Mapping, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) @TypeOf(writer).Error!void {
112 try writer.writeAll("{ ");
113 for (self.items) |it| {
114 try std.fmt.format(writer, "{s}: ", .{it.key});
115 try std.fmt.format(writer, "{}, ", .{it.value});
116 }
117 try writer.writeAll("}");
118 }
67119};
68120
69121//