Convert from Zig struct to C struct

This commit is contained in:
2025-04-23 07:31:38 -04:00
parent ce21b94a43
commit d459dd60ef
2 changed files with 33 additions and 4 deletions

View File

@@ -4,11 +4,38 @@ const c = @cImport({
fn zigToCMessage(msg: ?*zaprus.Message) ?*c.SaprusMessage {
if (msg) |m| {
return switch (m.*) {
.relay => |_| @constCast(&c.SaprusMessage{}),
.connection => |_| @constCast(&c.SaprusMessage{}),
else => |_| null,
var res = c.SaprusMessage{
.packet_type = @intFromEnum(m.*),
};
switch (m.*) {
.relay => |r| {
res.unnamed_0 = .{ .relay = .{
.unnamed_0 = .{
.dest = r.header.dest,
},
.payload_len = r.payload.len,
.payload = (allocator.alloc(u8, r.payload.len) catch return null).ptr,
} };
},
.connection => |con| {
res.unnamed_0 = .{
.connection = .{
.unnamed_0 = .{
.src_port = con.header.src_port,
.dest_port = con.header.dest_port,
.seq_num = con.header.seq_num,
.msg_id = con.header.msg_id,
._reserved = con.header.reserved,
.options = @bitCast(con.header.options),
},
.payload_len = con.payload.len,
.payload = (allocator.alloc(u8, con.payload.len) catch return null).ptr,
},
};
},
else => return null,
}
return &res;
} else return null;
}