| ... | @@ -21,6 +21,29 @@ pub const Item = union(enum) { | ... | @@ -21,6 +21,29 @@ pub const Item = union(enum) { |
| 21 | sequence: []Item, | 21 | sequence: []Item, |
| 22 | document: Document, | 22 | document: Document, |
| 23 | string: []const u8, | 23 | 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 | } |
| 24 | }; | 47 | }; |
| 25 | | 48 | |
| 26 | pub const Key = struct { | 49 | pub const Key = struct { |
| ... | @@ -32,6 +55,26 @@ pub const Value = union(enum) { | ... | @@ -32,6 +55,26 @@ pub const Value = union(enum) { |
| 32 | string: []const u8, | 55 | string: []const u8, |
| 33 | mapping: Mapping, | 56 | mapping: Mapping, |
| 34 | sequence: []Item, | 57 | 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 | } |
| 35 | }; | 78 | }; |
| 36 | | 79 | |
| 37 | pub const Mapping = struct { | 80 | pub const Mapping = struct { |
| ... | @@ -64,6 +107,15 @@ pub const Mapping = struct { | ... | @@ -64,6 +107,15 @@ pub const Mapping = struct { |
| 64 | } | 107 | } |
| 65 | return list.items; | 108 | return list.items; |
| 66 | } | 109 | } |
| | 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 | } |
| 67 | }; | 119 | }; |
| 68 | | 120 | |
| 69 | // | 121 | // |