mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 19:54:48 +00:00
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user