Improve niceTypeName, and also swap a warn to an err

This commit is contained in:
Lyssieth 2024-11-28 23:45:02 +02:00
parent 166fcc4abe
commit 6a6bffaa3b
Signed by untrusted user who does not match committer: lyssieth
GPG key ID: 200268854934CFAB
2 changed files with 11 additions and 1 deletions

View file

@ -38,7 +38,7 @@ fn parseEnumFromStr(comptime T: type, str: []const u8) !T {
// todo: maybe find a way to get Zig tests to run without stderr *unless* they fail? // todo: maybe find a way to get Zig tests to run without stderr *unless* they fail?
} }
log.warn("could not parse `{s}` as enum `{s}`", .{ str, @typeName(T) }); log.err("could not parse `{s}` as enum `{s}`", .{ str, @typeName(T) });
log.warn("hint: try one of the following:", .{}); log.warn("hint: try one of the following:", .{});
inline for (info.Enum.fields) |field| { inline for (info.Enum.fields) |field| {

View file

@ -11,14 +11,24 @@ pub fn niceTypeName(comptime T: type) []const u8 {
return "array"; return "array";
} }
if (std.mem.lastIndexOf(u8, name, ".")) |idx| {
return name[idx + 1 ..];
}
return name; return name;
} }
const t = std.testing; const t = std.testing;
test "nice type names" { test "nice type names" {
const Enum = enum(u8) {
ThisIs,
ATest,
};
try t.expectEqualStrings("string", niceTypeName([]const u8)); try t.expectEqualStrings("string", niceTypeName([]const u8));
try t.expectEqualStrings("array", niceTypeName(std.ArrayList(u8))); try t.expectEqualStrings("array", niceTypeName(std.ArrayList(u8)));
try t.expectEqualStrings("Enum", niceTypeName(Enum));
try t.expectEqualStrings("u8", niceTypeName(u8)); try t.expectEqualStrings("u8", niceTypeName(u8));
} }