feat: add Bytearray wrapper

This fixes some byte arrays being deserialized as []u32
This commit is contained in:
LordMZTE 2022-08-08 17:58:26 +02:00
parent 4c91738fd0
commit e9afbcf8ae
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
4 changed files with 89 additions and 8 deletions

View File

@ -0,0 +1,52 @@
const std = @import("std");
const getty = @import("getty");
const types = @import("../types.zig");
const Vis = struct {
pub usingnamespace getty.de.Visitor(
@This(),
types.Bytearray,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
visitSeq,
undefined,
undefined,
undefined,
undefined,
);
pub fn visitSeq(
_: @This(),
alloc: ?std.mem.Allocator,
comptime Deserializer: type,
input: anytype,
) Deserializer.Error!types.Bytearray {
var list = std.ArrayList(u8).init(alloc.?);
while (try input.nextElement(alloc, u8)) |element| {
try list.append(element);
}
return types.Bytearray{ .a = list.toOwnedSlice() };
}
};
pub fn is(comptime T: type) bool {
return T == types.Bytearray;
}
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.deserializeSeq(alloc, visitor);
}

View File

@ -50,7 +50,7 @@ pub fn createPlugin(comptime Plugin: type) void {
export fn update() void {
if (allocator == null) {
@panic("Got event while allocator is null! Did you forget to set it?");
@panic("Got event while allocator is null! Did you forget to set it?");
}
var ev = api.recvObj(types.Event) catch |err| {

View File

@ -4,6 +4,7 @@ const json = @import("json");
/// getty deserialization blocks required to properly deserialize messages
pub const dbs = .{
@import("deserialization_blocks/bytearray.zig"),
@import("deserialization_blocks/char.zig"),
@import("deserialization_blocks/char_or_arrow.zig"),
@import("deserialization_blocks/line_and_column.zig"),
@ -54,6 +55,37 @@ test "deserialize Char" {
defer deser.deinit();
}
/// A struct that wraps a bytearray.
/// This struct has some custom deserialization behaviour,
/// so that bytearrays aren't deserialized as strings.
pub const Bytearray = struct {
a: []u8,
};
test "deserialize Bytearray" {
const valid_json_src =
\\[
\\ 1,
\\ 2,
\\ 3,
\\ 4
\\]
;
var valid_deser = try OwnedDeserData(Bytearray)
.deserialize(std.testing.allocator, valid_json_src);
defer valid_deser.deinit();
const invalid_json_src =
\\"I'm a string!"
;
try std.testing.expectError(
error.InvalidType,
OwnedDeserData(Bytearray).deserialize(std.testing.allocator, invalid_json_src),
);
}
pub const EventType = enum {
ModeUpdate,
TabUpdate,
@ -515,11 +547,9 @@ pub const SearchOption = enum {
Wrap,
};
// TODO: Write, TabNameInput, PaneNameInput and SearchInput are actually `[]u8`,
// but getty thinks they have to be a string, and not byte arrays.
pub const Action = union(enum) {
Quit,
Write: []u32,
Write: Bytearray,
WriteChars: []u8,
SwitchToMode: InputMode,
Resize: ResizeDirection,
@ -547,7 +577,7 @@ pub const Action = union(enum) {
TogglePaneEmbedOrFloating,
ToggleFloatingPanes,
CloseFocus,
PaneNameInput: []u32,
PaneNameInput: Bytearray,
UndoRenamePane,
NewTab: ?TabLayout,
NoOp,
@ -556,7 +586,7 @@ pub const Action = union(enum) {
CloseTab,
GoToTab: u32,
ToggleTab,
TabNameInput: []u32,
TabNameInput: Bytearray,
UndoRenameTab,
Run: RunCommandAction,
Detach,
@ -568,7 +598,7 @@ pub const Action = union(enum) {
Confirm,
Deny,
SkipConfirm: *Action,
SearchInput: []u32,
SearchInput: Bytearray,
Search: SearchDirection,
SearchToggleOption: SearchOption,
};

View File

@ -7,4 +7,3 @@ pub extern "zellij" fn host_open_file() void;
pub extern "zellij" fn host_switch_tab_to(tab_idx: u32) void;
pub extern "zellij" fn host_set_timeout(secs: f64) void;
pub extern "zellij" fn host_exec_cmd() void;