From 6bdc230417a150896c591c19f01150c5cdd05a92 Mon Sep 17 00:00:00 2001 From: LeRoyce Pearson Date: Thu, 10 Aug 2023 19:24:32 -0600 Subject: [PATCH] Add support for parsing standalone enum fields --- examples/01_client_connect.zig | 5 +++++ src/core.zig | 11 ++++------- src/main.zig | 5 +++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/examples/01_client_connect.zig b/examples/01_client_connect.zig index bdfac0b..a5e252f 100644 --- a/examples/01_client_connect.zig +++ b/examples/01_client_connect.zig @@ -196,6 +196,11 @@ pub fn main() !void { std.debug.print("<- xdg_toplevel@{} {s} {}\n", .{ header.object_id, @tagName(@as(std.meta.Tag(wayland.xdg.Toplevel.Event), @enumFromInt(header.size_and_opcode.opcode))), std.zig.fmtEscapes(std.mem.sliceAsBytes(message_buffer.items)) }); } else if (header.object_id == registry_done_id) { done = true; + } else if (header.object_id == shm_id) { + const event = try wayland.deserialize(wayland.core.Shm.Event, header, message_buffer.items); + switch (event) { + .format => |format| std.debug.print("<- format {} {}\n", .{ format.format, std.zig.fmtEscapes(std.mem.asBytes(&format.format)) }), + } } else if (header.object_id == 1) { const event = try wayland.deserialize(wayland.core.Display.Event, header, message_buffer.items); switch (event) { diff --git a/src/core.zig b/src/core.zig index 807f8d8..fd22086 100644 --- a/src/core.zig +++ b/src/core.zig @@ -144,13 +144,10 @@ pub const Shm = struct { }; }; - pub const Event = union(Event.Tag) { - format: Format, - - pub const Tag = enum(u16) { - @"error", - delete_id, - }; + pub const Event = union(enum) { + format: struct { + format: Format, + }, }; pub const Format = enum(u32) { diff --git a/src/main.zig b/src/main.zig index 2972cd9..3f74ea4 100644 --- a/src/main.zig +++ b/src/main.zig @@ -113,6 +113,11 @@ pub fn deserializeArguments(comptime Signature: type, buffer: []const u32) !Sign .signed => @field(result, field.name) = try readInt(buffer, &pos), .unsigned => @field(result, field.name) = try readUInt(buffer, &pos), }, + .Enum => |enum_info| if (@sizeOf(enum_info.tag_type) == @sizeOf(u32)) { + @field(result, field.name) = @enumFromInt(try readInt(buffer, &pos)); + } else { + @compileError("Unsupported type " ++ @typeName(field.type)); + }, .Pointer => |ptr| switch (ptr.size) { .Slice => if (ptr.child == u8) { @field(result, field.name) = try readString(buffer, &pos);