const std = @import("std"); const u = @import("utils.zig"); pub fn Param( comptime T: type, comptime name: []const u8, comptime description: []const u8, comptime options: u.Options, ) type { if (options.optional == .default_value and options.optional.default_value == null and @typeInfo(T) != .optional) @compileError("a null default requires an optional param type, got: " ++ @typeName(T)); return struct { raw: []const u8, value: T, pub const info: u.Info = .{ .name = name, .description = u.writeDescription(description, options), .type_text = options.type_text orelse u.typeText(T), .optional = options.optional, .enum_values = options.enum_values, .max_length = options.max_length, .min_length = options.min_length, .max_value = options.max_value, .min_value = options.min_value, }; }; } pub fn String( comptime name: []const u8, comptime description: []const u8, comptime options: u.StringOptions, ) type { return Param([]const u8, name, description, .{ .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = value }, }, .type_text = options.type_text, .min_length = options.min_length, .max_length = options.max_length, .quote_default = true, }); } pub fn Flag( comptime name: []const u8, comptime description: []const u8, ) type { return struct { is_present: bool, pub const info: u.Info = .{ .name = name, .description = description, .type_text = u.flag, // this is mostly just a filler, switch is always false if it is not present .optional = .{ .default_value = "false" }, }; }; } pub const Switch = Flag; pub fn Enum( comptime T: type, comptime description: []const u8, comptime options: u.EnumOptions(T), ) type { const name = if (options.name) |value| value else blk: { const string = u.typeName(T); var buffer: [string.len:0]u8 = @splat(0); _ = std.ascii.lowerString(&buffer, string); const copy = buffer; break :blk © }; const Type: std.lang.Type = @typeInfo(T); const E = Type.@"enum"; return Param(T, name, description, .{ .type_text = options.type_text, // I'm not sure if I want field_names or decl_names .enum_values = E.field_names, .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = @tagName(value) }, }, }); } pub fn Float( comptime name: []const u8, comptime description: []const u8, comptime options: u.NumberOptions(f128), ) type { return Param(f128, name, description, .{ .type_text = options.type_text, .min_value = options.min, .max_value = options.max, .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = std.fmt.comptimePrint("{d}", .{value}) }, }, }); } pub fn Int( comptime name: []const u8, comptime description: []const u8, comptime options: u.NumberOptions(i128), ) type { return Param(i128, name, description, .{ .type_text = options.type_text, .min_value = if (options.min) |min| @floatFromInt(min) else null, .max_value = if (options.max) |max| @floatFromInt(max) else null, .optional = switch (options.optional) { .not => .not, .default_value => |value| .{ .default_value = std.fmt.comptimePrint("{d}", .{value}) }, }, }); }