feat: add support for encoder members
This commit is contained in:
parent
a6174af2ec
commit
37021a266b
1 changed files with 12 additions and 0 deletions
12
src/cbor.zig
12
src/cbor.zig
|
@ -215,6 +215,9 @@ pub fn writeValue(writer: anytype, value: anytype) @TypeOf(writer).Error!void {
|
|||
.error_union => return if (value) |v| writeValue(writer, v) else |err| writeValue(writer, err),
|
||||
.error_set => return writeErrorset(writer, value),
|
||||
.@"union" => |info| {
|
||||
if (std.meta.hasFn(T, "cborEncode")) {
|
||||
return value.cborEncode(writer);
|
||||
}
|
||||
if (info.tag_type) |TagType| {
|
||||
comptime var v = void;
|
||||
inline for (info.fields) |u_field| {
|
||||
|
@ -231,6 +234,9 @@ pub fn writeValue(writer: anytype, value: anytype) @TypeOf(writer).Error!void {
|
|||
}
|
||||
},
|
||||
.@"struct" => |info| {
|
||||
if (std.meta.hasFn(T, "cborEncode")) {
|
||||
return value.cborEncode(writer);
|
||||
}
|
||||
if (info.is_tuple) {
|
||||
if (info.fields.len == 0) return writeNull(writer);
|
||||
try writeArrayHeader(writer, info.fields.len);
|
||||
|
@ -277,6 +283,12 @@ pub fn writeValue(writer: anytype, value: anytype) @TypeOf(writer).Error!void {
|
|||
64 => try writeF64(writer, value),
|
||||
else => @compileError("cannot write type '" ++ @typeName(T) ++ "' to cbor stream"),
|
||||
},
|
||||
.@"enum" => {
|
||||
if (std.meta.hasFn(T, "cborEncode")) {
|
||||
return value.cborEncode(writer);
|
||||
}
|
||||
return writeString(writer, @tagName(value));
|
||||
},
|
||||
else => @compileError("cannot write type '" ++ @typeName(T) ++ "' to cbor stream"),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue