summaryrefslogtreecommitdiff
path: root/src/lib/dispatch.zig
diff options
context:
space:
mode:
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;
+}