Skip to content

Commit 6091de8

Browse files
committed
Simpler API
1 parent 7d7f4e7 commit 6091de8

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

src/index_tests.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const std = @import("std");
22

33
const common = @import("common.zig");
4-
const Change = common.Change;
4+
const Change = @import("change.zig").Change;
55
const SearchResults = common.SearchResults;
66

77
const Scheduler = @import("utils/Scheduler.zig");

src/utils/msgpack/msgpack.zig

+21
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,27 @@ pub fn unpackerNoAlloc(reader: anytype) Unpacker(@TypeOf(reader), NoAllocator) {
880880
return Unpacker(@TypeOf(reader), NoAllocator).init(reader, .{});
881881
}
882882

883+
const UnpackOptions = struct {
884+
allocator: ?std.mem.Allocator = null,
885+
};
886+
887+
pub fn unpack(comptime T: type, reader: anytype, options: UnpackOptions) !T {
888+
if (options.allocator) |allocator| {
889+
return try unpacker(reader, allocator).read(T);
890+
} else {
891+
return try unpackerNoAlloc(reader).read(T);
892+
}
893+
}
894+
895+
pub fn unpackFromBytes(comptime T: type, bytes: []const u8, options: UnpackOptions) !T {
896+
var stream = std.io.fixedBufferStream(bytes);
897+
return try unpack(T, stream.reader(), options);
898+
}
899+
900+
pub fn pack(comptime T: type, writer: anytype, value: anytype) !void {
901+
return try packer(writer).write(T, value);
902+
}
903+
883904
test {
884905
_ = @import("msgpack_test.zig");
885906
}
+27-29
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
11
const std = @import("std");
22
const msgpack = @import("msgpack.zig");
33

4-
test "readBool: null" {
5-
const buffer = [_]u8{0xc0};
6-
var stream = std.io.fixedBufferStream(&buffer);
7-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
8-
try std.testing.expectEqual(null, try unpacker.read(?bool));
9-
}
4+
const packed_null = [_]u8{0xc0};
5+
const packed_true = [_]u8{0xc3};
6+
const packed_false = [_]u8{0xc2};
7+
const packed_zero = [_]u8{0x00};
108

119
test "readBool: false" {
12-
const buffer = [_]u8{0xc2};
13-
var stream = std.io.fixedBufferStream(&buffer);
14-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
15-
try std.testing.expectEqual(false, try unpacker.read(bool));
10+
try std.testing.expectEqual(false, try msgpack.unpackFromBytes(bool, &packed_false, .{}));
1611
}
1712

1813
test "readBool: true" {
19-
const buffer = [_]u8{0xc3};
20-
var stream = std.io.fixedBufferStream(&buffer);
21-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
22-
try std.testing.expectEqual(true, try unpacker.read(bool));
14+
try std.testing.expectEqual(true, try msgpack.unpackFromBytes(bool, &packed_true, .{}));
2315
}
2416

25-
test "readBool: wrong data" {
26-
const buffer = [_]u8{0x00};
27-
var stream = std.io.fixedBufferStream(&buffer);
28-
var unpacker = msgpack.unpackerNoAlloc(stream.reader());
29-
try std.testing.expectError(error.InvalidFormat, unpacker.read(bool));
17+
test "readBool: null" {
18+
try std.testing.expectEqual(null, try msgpack.unpackFromBytes(?bool, &packed_null, .{}));
19+
}
20+
21+
test "readBool: wrong type" {
22+
try std.testing.expectError(error.InvalidFormat, msgpack.unpackFromBytes(bool, &packed_zero, .{}));
3023
}
3124

3225
test "writeBool: false" {
33-
var buffer: [100]u8 = undefined;
34-
var stream = std.io.fixedBufferStream(&buffer);
35-
var packer = msgpack.packer(stream.writer());
36-
try packer.writeBool(false);
37-
try std.testing.expectEqualSlices(u8, &.{0xc2}, stream.getWritten());
26+
var output = std.ArrayList(u8).init(std.testing.allocator);
27+
defer output.deinit();
28+
try msgpack.pack(bool, output.writer(), false);
29+
try std.testing.expectEqualSlices(u8, &packed_false, output.items);
3830
}
3931

4032
test "writeBool: true" {
41-
var buffer: [100]u8 = undefined;
42-
var stream = std.io.fixedBufferStream(&buffer);
43-
var packer = msgpack.packer(stream.writer());
44-
try packer.writeBool(true);
45-
try std.testing.expectEqualSlices(u8, &.{0xc3}, stream.getWritten());
33+
var output = std.ArrayList(u8).init(std.testing.allocator);
34+
defer output.deinit();
35+
try msgpack.pack(bool, output.writer(), true);
36+
try std.testing.expectEqualSlices(u8, &packed_true, output.items);
37+
}
38+
39+
test "writeBool: null" {
40+
var output = std.ArrayList(u8).init(std.testing.allocator);
41+
defer output.deinit();
42+
try msgpack.pack(?bool, output.writer(), null);
43+
try std.testing.expectEqualSlices(u8, &packed_null, output.items);
4644
}

0 commit comments

Comments
 (0)