| ... | ... | @@ -124,7 +124,27 @@ pub fn addOption(self: *OptionsStep, comptime T: type, name: []const u8, value: |
| 124 | 124 | }, |
| 125 | 125 | else => {}, |
| 126 | 126 | } |
| 127 | | out.print("pub const {}: {s} = {};\n", .{ std.zig.fmtId(name), @typeName(T), value }) catch unreachable; |
| 127 | out.print("pub const {}: {s} = ", .{ std.zig.fmtId(name), @typeName(T) }) catch unreachable; |
| 128 | printLiteral(out, value, 0) catch unreachable; |
| 129 | out.writeAll(";\n") catch unreachable; |
| 130 | } |
| 131 | |
| 132 | fn printLiteral(out: anytype, val: anytype, indent: u8) !void { |
| 133 | const T = @TypeOf(val); |
| 134 | switch (@typeInfo(T)) { |
| 135 | .Array => { |
| 136 | // TODO: non-recursive? |
| 137 | try out.print("{s}{{\n", .{@typeName(T)}); |
| 138 | for (val) |item| { |
| 139 | try out.writeByteNTimes(' ', indent + 4); |
| 140 | try printLiteral(out, item, indent + 4); |
| 141 | try out.writeAll(",\n"); |
| 142 | } |
| 143 | try out.writeByteNTimes(' ', indent); |
| 144 | try out.writeAll("}"); |
| 145 | }, |
| 146 | else => try out.print("{any}", .{val}), |
| 147 | } |
| 128 | 148 | } |
| 129 | 149 | |
| 130 | 150 | /// The value is the path in the cache dir. |
| ... | ... | @@ -246,6 +266,7 @@ test "OptionsStep" { |
| 246 | 266 | options.addOption(?usize, "option2", null); |
| 247 | 267 | options.addOption([]const u8, "string", "zigisthebest"); |
| 248 | 268 | options.addOption(?[]const u8, "optional_string", null); |
| 269 | options.addOption([2][2]u16, "array", [2][2]u16{ [2]u16{ 300, 200 }, [2]u16{ 300, 200 } }); |
| 249 | 270 | options.addOption(KeywordEnum, "keyword_enum", .@"0.8.1"); |
| 250 | 271 | options.addOption(std.builtin.Version, "version", try std.builtin.Version.parse("0.1.2")); |
| 251 | 272 | options.addOption(std.SemanticVersion, "semantic_version", try std.SemanticVersion.parse("0.1.2-foo+bar")); |
| ... | ... | @@ -255,6 +276,16 @@ test "OptionsStep" { |
| 255 | 276 | \\pub const option2: ?usize = null; |
| 256 | 277 | \\pub const string: []const u8 = "zigisthebest"; |
| 257 | 278 | \\pub const optional_string: ?[]const u8 = null; |
| 279 | \\pub const array: [2][2]u16 = [2][2]u16{ |
| 280 | \\ [2]u16{ |
| 281 | \\ 300, |
| 282 | \\ 200, |
| 283 | \\ }, |
| 284 | \\ [2]u16{ |
| 285 | \\ 300, |
| 286 | \\ 200, |
| 287 | \\ }, |
| 288 | \\}; |
| 258 | 289 | \\pub const KeywordEnum = enum { |
| 259 | 290 | \\ @"0.8.1", |
| 260 | 291 | \\}; |