Skip to content

Commit f164d06

Browse files
author
Andrew Hart
committed
Support bool output
1 parent fc9b3dd commit f164d06

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/Yaml.zig

+4
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,10 @@ pub const Value = union(enum) {
537537
.float,
538538
=> return Value{ .scalar = try std.fmt.allocPrint(arena, "{d}", .{input}) },
539539

540+
// TODO - Support per-field option to use yes/no or on/off instead of true/false
541+
.bool,
542+
=> return Value{ .scalar = try std.fmt.allocPrint(arena, "{}", .{input}) },
543+
540544
.@"struct" => |info| if (info.is_tuple) {
541545
var list: std.ArrayListUnmanaged(Value) = .empty;
542546
try list.ensureTotalCapacityPrecise(arena, info.fields.len);

src/Yaml/test.zig

+30
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ test "several integer bases" {
113113
try testing.expectEqualSlices(i8, &[_]i8{ 10, -10, 16, -16, 8, -8 }, &arr);
114114
}
115115

116+
test "bools" {
117+
const source =
118+
\\- false
119+
\\- true
120+
\\- off
121+
\\- on
122+
\\- no
123+
\\- yes
124+
\\- n
125+
\\- y
126+
;
127+
128+
var yaml: Yaml = .{ .source = source };
129+
defer yaml.deinit(testing.allocator);
130+
try yaml.load(testing.allocator);
131+
132+
try testing.expectEqual(yaml.docs.items.len, 1);
133+
134+
var arena = Arena.init(testing.allocator);
135+
defer arena.deinit();
136+
137+
const arr = try yaml.parse(arena.allocator(), [8]bool);
138+
try testing.expectEqualSlices(bool, &[_]bool{ false, true, false, true, false, true, false, true, }, &arr);
139+
}
140+
116141
const TestEnum = enum {
117142
alpha,
118143
bravo,
@@ -687,6 +712,11 @@ test "stringify a list" {
687712
try testStringify("[ 1, 2, 3 ]", arr);
688713
}
689714

715+
test "stringify a bool" {
716+
try testStringify("false", false);
717+
try testStringify("true", true);
718+
}
719+
690720
test "stringify an enum" {
691721
try testStringify("alpha", TestEnum.alpha);
692722
try testStringify("bravo", TestEnum.bravo);

0 commit comments

Comments
 (0)