Skip to content

Commit 7a33a52

Browse files
Add parseYaml function for custom types
1 parent 1a0cc53 commit 7a33a52

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/Yaml.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ fn parseBoolean(self: Yaml, comptime T: type, value: Value) Error!T {
148148
fn parseUnion(self: Yaml, arena: Allocator, comptime T: type, value: Value) Error!T {
149149
const union_info = @typeInfo(T).@"union";
150150

151+
if (@hasDecl(T, "parseYaml")) {
152+
return T.parseYaml(self, arena, value);
153+
}
154+
151155
if (union_info.tag_type) |_| {
152156
inline for (union_info.fields) |field| {
153157
if (self.parseValue(arena, field.type, value)) |u_value| {
@@ -174,6 +178,10 @@ fn parseStruct(self: Yaml, arena: Allocator, comptime T: type, map: Map) Error!T
174178
const struct_info = @typeInfo(T).@"struct";
175179
var parsed: T = undefined;
176180

181+
if (@hasDecl(T, "parseYaml")) {
182+
return T.parseYaml(self, arena, .{ .map = map });
183+
}
184+
177185
inline for (struct_info.fields) |field| {
178186
var value: ?Value = map.get(field.name) orelse blk: {
179187
const field_name = try mem.replaceOwned(u8, arena, field.name, "_", "-");

0 commit comments

Comments
 (0)