fediglam/src/main/query.zig

54 lines
1.3 KiB
Zig
Raw Normal View History

2022-10-08 07:51:22 +00:00
const std = @import("std");
const ParamIter = struct {
remaining: []const u8,
target: []const u8,
fn next(self: *ParamIter) ?[]const u8 {
//
_ = self;
unreachable;
}
};
pub fn getParam(str: []const u8, param: []const u8) !?[]const u8 {
var iter = ParamIter{ .remaining = str, .target = param };
const result = iter.next() orelse return null;
if (iter.next() != null) return error.TooMany;
return result;
}
fn isScalarType(comptime T: type) bool {
return switch (T) {
[]const u8 => true,
else => switch (@typeInfo(T)) {
.Int, .Float, .Bool => true,
.Optional => |info| isScalarType(info.child),
.Enum => |info| if (info.is_exhaustive)
true
else
@compileError("Unsupported type " ++ @typeName(T)),
.Struct => false,
else => @compileError("Unsupported type " ++ @typeName(T)),
},
};
}
pub fn parseQueryArgs(comptime T: type, str: []const u8) !T {
var result = std.mem.zeroInit(T, .{});
_ = str;
for (std.meta.fields(T)) |field| {
const ParseType = switch (@typeInfo(field.field_type)) {
.Optional => |info| info.child,
else => field.field_type,
};
_ = ParseType;
}
return result;
}