This commit is contained in:
2025-11-19 09:49:32 -05:00
parent 5471cbe155
commit dbf189adc0
2 changed files with 29 additions and 15 deletions

View File

@@ -123,15 +123,24 @@ fn parsePub(in: *std.Io.Reader) !Message.Pub {
// try returning error in debug mode, only null in release?
pub fn parseNextMessage(alloc: std.mem.Allocator, in: *std.Io.Reader) ?Message {
const message_type: MessageType = blk: {
const word: []const u8 = (in.takeDelimiter(' ') catch return null) orelse return null;
std.debug.print("word: {s}\n", .{word});
break :blk MessageType.parse(word) orelse return null;
var word: ["CONNECT".len]u8 = undefined;
var len: usize = 0;
for (&word, 0..) |*b, i| {
const byte = in.takeByte() catch return null;
if (std.ascii.isUpper(byte)) {
b.* = byte;
len = i + 1;
} else break;
}
std.debug.print("word: '{s}'\n", .{word[0..len]});
break :blk MessageType.parse(word[0..len]) orelse return null;
};
// defer in.toss(2); // CRLF
std.debug.print("buffered: '{s}'\n", .{in.buffered()});
defer in.toss(2); // CRLF
return switch (message_type) {
.connect => .{ .connect = parseJsonMessage(Message.Connect, alloc, in) catch return null },
.@"pub" => .{ .@"pub" = parsePub(in) catch |err| std.debug.panic("{}", .{err}) },
.ping => .{ .ping = {} },
.ping => .ping,
else => null,
};
}