StaticStringMap no field named '0' in struct

I wanted to try out the StaticStringMap to determine the hardware configuration:

const std = @import("std");

const LedControl = enum {
    NoLeds,
    Rukr,
    Gpio,
};

const AdcConverter = enum {
    Mcp3004,
    Ads1015,
};

const HwConfig = struct {
    led_control: LedControl,
    adc: AdcConverter,
    slots: u8,
};

pub fn main() !void {
    const hw_map = std.static_string_map.StaticStringMap(HwConfig);
    hw_map.initComptime(.{
        .{ "Moduline Mini V1.03", .{ .Rukr, .Ads1015, 4 } },
        .{ "Moduline Mini V1.05", .{ .Rukr, .Mcp3004, 4 } },
        .{ "Moduline Mini V1.06", .{ .Rukr, .Mcp3004, 4 } },
        .{ "Moduline Mini V1.07", .{ .Rukr, .Mcp3004, 4 } },
        .{ "Moduline Mini V1.10", .{ .Rukr, .Mcp3004, 4 } },
        .{ "Moduline Mini V1.11", .{ .Rukr, .Mcp3004, 4 } },
        .{ "Moduline IV V3.00", .{ .Gpio, .Ads1015, 8 } },
        .{ "Moduline IV V3.01", .{ .Gpio, .Ads1015, 8 } },
        .{ "Moduline IV V3.02", .{ .Rukr, .Ads1015, 8 } },
        .{ "Moduline IV V3.03", .{ .Rukr, .Ads1015, 8 } },
        .{ "Moduline IV V3.04", .{ .Rukr, .Ads1015, 8 } },
        .{ "Moduline IV V3.05", .{ .Rukr, .Ads1015, 8 } },
        .{ "Moduline IV V3.06", .{ .Rukr, .Mcp3004, 8 } },
        .{ "Moduline IV V3.06-D", .{ .Rukr, .Mcp3004, 8 } },
        .{ "Moduline Display V1.01", .{ .NoLeds, .Mcp3004, 2 } },
        .{ "Moduline Display V1.02", .{ .NoLeds, .Mcp3004, 2 } },
        .{ "Moduline Display V1.03", .{ .NoLeds, .Mcp3004, 2 } },
        .{ "Moduline Display V1.04", .{ .NoLeds, .Mcp3004, 2 } },
        .{ "Moduline Display V1.05", .{ .NoLeds, .Mcp3004, 2 } },
        .{ "Moduline Display V1.06", .{ .NoLeds, .Mcp3004, 2 } },
    });
    const hw_buffer: [50]u8 = undefined;
    var hw_file = try std.fs.openFileAbsolute("/sys/firmware/devicetree/base/hardware", .{});
    const bytes = hw_file.read(hw_buffer) orelse 0;
    const current_hw: HwConfig = if (bytes == 0)
        .{ .Rukr, .Mcp3004 }
    else
        hw_map.get(hw_buffer[0..bytes]) orelse .{ .Rukr, .Mcp3004 };
    std.debug.print("hardware {}", .{current_hw});
}

but when I try to compile this I get this error:

/home/maud/zig/0.13.0/files/lib/std/static_string_map.zig:163:59: error: no field named '0' in struct 'main.HwConfig'
                sorted_vals[i] = if (V == void) {} else kv.@"1";
                                                        ~~^~~~~
src/main.zig:14:18: note: struct declared here
const HwConfig = struct {
                 ^~~~~~
/home/maud/zig/0.13.0/files/lib/std/static_string_map.zig:85:35: note: called from here
                self.initSortedKVs(kvs_list, &sorted_keys, &sorted_vals);
                ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.zig:22:24: note: called from here
    hw_map.initComptime(.{
    ~~~~~~~~~~~~~~~~~~~^
referenced by:
    callMain: /home/maud/zig/0.13.0/files/lib/std/start.zig:524:32
    callMainWithArgs: /home/maud/zig/0.13.0/files/lib/std/start.zig:482:12
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

I can’t figure out what I’m doing wrong, it’s probably something easy that missing.

I think you have to specify the field names:

.{ "Moduline Mini V1.03", .{
    .led_control = .Rukr,
    .adc = .Ads1015,
    .slots = 4,
} },
3 Likes

There were some other issues too, but that was it. Thanks!

1 Like