const std = @import("std"); const assert = std.debug.assert; pub fn EnumStringUtils(TargetEnum: anytype, str_to_tag_mapping: anytype) type { if (str_to_tag_mapping.len != @typeInfo(TargetEnum).Enum.fields.len) { @compileLog("Mapping is not exhaustive"); } const EnumMapping = std.StaticStringMap(TargetEnum).initComptime(str_to_tag_mapping); return struct { pub fn fromString(str: []const u8) ?TargetEnum { return EnumMapping.get(str); } pub fn toString(value: TargetEnum) []const u8 { inline for (str_to_tag_mapping) |mapping| { if (mapping[1] == value) { return mapping[0]; } } unreachable; } }; }