| ... | @@ -37,10 +37,51 @@ | ... | @@ -37,10 +37,51 @@ |
| 37 | //! ZON does not have syntax for pointers, but the parsers will allocate as needed to match the | 37 | //! ZON does not have syntax for pointers, but the parsers will allocate as needed to match the |
| 38 | //! given Zig types. Similarly, the serializer will traverse pointers. | 38 | //! given Zig types. Similarly, the serializer will traverse pointers. |
| 39 | | 39 | |
| | 40 | const std = @import("std"); |
| | 41 | |
| 40 | pub const parse = @import("zon/parse.zig"); | 42 | pub const parse = @import("zon/parse.zig"); |
| 41 | pub const stringify = @import("zon/stringify.zig"); | 43 | pub const stringify = @import("zon/stringify.zig"); |
| 42 | pub const Serializer = @import("zon/Serializer.zig"); | 44 | pub const Serializer = @import("zon/Serializer.zig"); |
| 43 | | 45 | |
| | 46 | /// Returns a formatter that formats the given value using stringify. |
| | 47 | pub fn fmt(value: anytype, options: stringify.SerializeOptions) Formatter(@TypeOf(value)) { |
| | 48 | return Formatter(@TypeOf(value)){ .value = value, .options = options }; |
| | 49 | } |
| | 50 | |
| | 51 | test fmt { |
| | 52 | const expectFmt = std.testing.expectFmt; |
| | 53 | try expectFmt("123", "{f}", .{fmt(@as(u32, 123), .{})}); |
| | 54 | try expectFmt( |
| | 55 | \\.{ |
| | 56 | \\ .num = 927, |
| | 57 | \\ .msg = "hello", |
| | 58 | \\ .sub = .{ .mybool = true }, |
| | 59 | \\} |
| | 60 | , "{f}", .{fmt(struct { |
| | 61 | num: u32, |
| | 62 | msg: []const u8, |
| | 63 | sub: struct { |
| | 64 | mybool: bool, |
| | 65 | }, |
| | 66 | }{ |
| | 67 | .num = 927, |
| | 68 | .msg = "hello", |
| | 69 | .sub = .{ .mybool = true }, |
| | 70 | }, .{})}); |
| | 71 | } |
| | 72 | |
| | 73 | /// Formats the given value using stringify. |
| | 74 | pub fn Formatter(comptime T: type) type { |
| | 75 | return struct { |
| | 76 | value: T, |
| | 77 | options: stringify.SerializeOptions, |
| | 78 | |
| | 79 | pub fn format(self: @This(), writer: *std.Io.Writer) std.Io.Writer.Error!void { |
| | 80 | try stringify.serialize(self.value, self.options, writer); |
| | 81 | } |
| | 82 | }; |
| | 83 | } |
| | 84 | |
| 44 | test { | 85 | test { |
| 45 | _ = parse; | 86 | _ = parse; |
| 46 | _ = stringify; | 87 | _ = stringify; |