1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
}
|