diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-04-03 15:50:13 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-04-03 16:02:31 -0400 |
commit | 748991d35b5552475941fb75e3309e11c6d27529 (patch) | |
tree | 83a3c6c4e3df53564a7c302d92e7e59295df08cf | |
parent | f1fa898299931181131c5632d8ee6d0afbe4693f (diff) |
dedup some
Still need to clean up fromBytesAux
-rw-r--r-- | src/main.zig | 75 |
1 files changed, 35 insertions, 40 deletions
diff --git a/src/main.zig b/src/main.zig index 4f0f964..b4ae666 100644 --- a/src/main.zig +++ b/src/main.zig @@ -63,23 +63,9 @@ const SaprusMessage = union(SaprusPacketType) { try w.writeInt(u16, @intFromEnum(self), .big); switch (self) { - .relay => |r| { - const payload_list = try encodeToList(r.payload, allocator); - defer payload_list.deinit(); - - try w.writeStructEndian(r.header, .big); - try w.writeInt(u16, @intCast(payload_list.items.len), .big); - try w.writeAll(payload_list.items); - }, + .relay => |r| try toBytesAux(Relay.Header, r.header, r.payload, w, allocator), .file_transfer => return SaprusError.NotImplementedSaprusType, - .connection => |c| { - const payload_list = try encodeToList(c.payload, allocator); - defer payload_list.deinit(); - - try w.writeStructEndian(c.header, .big); - try w.writeInt(u16, @intCast(payload_list.items.len), .big); - try w.writeAll(payload_list.items); - }, + .connection => |c| try toBytesAux(Connection.Header, c.header, c.payload, w, allocator), } return buf.toOwnedSlice(); @@ -90,36 +76,45 @@ const SaprusMessage = union(SaprusPacketType) { const r = s.reader(); switch (@as(SaprusPacketType, @enumFromInt(try r.readInt(u16, .big)))) { - .relay => { - const header = try r.readStructEndian(Relay.Header, .big); - const len = try r.readInt(u16, .big); - const payload = try decodeFromReader(r, len, allocator); - - return .{ - .relay = .{ - .header = header, - .payload = payload, - }, - }; - }, + .relay => return fromBytesAux(.relay, Relay.Header, r, allocator), .file_transfer => return SaprusError.NotImplementedSaprusType, - .connection => { - const header = try r.readStructEndian(Connection.Header, .big); - const len = try r.readInt(u16, .big); - const payload = try decodeFromReader(r, len, allocator); - - return .{ - .connection = .{ - .header = header, - .payload = payload, - }, - }; - }, + .connection => return fromBytesAux(.connection, Connection.Header, r, allocator), else => return SaprusError.UnknownSaprusType, } } }; +inline fn toBytesAux(comptime Header: type, header: Header, payload: []const u8, w: std.ArrayList(u8).Writer, allocator: Allocator) !void { + const payload_list = try encodeToList(payload, allocator); + defer payload_list.deinit(); + + try w.writeStructEndian(header, .big); + try w.writeInt(u16, @intCast(payload_list.items.len), .big); + try w.writeAll(payload_list.items); +} + +inline fn fromBytesAux(comptime Packet: SaprusPacketType, comptime Header: type, r: StringReader, allocator: Allocator) !SaprusMessage { + const header = try r.readStructEndian(Header, .big); + const len = try r.readInt(u16, .big); + const payload = try decodeFromReader(r, len, allocator); + + return switch (Packet) { + .relay => SaprusMessage{ + .relay = .{ + .header = header, + .payload = payload, + }, + }, + .connection => SaprusMessage{ + .connection = .{ + .header = header, + .payload = payload, + }, + }, + else => unreachable, + }; +} + pub fn main() !void { var dba: ?DebugAllocator = if (comptime is_debug) DebugAllocator.init else null; defer if (dba) |*d| { |