From afac4c8c7780cc40bb41d8bae97a4ae83a79db9f Mon Sep 17 00:00:00 2001 From: Nic Gaffney Date: Tue, 20 Oct 2026 03:35:56 -0500 Subject: Finished minimal message impl --- src/lib/dispatch.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/lib/dispatch.zig (limited to 'src/lib/dispatch.zig') 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; +} -- cgit v1.2.3