summaryrefslogtreecommitdiff
path: root/src/util/struct.zig
blob: db41b09268c3025cf36fa7eb35276fd12a575ac2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const std = @import("std");

/// Maps each field of a struct `S` to the enum value in `E`
pub fn StructFieldsMap(
    comptime S: type,
    comptime E: type,
) type {
    const KV = struct { []const u8, E };
    var slice: []const KV = &[_]KV{};
    var idx: usize = 0;
    for (std.meta.fields(S)) |field| {
        slice = slice ++ &[_]KV{.{ field.name, @intToEnum(E, idx) }};
        idx += 1;
    }
    return std.ComptimeStringMap(E, slice);
}

/// Creates an enum with fields matching each field in struct `S`.
/// Runs `F` for each field in `S` to modify the names in the resulting enum.
// sort of opposite to the included EnumFieldStruct in std
pub fn StructFieldsEnum(
    comptime S: type,
    comptime F: ?fn (comptime []const u8) []const u8,
) type {
    const EnumField = std.builtin.Type.EnumField;
    var fields: []const EnumField = &[_]EnumField{};
    var idx: usize = 0;
    for (std.meta.fields(S)) |field| {
        fields = fields ++ &[_]EnumField{.{
            .name = blk: {
                if (F) |f| break :blk f(field.name);
                break :blk field.name;
            },
            .value = idx,
        }};
        idx += 1;
    }
    return @Type(.{ .Enum = .{
        .tag_type = usize,
        .fields = fields,
        .decls = &.{},
        .is_exhaustive = true,
    } });
}

const TestStruct = struct {
    a_a: bool,
    b_b: bool,
};

fn replaceUnderscores(comptime o: []const u8) []const u8 {
    var name: [o.len]u8 = undefined;
    _ = std.mem.replace(u8, o, &[_]u8{'_'}, &[_]u8{'-'}, &name);
    return &name;
}

test "StructFieldsEnum" {
    {
        const e = StructFieldsEnum(TestStruct, null);
        try std.testing.expect(std.mem.eql(u8, std.meta.fields(e)[0].name, "a_a"));
        try std.testing.expect(std.mem.eql(u8, std.meta.fields(e)[1].name, "b_b"));
    }

    {
        const e = StructFieldsEnum(TestStruct, replaceUnderscores);
        try std.testing.expect(std.mem.eql(u8, std.meta.fields(e)[0].name, "a-a"));
        try std.testing.expect(std.mem.eql(u8, std.meta.fields(e)[1].name, "b-b"));
    }
}

test "StructFieldsMap" {
    {
        const e = StructFieldsEnum(TestStruct, null);
        const map = StructFieldsMap(TestStruct, e);
        try std.testing.expect(std.mem.eql(u8, @tagName(map.get("a_a").?), "a_a"));
        try std.testing.expect(std.mem.eql(u8, @tagName(map.get("b_b").?), "b_b"));
    }

    {
        const e = StructFieldsEnum(TestStruct, replaceUnderscores);
        const map = StructFieldsMap(TestStruct, e);
        try std.testing.expect(std.mem.eql(u8, @tagName(map.get("a_a").?), "a-a"));
        try std.testing.expect(std.mem.eql(u8, @tagName(map.get("b_b").?), "b-b"));
    }
}