std: format non-exhaustive enums

master
daurnimator 2020-01-31 21:18:20 +11:00
parent b9f720365c
commit a351350b88
No known key found for this signature in database
GPG Key ID: 45B429A8F9D9D22A
1 changed files with 10 additions and 3 deletions

View File

@ -365,14 +365,21 @@ pub fn formatType(
try output(context, "error.");
return output(context, @errorName(value));
},
.Enum => {
.Enum => |enumInfo| {
if (comptime std.meta.trait.hasFn("format")(T)) {
return value.format(fmt, options, context, Errors, output);
}
try output(context, @typeName(T));
try output(context, ".");
return formatType(@tagName(value), "", options, context, Errors, output, max_depth);
if (enumInfo.is_exhaustive) {
try output(context, ".");
return formatType(@tagName(value), "", options, context, Errors, output, max_depth);
} else {
// TODO: when @tagName works on exhaustive enums print known enum strings
try output(context, "(");
try formatType(@enumToInt(value), "", options, context, Errors, output, max_depth);
try output(context, ")");
}
},
.Union => {
if (comptime std.meta.trait.hasFn("format")(T)) {