Commit 76d4b1b823

Aaron Sikes <aaron@sikes.io>
2021-10-23 20:23:58
Better erroring for unsupported build option types
1 parent 808d1b8
Changed files (1)
lib
std
lib/std/build/OptionsStep.zig
@@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value:
     out.writeAll(";\n") catch unreachable;
 }
 
+// TODO: non-recursive?
 fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
     const T = @TypeOf(val);
     switch (@typeInfo(T)) {
         .Array => {
-            // TODO: non-recursive?
             try out.print("{s} {{\n", .{@typeName(T)});
             for (val) |item| {
                 try out.writeByteNTimes(' ', indent + 4);
@@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void {
             try out.writeByteNTimes(' ', indent);
             try out.writeAll("}");
         },
-        else => try out.print("{any}", .{val}),
+        .Optional => {
+            if (val) |inner| {
+                return printLiteral(out, inner, indent);
+            } else {
+                return out.writeAll("null");
+            }
+        },
+        .Void,
+        .Bool,
+        .Int,
+        .Float,
+        .Null,
+        => try out.print("{any}", .{val}),
+        else => @compileError(comptime std.fmt.comptimePrint("`{s}` are not yet supported as build options", .{@tagName(@typeInfo(T))})),
     }
 }
 
@@ -283,6 +296,7 @@ test "OptionsStep" {
 
     options.addOption(usize, "option1", 1);
     options.addOption(?usize, "option2", null);
+    options.addOption(?usize, "option3", 3);
     options.addOption([]const u8, "string", "zigisthebest");
     options.addOption(?[]const u8, "optional_string", null);
     options.addOption([2][2]u16, "nested_array", nested_array);
@@ -294,6 +308,7 @@ test "OptionsStep" {
     try std.testing.expectEqualStrings(
         \\pub const option1: usize = 1;
         \\pub const option2: ?usize = null;
+        \\pub const option3: ?usize = 3;
         \\pub const string: []const u8 = "zigisthebest";
         \\pub const optional_string: ?[]const u8 = null;
         \\pub const nested_array: [2][2]u16 = [2][2]u16 {