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; }