summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2026-01-08 23:46:39 -0500
committerRobby Zambito <contact@robbyzambito.me>2026-01-08 23:46:49 -0500
commitf4b545f852fc52060f3b3fb55f69a8d7c52f8aa5 (patch)
treee8dc82422c5b08e5dc2f8a47e70ffe952385210b
parented9911596915bfaefaae7b784af30502a13027b0 (diff)
Improve errors in parse API
-rw-r--r--src/Server/parse.zig43
1 files changed, 22 insertions, 21 deletions
diff --git a/src/Server/parse.zig b/src/Server/parse.zig
index 2aca6e8..b8d979a 100644
--- a/src/Server/parse.zig
+++ b/src/Server/parse.zig
@@ -19,6 +19,12 @@ const message = @import("./message.zig");
const Message = message.Message;
const Payload = @import("./Payload.zig");
+pub const Error = error{
+ EndOfStream,
+ ReadFailed,
+ InvalidStream,
+};
+
const client_control = StaticStringMap(message.Control).initComptime(
.{
// {"INFO", .info},
@@ -36,7 +42,7 @@ const client_control = StaticStringMap(message.Control).initComptime(
},
);
-pub fn control(in: *Reader) !message.Control {
+pub fn control(in: *Reader) Error!message.Control {
const longest_ctrl = comptime blk: {
var min_len = 0;
for (client_control.keys()) |ctrl| {
@@ -54,7 +60,7 @@ pub fn control(in: *Reader) !message.Control {
in.toss(str.len);
return ctrl;
} else if (str.len >= longest_ctrl) {
- return error.InvalidControl;
+ return error.InvalidStream;
}
}
log.debug("filling more in control.", .{});
@@ -109,13 +115,13 @@ test control {
var in: std.testing.Reader = .init(&buf, &.{
.{ .buffer = "CONNECC" },
});
- try std.testing.expectError(error.InvalidControl, control(&in.interface));
+ try std.testing.expectError(error.InvalidStream, control(&in.interface));
}
}
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
-pub fn @"pub"(in: *Reader) !Message.Pub {
+pub fn @"pub"(in: *Reader) Error!Message.Pub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
@@ -127,7 +133,7 @@ pub fn @"pub"(in: *Reader) !Message.Pub {
if (in.buffered().len > iter.index) {
if (in.buffered()[iter.index] == '\r') {
const bytes_str = second;
- const bytes = try parseUnsigned(usize, bytes_str, 10);
+ const bytes = parseUnsigned(usize, bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + bytes + 4) {
try in.fill(iter.index + bytes + 4);
@@ -147,7 +153,7 @@ pub fn @"pub"(in: *Reader) !Message.Pub {
const reply_to = second;
if (iter.next()) |bytes_str| {
- const bytes = try parseUnsigned(usize, bytes_str, 10);
+ const bytes = parseUnsigned(usize, bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len > iter.index and in.buffered()[iter.index] == '\r') {
if (in.buffered().len < iter.index + bytes + 4) {
@@ -260,7 +266,7 @@ test @"pub" {
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
-pub fn sub(in: *Reader) !Message.Sub {
+pub fn sub(in: *Reader) Error!Message.Sub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
@@ -378,7 +384,7 @@ test sub {
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
-pub fn unsub(in: *Reader) !Message.Unsub {
+pub fn unsub(in: *Reader) Error!Message.Unsub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
@@ -403,7 +409,7 @@ pub fn unsub(in: *Reader) !Message.Unsub {
}
if (iter.next()) |max_msgs_str| {
if (in.buffered().len > iter.index and in.buffered()[iter.index] == '\r') {
- const max_msgs = try parseUnsigned(usize, max_msgs_str, 10);
+ const max_msgs = parseUnsigned(usize, max_msgs_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + 2) {
try in.fill(iter.index + 2);
@@ -429,7 +435,7 @@ pub fn unsub(in: *Reader) !Message.Unsub {
const sid = iter.next() orelse return error.EndOfStream;
const max_msgs = if (iter.next()) |max_msgs_str| blk: {
log.debug("max_msgs: {any}", .{max_msgs_str});
- break :blk try parseUnsigned(usize, max_msgs_str, 10);
+ break :blk parseUnsigned(usize, max_msgs_str, 10) catch return error.InvalidStream;
} else null;
return .{
.sid = sid,
@@ -515,7 +521,7 @@ test unsub {
/// The return value is owned by the reader passed to this function.
/// Operations that modify the readers buffer invalidates this value.
-pub fn hpub(in: *Reader) !Message.HPub {
+pub fn hpub(in: *Reader) Error!Message.HPub {
// TODO: Add pedantic option.
// See: https://docs.nats.io/reference/reference-protocols/nats-protocol#syntax-1
while (true) {
@@ -529,8 +535,8 @@ pub fn hpub(in: *Reader) !Message.HPub {
const header_bytes_str = second;
const total_bytes_str = third;
- const header_bytes = try parseUnsigned(usize, header_bytes_str, 10);
- const total_bytes = try parseUnsigned(usize, total_bytes_str, 10);
+ const header_bytes = parseUnsigned(usize, header_bytes_str, 10) catch return error.InvalidStream;
+ const total_bytes = parseUnsigned(usize, total_bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + total_bytes + 4) {
try in.fill(iter.index + total_bytes + 4);
@@ -555,8 +561,8 @@ pub fn hpub(in: *Reader) !Message.HPub {
const header_bytes_str = third;
if (iter.next()) |total_bytes_str| {
if (in.buffered().len > iter.index and in.buffered()[iter.index] == '\r') {
- const header_bytes = try parseUnsigned(usize, header_bytes_str, 10);
- const total_bytes = try parseUnsigned(usize, total_bytes_str, 10);
+ const header_bytes = parseUnsigned(usize, header_bytes_str, 10) catch return error.InvalidStream;
+ const total_bytes = parseUnsigned(usize, total_bytes_str, 10) catch return error.InvalidStream;
if (in.buffered().len < iter.index + total_bytes + 4) {
try in.fill(iter.index + total_bytes + 4);
@@ -624,12 +630,7 @@ test hpub {
// TODO: more tests
}
-pub fn connect(alloc: Allocator, in: *Reader) error{
- EndOfStream,
- ReadFailed,
- OutOfMemory,
- InvalidStream,
-}!Message.Connect {
+pub fn connect(alloc: Allocator, in: *Reader) (error{OutOfMemory} || Error)!Message.Connect {
// for storing the json string
var connect_string_writer_allocating: AllocatingWriter = .init(alloc);
defer connect_string_writer_allocating.deinit();