authorgravatar for aaron@sikes.ioAaron Sikes <aaron@sikes.io> 2021-10-23 14:23:58-04:00
committergravatar for aaron@sikes.ioAaron Sikes <aaron@sikes.io> 2021-10-23 14:25:02-04:00
log76d4b1b823a6fbca20e24b37c4be97a5541eebf4
tree8ff6350ed52358cb8fa12948416baef70321257d
parent808d1b84a8349dc62597b22c7093df61fa3585d3

Better erroring for unsupported build option types


1 files changed, 17 insertions(+), 2 deletions(-)

lib/std/build/OptionsStep.zig+17-2
......@@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value:
129129 out.writeAll(";\n") catch unreachable;
130130}
131131
132// TODO: non-recursive?
132133fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
133134 const T = @TypeOf(val);
134135 switch (@typeInfo(T)) {
135136 .Array => {
136 // TODO: non-recursive?
137137 try out.print("{s} {{\n", .{@typeName(T)});
138138 for (val) |item| {
139139 try out.writeByteNTimes(' ', indent + 4);
......@@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
156156 try out.writeByteNTimes(' ', indent);
157157 try out.writeAll("}");
158158 },
159 else => try out.print("{any}", .{val}),
159 .Optional => {
160 if (val) |inner| {
161 return printLiteral(out, inner, indent);
162 } else {
163 return out.writeAll("null");
164 }
165 },
166 .Void,
167 .Bool,
168 .Int,
169 .Float,
170 .Null,
171 => try out.print("{any}", .{val}),
172 else => @compileError(comptime std.fmt.comptimePrint("`{s}` are not yet supported as build options", .{@tagName(@typeInfo(T))})),
160173 }
161174}
162175
......@@ -283,6 +296,7 @@ test "OptionsStep" {
283296
284297 options.addOption(usize, "option1", 1);
285298 options.addOption(?usize, "option2", null);
299 options.addOption(?usize, "option3", 3);
286300 options.addOption([]const u8, "string", "zigisthebest");
287301 options.addOption(?[]const u8, "optional_string", null);
288302 options.addOption([2][2]u16, "nested_array", nested_array);
......@@ -294,6 +308,7 @@ test "OptionsStep" {
294308 try std.testing.expectEqualStrings(
295309 \\pub const option1: usize = 1;
296310 \\pub const option2: ?usize = null;
311 \\pub const option3: ?usize = 3;
297312 \\pub const string: []const u8 = "zigisthebest";
298313 \\pub const optional_string: ?[]const u8 = null;
299314 \\pub const nested_array: [2][2]u16 = [2][2]u16 {