Skip to content

Commit

Permalink
Merge branch 'caleberi-add-support-for-yaml-bool'
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Dec 20, 2024
2 parents 60ac814 + 5217da6 commit d6e89cf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/yaml.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub const parse = @import("parse.zig");
const Node = parse.Node;
const Tree = parse.Tree;
const ParseError = parse.ParseError;
const supportedTruthyBooleanValue: [4][]const u8 = .{ "y", "yes", "on", "true" };
const supportedFalsyBooleanValue: [4][]const u8 = .{ "n", "no", "off", "false" };

pub const YamlError = error{
UnexpectedNodeType,
Expand All @@ -28,6 +30,7 @@ pub const Value = union(enum) {
empty,
int: i64,
float: f64,
boolean: bool,
string: []const u8,
list: List,
map: Map,
Expand All @@ -47,6 +50,11 @@ pub const Value = union(enum) {
return self.string;
}

pub fn asBool(self: Value) !bool {
if (self != .boolean) return error.TypeMismatch;
return self.boolean;
}

pub fn asList(self: Value) !List {
if (self != .list) return error.TypeMismatch;
return self.list;
Expand All @@ -68,6 +76,7 @@ pub const Value = union(enum) {
.int => |int| return writer.print("{}", .{int}),
.float => |float| return writer.print("{d}", .{float}),
.string => |string| return writer.print("{s}", .{string}),
.boolean => |bool_val| return writer.print("{}", .{bool_val}),
.list => |list| {
const len = list.len;
if (len == 0) return;
Expand Down Expand Up @@ -190,6 +199,21 @@ pub const Value = union(enum) {
return Value{ .float = float };
}

if (raw.len <= 5 and raw.len > 0) {
const lower_raw = try std.ascii.allocLowerString(arena, raw);
for (supportedTruthyBooleanValue) |v| {
if (std.mem.eql(u8, v, lower_raw)) {
return Value{ .boolean = true };
}
}

for (supportedFalsyBooleanValue) |v| {
if (std.mem.eql(u8, v, lower_raw)) {
return Value{ .boolean = false };
}
}
}

return Value{ .string = try arena.dupe(u8, value.string_value.items) };
} else {
log.debug("Unexpected node type: {}", .{node.tag});
Expand Down Expand Up @@ -370,6 +394,7 @@ pub const Yaml = struct {
fn parseValue(self: *Yaml, comptime T: type, value: Value) Error!T {
return switch (@typeInfo(T)) {
.int => math.cast(T, try value.asInt()) orelse return error.Overflow,
.bool => self.parseBoolean(bool, value),
.float => if (value.asFloat()) |float| {
return math.lossyCast(T, float);
} else |_| {
Expand All @@ -389,6 +414,11 @@ pub const Yaml = struct {
};
}

fn parseBoolean(self: *Yaml, comptime T: type, value: Value) Error!T {
_ = self;
return value.asBool();
}

fn parseUnion(self: *Yaml, comptime T: type, value: Value) Error!T {
const union_info = @typeInfo(T).@"union";

Expand Down
3 changes: 3 additions & 0 deletions test/simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ numbers:
- 10
- -8
- 6
isyaml: false
hasBoolean: NO
nested:
some: one
wick: john doe
ok: TRUE
finally: [ 8.17,
19.78 , 17 ,
21 ]
6 changes: 6 additions & 0 deletions test/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ test "simple" {
nested: struct {
some: []const u8,
wick: []const u8,
ok: bool,
},
finally: [4]f16,
isyaml: bool,
hasBoolean: bool,

pub fn eql(self: @This(), other: @This()) bool {
if (self.names.len != other.names.len) return false;
Expand Down Expand Up @@ -61,7 +64,10 @@ test "simple" {
.nested = .{
.some = "one",
.wick = "john doe",
.ok = true,
},
.isyaml = false,
.hasBoolean = false,
.finally = [_]f16{ 8.17, 19.78, 17, 21 },
};
try testing.expect(result.eql(expected));
Expand Down

0 comments on commit d6e89cf

Please sign in to comment.