format bit flags as set

This changes to format output from:

    MyFlagType{ .first_bit = true, second_bit = false, _reserved_bit_0 = false, _reserved_bit_1 = false }

to the following:

    MyFlagType{ first_bit }

That is, we only show bits which are `true`.
This commit is contained in:
Christofer Nolander
2023-08-23 20:00:15 +02:00
parent ed9401c72e
commit 5b99c4992a

View File

@@ -49,6 +49,7 @@ const preamble =
\\ pub fn contains(lhs: FlagsType, rhs: FlagsType) bool {
\\ return toInt(intersect(lhs, rhs)) == toInt(rhs);
\\ }
++ bit_flag_format ++
\\ };
\\}
\\pub fn makeApiVersion(variant: u3, major: u7, minor: u10, patch: u12) u32 {
@@ -69,6 +70,31 @@ const preamble =
\\
;
const bit_flag_format =
\\ pub fn format(
\\ self: @This(),
\\ comptime _: []const u8,
\\ _: std.fmt.FormatOptions,
\\ writer: anytype,
\\ ) !void {
\\ try writer.writeAll(@typeName(@This()) ++ "{");
\\ var first = true;
\\ inline for (comptime std.meta.fieldNames(@This())) |name| {
\\ if (name[0] == '_') continue;
\\ if (@field(self, name)) {
\\ if (first) {
\\ try writer.writeAll(" " ++ name);
\\ first = false;
\\ } else {
\\ try writer.writeAll(", " ++ name);
\\ }
\\ }
\\ }
\\ if (!first) try writer.writeAll(" ");
\\ try writer.writeAll("}");
\\ }
;
const builtin_types = std.ComptimeStringMap([]const u8, .{
.{ "void", @typeName(void) },
.{ "char", @typeName(u8) },
@@ -1060,6 +1086,7 @@ fn Renderer(comptime WriterType: type) type {
\\ }
\\ return true;
\\ }
++ bit_flag_format ++
\\ };
\\}
\\