@@ -13,6 +13,8 @@ pub const parse = @import("parse.zig");
1313const Node = parse .Node ;
1414const Tree = parse .Tree ;
1515const ParseError = parse .ParseError ;
16+ const supportedTruthyBooleanValue : [4 ][]const u8 = .{ "y" , "yes" , "on" , "true" };
17+ const supportedFalsyBooleanValue : [4 ][]const u8 = .{ "n" , "no" , "off" , "false" };
1618
1719pub const YamlError = error {
1820 UnexpectedNodeType ,
@@ -28,6 +30,7 @@ pub const Value = union(enum) {
2830 empty ,
2931 int : i64 ,
3032 float : f64 ,
33+ boolean : bool ,
3134 string : []const u8 ,
3235 list : List ,
3336 map : Map ,
@@ -47,6 +50,11 @@ pub const Value = union(enum) {
4750 return self .string ;
4851 }
4952
53+ pub fn asBool (self : Value ) ! bool {
54+ if (self != .boolean ) return error .TypeMismatch ;
55+ return self .boolean ;
56+ }
57+
5058 pub fn asList (self : Value ) ! List {
5159 if (self != .list ) return error .TypeMismatch ;
5260 return self .list ;
@@ -68,6 +76,7 @@ pub const Value = union(enum) {
6876 .int = > | int | return writer .print ("{}" , .{int }),
6977 .float = > | float | return writer .print ("{d}" , .{float }),
7078 .string = > | string | return writer .print ("{s}" , .{string }),
79+ .boolean = > | bool_val | return writer .print ("{}" , .{bool_val }),
7180 .list = > | list | {
7281 const len = list .len ;
7382 if (len == 0 ) return ;
@@ -190,6 +199,21 @@ pub const Value = union(enum) {
190199 return Value { .float = float };
191200 }
192201
202+ if (raw .len <= 5 and raw .len > 0 ) {
203+ const lower_raw = try std .ascii .allocLowerString (arena , raw );
204+ for (supportedTruthyBooleanValue ) | v | {
205+ if (std .mem .eql (u8 , v , lower_raw )) {
206+ return Value { .boolean = true };
207+ }
208+ }
209+
210+ for (supportedFalsyBooleanValue ) | v | {
211+ if (std .mem .eql (u8 , v , lower_raw )) {
212+ return Value { .boolean = false };
213+ }
214+ }
215+ }
216+
193217 return Value { .string = try arena .dupe (u8 , value .string_value .items ) };
194218 } else {
195219 log .debug ("Unexpected node type: {}" , .{node .tag });
@@ -370,6 +394,7 @@ pub const Yaml = struct {
370394 fn parseValue (self : * Yaml , comptime T : type , value : Value ) Error ! T {
371395 return switch (@typeInfo (T )) {
372396 .int = > math .cast (T , try value .asInt ()) orelse return error .Overflow ,
397+ .bool = > self .parseBoolean (bool , value ),
373398 .float = > if (value .asFloat ()) | float | {
374399 return math .lossyCast (T , float );
375400 } else | _ | {
@@ -389,6 +414,11 @@ pub const Yaml = struct {
389414 };
390415 }
391416
417+ fn parseBoolean (self : * Yaml , comptime T : type , value : Value ) Error ! T {
418+ _ = self ;
419+ return value .asBool ();
420+ }
421+
392422 fn parseUnion (self : * Yaml , comptime T : type , value : Value ) Error ! T {
393423 const union_info = @typeInfo (T ).@"union" ;
394424
0 commit comments