zellzig/src/deserialization_blocks/char_or_arrow.zig
2022-11-29 16:20:03 +01:00

45 lines
1 KiB
Zig

const std = @import("std");
const getty = @import("getty");
const types = @import("../types.zig");
const Vis = struct {
pub usingnamespace getty.de.Visitor(
@This(),
types.CharOrArrow,
.{ .visitString = visitString },
);
pub fn visitString(
_: @This(),
_: ?std.mem.Allocator,
comptime Deserializer: type,
input: anytype,
) Deserializer.Error!types.CharOrArrow {
if (input.len == 1) {
return types.CharOrArrow{ .Char = .{ .c = input[0] } };
}
return types.CharOrArrow{
.Direction = std.meta.stringToEnum(types.Direction, input) orelse
return error.UnknownVariant,
};
}
};
pub fn is(comptime T: type) bool {
return T == types.CharOrArrow;
}
pub fn Visitor(comptime _: type) type {
return Vis;
}
pub fn deserialize(
alloc: ?std.mem.Allocator,
comptime _: type,
deserializer: anytype,
visitor: anytype,
) !@TypeOf(visitor).Value {
return try deserializer.deserializeString(alloc, visitor);
}