| ... | @@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value: | ... | @@ -129,11 +129,11 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value: |
| 129 | out.writeAll(";\n") catch unreachable; | 129 | out.writeAll(";\n") catch unreachable; |
| 130 | } | 130 | } |
| 131 | | 131 | |
| | 132 | // TODO: non-recursive? |
| 132 | fn printLiteral(out: anytype, val: anytype, indent: u8) !void { | 133 | fn printLiteral(out: anytype, val: anytype, indent: u8) !void { |
| 133 | const T = @TypeOf(val); | 134 | const T = @TypeOf(val); |
| 134 | switch (@typeInfo(T)) { | 135 | switch (@typeInfo(T)) { |
| 135 | .Array => { | 136 | .Array => { |
| 136 | // TODO: non-recursive? | | |
| 137 | try out.print("{s} {{\n", .{@typeName(T)}); | 137 | try out.print("{s} {{\n", .{@typeName(T)}); |
| 138 | for (val) |item| { | 138 | for (val) |item| { |
| 139 | try out.writeByteNTimes(' ', indent + 4); | 139 | try out.writeByteNTimes(' ', indent + 4); |
| ... | @@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void { | ... | @@ -156,7 +156,20 @@ fn printLiteral(out: anytype, val: anytype, indent: u8) !void { |
| 156 | try out.writeByteNTimes(' ', indent); | 156 | try out.writeByteNTimes(' ', indent); |
| 157 | try out.writeAll("}"); | 157 | try out.writeAll("}"); |
| 158 | }, | 158 | }, |
| 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))})), |
| 160 | } | 173 | } |
| 161 | } | 174 | } |
| 162 | | 175 | |
| ... | @@ -283,6 +296,7 @@ test "OptionsStep" { | ... | @@ -283,6 +296,7 @@ test "OptionsStep" { |
| 283 | | 296 | |
| 284 | options.addOption(usize, "option1", 1); | 297 | options.addOption(usize, "option1", 1); |
| 285 | options.addOption(?usize, "option2", null); | 298 | options.addOption(?usize, "option2", null); |
| | 299 | options.addOption(?usize, "option3", 3); |
| 286 | options.addOption([]const u8, "string", "zigisthebest"); | 300 | options.addOption([]const u8, "string", "zigisthebest"); |
| 287 | options.addOption(?[]const u8, "optional_string", null); | 301 | options.addOption(?[]const u8, "optional_string", null); |
| 288 | options.addOption([2][2]u16, "nested_array", nested_array); | 302 | options.addOption([2][2]u16, "nested_array", nested_array); |
| ... | @@ -294,6 +308,7 @@ test "OptionsStep" { | ... | @@ -294,6 +308,7 @@ test "OptionsStep" { |
| 294 | try std.testing.expectEqualStrings( | 308 | try std.testing.expectEqualStrings( |
| 295 | \\pub const option1: usize = 1; | 309 | \\pub const option1: usize = 1; |
| 296 | \\pub const option2: ?usize = null; | 310 | \\pub const option2: ?usize = null; |
| | 311 | \\pub const option3: ?usize = 3; |
| 297 | \\pub const string: []const u8 = "zigisthebest"; | 312 | \\pub const string: []const u8 = "zigisthebest"; |
| 298 | \\pub const optional_string: ?[]const u8 = null; | 313 | \\pub const optional_string: ?[]const u8 = null; |
| 299 | \\pub const nested_array: [2][2]u16 = [2][2]u16 { | 314 | \\pub const nested_array: [2][2]u16 = [2][2]u16 { |