zig-zyrup/src/record.zig

44 lines
1.3 KiB
Zig

const std = @import("std");
const getty = @import("getty");
const Serializer = @import("ser.zig");
pub fn Record(comptime label: anytype, comptime tuple: anytype) type {
const T = @TypeOf(tuple);
if (!std.meta.trait.isTuple(T)) {
@compileError("Type passed into Record constructor must be a tuple.");
}
return struct {
label: @TypeOf(label) = label,
values: @TypeOf(tuple) = tuple,
const Self = @This();
pub fn init() Self {
return Self{};
}
pub const @"getty.sb" = struct {
pub fn serialize(
allocator: ?std.mem.Allocator,
value: anytype,
serializer: anytype,
) @TypeOf(serializer).Error!@TypeOf(serializer).Ok {
if (@TypeOf(serializer.context) == Serializer and @TypeOf(value) == Self) {
try serializer.context.writer.writeByte('<');
try getty.serialize(allocator, value.label, serializer);
inline for (value.values) |val| {
try getty.serialize(allocator, val, serializer);
}
try serializer.context.writer.writeByte('>');
}
}
};
};
}