fn sliceToEnum(comptime T: type, slice: []const u8) !T T: typeslice: []const u8787
788
789
790
791
792
793
794fn sliceToEnum(comptime T: type, slice: []const u8) !T {
// Check for a named value.
if (std.meta.stringToEnum(T, slice)) |value| return value;
// Check for a numeric value.
if (!isNumberFormattedLikeAnInteger(slice)) return error.InvalidEnumTag;
const n = std.fmt.parseInt(@typeInfo(T).@"enum".tag_type, slice, 10) catch return error.InvalidEnumTag;
return std.enums.fromInt(T, n) orelse return error.InvalidEnumTag;
}