Parse ints and percentage properly

This commit is contained in:
Ivan Duran
2024-11-10 21:34:59 +03:00
parent e6df8453c6
commit cc705a0e2f

View File

@ -498,13 +498,13 @@ pub const Action = union(enum) {
return std.meta.stringToEnum(T, value) orelse return Error.InvalidFormat; return std.meta.stringToEnum(T, value) orelse return Error.InvalidFormat;
} }
fn parseInt(comptime T: type, value: []const u8) !T { fn parseInt(comptime T: type, value: []const u8, comptime ParentType: ?type) !T {
return switch (T) { return switch (ParentType orelse void) {
Action.Percentage => blk: { Action.SplitParameter => blk: {
if (value.len < 2) return Error.InvalidFormat; if (value.len < 2) return Error.InvalidFormat;
if (value[value.len - 1] != '%') return Error.InvalidFormat; if (value[value.len - 1] != '%') return Error.InvalidFormat;
const percent = value[0 .. value.len - 1]; const percent = value[0 .. value.len - 1];
const parsed_percent = std.fmt.parseInt(u16, percent, 10) catch return Error.InvalidFormat; const parsed_percent = std.fmt.parseInt(T, percent, 10) catch return Error.InvalidFormat;
const clamped_percent: u16 = @min(parsed_percent, 100); const clamped_percent: u16 = @min(parsed_percent, 100);
break :blk clamped_percent; break :blk clamped_percent;
}, },
@ -522,7 +522,7 @@ pub const Action = union(enum) {
) !field.type { ) !field.type {
return switch (@typeInfo(field.type)) { return switch (@typeInfo(field.type)) {
.Enum => try parseEnum(field.type, param), .Enum => try parseEnum(field.type, param),
.Int => try parseInt(field.type, param), .Int => try parseInt(field.type, param, null),
.Float => try parseFloat(field.type, param), .Float => try parseFloat(field.type, param),
.Struct => |info| blk: { .Struct => |info| blk: {
// Only tuples are supported to avoid ambiguity with field // Only tuples are supported to avoid ambiguity with field
@ -535,7 +535,7 @@ pub const Action = union(enum) {
const next = it.next() orelse return Error.InvalidFormat; const next = it.next() orelse return Error.InvalidFormat;
@field(value, field_.name) = switch (@typeInfo(field_.type)) { @field(value, field_.name) = switch (@typeInfo(field_.type)) {
.Enum => try parseEnum(field_.type, next), .Enum => try parseEnum(field_.type, next),
.Int => try parseInt(field_.type, next), .Int => try parseInt(field_.type, next, field.type),
.Float => try parseFloat(field_.type, next), .Float => try parseFloat(field_.type, next),
else => unreachable, else => unreachable,
}; };