summaryrefslogtreecommitdiff
path: root/src/lib/dispatch.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2026-10-20 03:35:56 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2026-10-20 03:35:56 -0500
commitafac4c8c7780cc40bb41d8bae97a4ae83a79db9f (patch)
tree63b7efe0b995630002a662738013d3fcb885df29 /src/lib/dispatch.zig
parent73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690 (diff)
downloadnvlope-afac4c8c7780cc40bb41d8bae97a4ae83a79db9f.tar.gz
Finished minimal message impl
Diffstat (limited to 'src/lib/dispatch.zig')
-rw-r--r--src/lib/dispatch.zig32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/lib/dispatch.zig b/src/lib/dispatch.zig
new file mode 100644
index 0000000..d4c83b8
--- /dev/null
+++ b/src/lib/dispatch.zig
@@ -0,0 +1,32 @@
+const std = @import("std");
+const nvcmd = @import("commands.zig");
+const nvparse = @import("parse.zig");
+const nvrply = @import("reply.zig");
+const Reply = nvrply.Reply;
+const Command = nvcmd.Command;
+const Allocator = std.mem.Allocator;
+const Io = std.Io;
+const Action = nvrply.Action;
+const parse = nvparse.parse;
+
+pub fn dispatch(comptime H: type, h: *H, arena: Allocator, cmd: Command) !Reply {
+ switch (cmd) {
+ inline else => |payload, tag| {
+ const name = @tagName(tag);
+ if (@hasDecl(H, name)) return @field(H, name)(h, arena, payload);
+ return Reply.single(.not_impl, "Command not implemented");
+ },
+ }
+}
+
+pub fn handleLine(comptime H: type, h: *H, arena: Allocator, line: []const u8, w: *Io.Writer) !Action {
+ const reply = if (parse(line)) |cmd|
+ try dispatch(H, h, arena, cmd)
+ else |err| switch (err) {
+ error.UnknownCommand => Reply.single(.bad_cmd, "Command not recognized"),
+ error.BadParam => Reply.single(.bad_param, "Syntax error in parameters"),
+ };
+ try reply.send(w);
+ try w.flush();
+ return reply.action;
+}