@@ -287,12 +287,11 @@ fn printValue(options: *Options, out: Writer, comptime T: type, value: T, indent
287
287
try out .writeByteNTimes (' ' , indent );
288
288
try out .writeAll ("}" );
289
289
},
290
- .@"union" = > | @ "union" | {
290
+ .@"union" = > {
291
291
try out .writeAll (".{ " );
292
292
switch (value ) {
293
293
inline else = > | payload , tag | {
294
- try printValue (options , out , @"union" .tag_type .? , tag , indent );
295
- try out .writeAll (" = " );
294
+ try out .print (".{p_} = " , .{std .zig .fmtId (@tagName (tag ))});
296
295
try printValue (options , out , @TypeOf (payload ), payload , indent );
297
296
},
298
297
}
@@ -477,32 +476,45 @@ test Options {
477
476
world : bool = true ,
478
477
};
479
478
479
+ const NormalUnion = union (NormalEnum ) {
480
+ foo : u16 ,
481
+ bar : [2 ]u8 ,
482
+ };
483
+
480
484
const NestedStruct = struct {
481
485
normal_struct : NormalStruct ,
482
486
normal_enum : NormalEnum = .foo ,
483
487
};
484
488
489
+ const NonExhaustiveEnum = enum (u8 ) {
490
+ one ,
491
+ two ,
492
+ three ,
493
+ _ ,
494
+ };
495
+
485
496
options .addOption (usize , "option1" , 1 );
486
497
options .addOption (? usize , "option2" , null );
487
498
options .addOption (? usize , "option3" , 3 );
488
499
options .addOption (comptime_int , "option4" , 4 );
500
+ options .addOption (comptime_float , "option5" , 0.125 );
489
501
options .addOption ([]const u8 , "string" , "zigisthebest" );
490
502
options .addOption (? []const u8 , "optional_string" , null );
491
503
options .addOption ([2 ][2 ]u16 , "nested_array" , nested_array );
492
504
options .addOption ([]const []const u16 , "nested_slice" , nested_slice );
493
505
options .addOption (KeywordEnum , "keyword_enum" , .@"0.8.1" );
494
506
options .addOption (std .SemanticVersion , "semantic_version" , try std .SemanticVersion .parse ("0.1.2-foo+bar" ));
495
- options .addOption (NormalEnum , "normal1_enum" , NormalEnum .foo );
496
- options .addOption (NormalEnum , "normal2_enum" , NormalEnum .bar );
497
- options .addOption (NormalStruct , "normal1_struct" , NormalStruct {
498
- .hello = "foo" ,
499
- });
500
- options .addOption (NormalStruct , "normal2_struct " , NormalStruct {
501
- . hello = null ,
502
- . world = false ,
503
- });
504
- options . addOption ( NestedStruct , "nested_struct" , NestedStruct {
505
- .normal_struct = . { .hello = "bar" },
507
+ options .addOption (NormalEnum , "normal1_enum" , .foo );
508
+ options .addOption (NormalEnum , "normal2_enum" , .bar );
509
+ options .addOption (NormalStruct , "normal1_struct" , .{ . hello = "foo" });
510
+ options . addOption ( NormalStruct , "normal2_struct" , .{ .hello = null , . world = false });
511
+ options . addOption ( NormalUnion , "normal_union" , .{ . bar = .{ 42 , 64 } });
512
+ options .addOption (NestedStruct , "nested_struct " , .{ . normal_struct = .{ . hello = "bar" } });
513
+ options . addOption ( NonExhaustiveEnum , "non_exhaustive_enum1" , .two );
514
+ options . addOption ( NonExhaustiveEnum , "non_exhaustive_enum2" , @enumFromInt ( 20 ));
515
+ options . addOption ([ 2 ] NormalStruct , "array_of_structs" , .{
516
+ .{ . hello = null },
517
+ .{ .hello = "zig" , . world = true },
506
518
});
507
519
508
520
try std .testing .expectEqualStrings (
@@ -514,6 +526,8 @@ test Options {
514
526
\\
515
527
\\pub const option4: comptime_int = 4;
516
528
\\
529
+ \\pub const option5: comptime_float = 1.25e-1;
530
+ \\
517
531
\\pub const string: []const u8 = "zigisthebest";
518
532
\\
519
533
\\pub const optional_string: ?[]const u8 = null;
@@ -578,6 +592,16 @@ test Options {
578
592
\\ .world = false,
579
593
\\};
580
594
\\
595
+ \\pub const @"Build.Step.Options.decltest.Options.NormalUnion" = union(@"Build.Step.Options.decltest.Options.NormalEnum") {
596
+ \\ foo: u16,
597
+ \\ bar: [2]u8,
598
+ \\};
599
+ \\
600
+ \\pub const normal_union: @"Build.Step.Options.decltest.Options.NormalUnion" = .{ .bar = .{
601
+ \\ 42,
602
+ \\ 64,
603
+ \\} };
604
+ \\
581
605
\\pub const @"Build.Step.Options.decltest.Options.NestedStruct" = struct {
582
606
\\ normal_struct: @"Build.Step.Options.decltest.Options.NormalStruct",
583
607
\\ normal_enum: @"Build.Step.Options.decltest.Options.NormalEnum" = .foo,
@@ -591,6 +615,28 @@ test Options {
591
615
\\ .normal_enum = .foo,
592
616
\\};
593
617
\\
618
+ \\pub const @"Build.Step.Options.decltest.Options.NonExhaustiveEnum" = enum(u8) {
619
+ \\ one = 0,
620
+ \\ two = 1,
621
+ \\ three = 2,
622
+ \\ _,
623
+ \\};
624
+ \\
625
+ \\pub const non_exhaustive_enum1: @"Build.Step.Options.decltest.Options.NonExhaustiveEnum" = .two;
626
+ \\
627
+ \\pub const non_exhaustive_enum2: @"Build.Step.Options.decltest.Options.NonExhaustiveEnum" = @enumFromInt(20);
628
+ \\
629
+ \\pub const array_of_structs: [2]@"Build.Step.Options.decltest.Options.NormalStruct" = .{
630
+ \\ .{
631
+ \\ .hello = null,
632
+ \\ .world = true,
633
+ \\ },
634
+ \\ .{
635
+ \\ .hello = "zig",
636
+ \\ .world = true,
637
+ \\ },
638
+ \\};
639
+ \\
594
640
\\
595
641
, options .contents .items );
596
642
0 commit comments