Reorganized things

This commit is contained in:
2025-12-31 22:18:24 +00:00
parent 7af7a30ed2
commit b447883d10
4 changed files with 131 additions and 142 deletions

View File

@@ -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