1//! ZON parsing and stringification.
2//!
3//! ZON ("Zig Object Notation") is a textual file format. Outside of `nan` and `inf` literals, ZON's
4//! grammar is a subset of Zig's.
5//!
6//! Supported Zig primitives:
7//! * boolean literals
8//! * number literals (including `nan` and `inf`)
9//! * character literals
10//! * enum literals
11//! * `null` literals
12//! * string literals
13//! * multiline string literals
14//!
15//! Supported Zig container types:
16//! * anonymous struct literals
17//! * anonymous tuple literals
18//!
19//! Here is an example ZON object:
20//! ```
21//! .{
22//! .a = 1.5,
23//! .b = "hello, world!",
24//! .c = .{ true, false },
25//! .d = .{ 1, 2, 3 },
26//! .e = .{ .x = 13, .y = 67 },
27//! }
28//! ```
29//!
30//! Individual primitives are also valid ZON, for example:
31//! ```
32//! "This string is a valid ZON object."
33//! ```
34//!
35//! ZON may not contain type names.
36//!
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.
39
40const std = @import("std");
41
42pub const parse = @import("zon/parse.zig");
43pub const stringify = @import("zon/stringify.zig");
44pub const Serializer = @import("zon/Serializer.zig");
45
46/// Returns a formatter that formats the given value using stringify.
47pub fn fmt(value: anytype, options: stringify.SerializeOptions) Formatter(@TypeOf(value)) {
48 return Formatter(@TypeOf(value)){ .value = value, .options = options };
49}
50
51test 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.
74pub 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
85test {
86 _ = parse;
87 _ = stringify;
88}