48 lines
1.1 KiB
Zig
48 lines
1.1 KiB
Zig
const std = @import("std");
|
|
|
|
const enumError = error{EnumFieldNotFound};
|
|
|
|
pub fn nameToEnumTag(name: []const u8, comptime Enum: type) enumError!Enum {
|
|
comptime {
|
|
if (@typeInfo(Enum) != .Enum) {
|
|
@compileError("Non enum type passed into function: " ++ @typeName(Enum));
|
|
}
|
|
}
|
|
|
|
inline for (@typeInfo(Enum).Enum.fields) |field| {
|
|
if (std.mem.eql(u8, name, field.name)) {
|
|
const tag: Enum = @enumFromInt(field.value);
|
|
return tag;
|
|
}
|
|
}
|
|
return enumError.EnumFieldNotFound;
|
|
}
|
|
|
|
test {
|
|
const State = enum {
|
|
good,
|
|
bad,
|
|
};
|
|
|
|
const enum_tag = try nameToEnumTag("good", State);
|
|
try std.testing.expect(enum_tag == State.good);
|
|
}
|
|
|
|
pub fn printGir() void {
|
|
std.debug.print(
|
|
\\ n n
|
|
\\ o ( )
|
|
\\ ___/_ / I am \
|
|
\\ / / . o ( your )
|
|
\\ 0> 0 / \ Doom! /
|
|
\\ Lu__/ ( )
|
|
\\ || - u -
|
|
\\ o^ ^ o
|
|
\\ _/ / / \_
|
|
\\ |__|
|
|
\\ O=> (O
|
|
\\
|
|
\\
|
|
, .{});
|
|
}
|