const ServerFeatures = packed struct {
yellowtext: bool,
flipping: bool,
customobjections: bool,
fastloading: bool,
noencryption: bool,
deskmod: bool,
evidence: bool,
cccc_ic_support: bool,
arup: bool,
casing_alerts: bool,
modcall_reason: bool,
looping_sfx: bool,
additive: bool,
effects: bool,
y_offset: bool,
expanded_desk_mods: bool,
auth_packet: bool,
fn fromString(s: []const u8) !ServerFeatures {
const E = comptime b: {
const fields = @typeInfo(ServerFeatures).Struct.fields;
var en_fi: [fields.len]std.builtin.Type.EnumField = undefined;
for(fields, &en_fi, 0..) |field, *d, i| {
d.* = .{.name = field.name, .value = i};
}
break :b @Type(std.builtin.Type{.Enum = .{.tag_type = @typeInfo(ServerFeatures).Struct.backing_integer.?, .fields = &en_fi, .decls = &.{}, .is_exhaustive = false}});
};
var sf: ServerFeatures = std.mem.zeroes(ServerFeatures);
switch(@enumToInt(std.meta.stringToEnum(E, s) orelse return error.Unknown)) {
inline 0...(@typeInfo(E).Enum.fields.len - 1) => |e| {
@field(sf, @tagName(@intToEnum(E, e))) = true;
},
else => unreachable,
}
return sf;
}
};
This is messy, but I can't think of anything better. Can anyone else?