diff options
| author | Robby Zambito <contact@robbyzambito.me> | 2025-12-31 22:18:24 +0000 |
|---|---|---|
| committer | Robby Zambito <contact@robbyzambito.me> | 2026-01-01 05:22:02 +0000 |
| commit | b447883d106f0ac427b0b0a00a8015be8eb4730c (patch) | |
| tree | d8d46d8ed960d84b7d16b99adabb443c9499b885 /src/server/message_parser.zig | |
| parent | 7af7a30ed21663d510d368d0f936b08285bf092b (diff) | |
Reorganized things
Diffstat (limited to 'src/server/message_parser.zig')
| -rw-r--r-- | src/server/message_parser.zig | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/src/server/message_parser.zig b/src/server/message_parser.zig index 2165691..b156dd6 100644 --- a/src/server/message_parser.zig +++ b/src/server/message_parser.zig @@ -33,7 +33,7 @@ pub const MessageType = enum { pub const Message = union(MessageType) { info: ServerInfo, - connect: AllocatedConnect, + connect: Connect, @"pub": Pub, hpub: void, sub: Sub, @@ -71,14 +71,6 @@ pub const Message = union(MessageType) { /// feature. proto: u32 = 1, }; - pub const AllocatedConnect = struct { - allocator: std.heap.ArenaAllocator, - connect: Connect, - - pub fn deinit(self: AllocatedConnect) void { - self.allocator.deinit(); - } - }; pub const Connect = struct { verbose: bool = false, pedantic: bool = false, @@ -96,6 +88,39 @@ pub const Message = union(MessageType) { no_responders: ?bool = null, headers: ?bool = null, nkey: ?[]const u8 = null, + + pub fn deinit(self: Connect, alloc: std.mem.Allocator) void { + if (self.auth_token) |a| alloc.free(a); + if (self.user) |u| alloc.free(u); + if (self.pass) |p| alloc.free(p); + if (self.name) |n| alloc.free(n); + alloc.free(self.lang); + alloc.free(self.version); + if (self.sig) |s| alloc.free(s); + if (self.jwt) |j| alloc.free(j); + if (self.nkey) |n| alloc.free(n); + } + + pub fn dupe(self: Connect, alloc: std.mem.Allocator) !Connect { + return .{ + .verbose = self.verbose, + .pedantic = self.pedantic, + .tls_required = self.tls_required, + .auth_token = if (self.auth_token) |a| try alloc.dupe(u8, a) else null, + .user = if (self.user) |u| try alloc.dupe(u8, u) else null, + .pass = if (self.pass) |p| try alloc.dupe(u8, p) else null, + .name = if (self.name) |n| try alloc.dupe(u8, n) else null, + .lang = self.lang, + .version = self.version, + .protocol = self.protocol, + .echo = self.echo, + .sig = if (self.sig) |s| try alloc.dupe(u8, s) else null, + .jwt = if (self.jwt) |j| try alloc.dupe(u8, j) else null, + .no_responders = self.no_responders, + .headers = self.headers, + .nkey = if (self.nkey) |n| try alloc.dupe(u8, n) else null, + }; + } }; pub const Pub = struct { /// The destination subject to publish to. @@ -208,8 +233,8 @@ pub const Message = union(MessageType) { switch (operation) { .connect => { - // TODO: should be ARENA allocator var connect_arena_allocator: std.heap.ArenaAllocator = .init(alloc); + defer connect_arena_allocator.deinit(); const connect_allocator = connect_arena_allocator.allocator(); const connect_string_writer_allocating: std.Io.Writer.Allocating = try .initCapacity(connect_allocator, 1024); var connect_string_writer = connect_string_writer_allocating.writer; @@ -223,7 +248,7 @@ pub const Message = union(MessageType) { // TODO: should be CONNECTION allocator const res = try std.json.parseFromSliceLeaky(Connect, connect_allocator, connect_string_writer.buffered(), .{ .allocate = .alloc_always }); - return .{ .connect = .{ .allocator = connect_arena_allocator, .connect = res } }; + return .{ .connect = try res.dupe(alloc) }; }, .@"pub" => { try in.discardAll(1); // throw away space |
