2022-11-26 20:29:23 -08:00
|
|
|
const std = @import("std");
|
|
|
|
const builtin = std.builtin;
|
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
const params = @import("./params.zig");
|
2022-11-26 20:29:23 -08:00
|
|
|
|
|
|
|
pub fn stringHandler(comptime UserContext: type) HandlerType(.{ .UserContext = UserContext, .Output = []const u8 }) {
|
|
|
|
return struct {
|
|
|
|
pub fn handler(_: UserContext, buf: []const u8) ![]const u8 {
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}.handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn intHandler(comptime UserContext: type, comptime IntType: type) HandlerType(.{ .UserContext = UserContext, .Output = IntType }) {
|
|
|
|
return struct {
|
|
|
|
pub fn handler(_: UserContext, buf: []const u8) std.fmt.ParseIntError!IntType {
|
|
|
|
return try std.fmt.parseInt(IntType, buf, 0);
|
|
|
|
}
|
|
|
|
}.handler;
|
|
|
|
}
|
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
pub fn HandlerType(comptime args: params.ParameterArgs) type {
|
2022-11-26 20:29:23 -08:00
|
|
|
return *const fn (args.UserContext, []const u8) anyerror!args.Output;
|
|
|
|
}
|
|
|
|
|
2022-11-27 01:31:20 -08:00
|
|
|
pub fn getDefaultHandler(comptime args: params.ParameterArgs) ?HandlerType(args) {
|
2022-11-26 20:29:23 -08:00
|
|
|
switch (@typeInfo(args.Output)) {
|
|
|
|
.Optional => |info| return getDefaultHandler(.{ .Output = info.child, .UserContext = args.user }),
|
|
|
|
.Int => return intHandler(args.UserContext, args.Output),
|
|
|
|
.Pointer => |info| {
|
|
|
|
if (info.size == .Slice and info.child == u8) {
|
|
|
|
return stringHandler(args.UserContext);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
else => return null,
|
|
|
|
}
|
|
|
|
}
|