feature. See also
. The project being documented here (as the example) is the Zig library itself.
Options.printStruct
fn printStruct(
options: *Options,
out: *std.ArrayList(u8),
comptime T: type,
comptime val: std.builtin.Type.Struct,
indent: u8,
) !void
File
Code
fn printStruct(
options: *Options,
out: *std.ArrayList(u8),
comptime T: type,
comptime val: std.builtin.Type.Struct,
indent: u8,
) !void {
const gpa = options.step.owner.allocator;
const gop = try options.encountered_types.getOrPut(gpa, @typeName(T));
if (gop.found_existing) return;
try out.appendNTimes(gpa, ' ', indent);
try out.print(gpa, "pub const {f} = ", .{std.zig.fmtId(@typeName(T))});
switch (val.layout) {
.@"extern" => try out.appendSlice(gpa, "extern struct"),
.@"packed" => try out.appendSlice(gpa, "packed struct"),
else => try out.appendSlice(gpa, "struct"),
}
try out.appendSlice(gpa, " {\n");
inline for (val.field_names, val.field_types, val.field_attrs) |field_name, field_type, field_attrs| {
try out.appendNTimes(gpa, ' ', indent);
const type_name = @typeName(field_type);
if (std.mem.containsAtLeast(u8, type_name, 1, ".")) {
try out.print(gpa, " {f}: {f}", .{
std.zig.fmtIdFlags(field_name, .{ .allow_underscore = true, .allow_primitive = true }),
std.zig.fmtId(type_name),
});
} else {
try out.print(gpa, " {f}: {s}", .{
std.zig.fmtIdFlags(field_name, .{ .allow_underscore = true, .allow_primitive = true }),
type_name,
});
}
if (field_attrs.defaultValue(field_type)) |default_value| {
try out.appendSlice(gpa, " = ");
switch (@typeInfo(field_type)) {
.@"enum" => try out.print(gpa, ".{s},\n", .{@tagName(default_value)}),
.@"struct" => |info| {
try printStructValue(options, out, info, default_value, indent + 4);
},
else => try printType(options, out, field_type, default_value, indent, null),
}
} else {
try out.appendSlice(gpa, ",\n");
}
}
try out.appendNTimes(gpa, ' ', indent);
try out.appendSlice(gpa, "};\n");
inline for (val.field_types) |field_type| {
try printUserDefinedType(options, out, field_type, 0);
}
}