Skip to content

Commit 2bc6d3d

Browse files
committed
v0.7.0
1 parent db885e1 commit 2bc6d3d

File tree

7 files changed

+568
-205
lines changed

7 files changed

+568
-205
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ You can customize how types are formatted in message pack:
8080

8181
See [examples](examples/examples.zig) for how to do it.
8282

83+
## Manual Encoding / Decoding
84+
85+
If you require the finest level of control over how data is encoded and decoded, the `lizpack.manual` API
86+
may suit your use case.
87+
88+
See [examples](examples/examples.zig) for how to do it.
89+
8390
## Examples
8491

8592
```zig

build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.{
22
.name = "lizpack",
3-
.version = "0.6.0",
3+
.version = "0.7.0",
44
.minimum_zig_version = "0.14.0-dev.2802+257054a14",
55
.paths = .{
66
"build.zig",

examples/examples.zig

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ test "array and slice format customizations" {
160160

161161
const format_as_bin: lizpack.FormatOptions(@TypeOf(my_string)) = .bin;
162162
const slice = try lizpack.encode(my_string, &out, .{ .format = format_as_bin });
163-
try std.testing.expectEqualSlices(u8, &.{ (lizpack.Spec.Format{ .bin_8 = {} }).encode(), 3, 'f', 'o', 'o' }, slice);
163+
try std.testing.expectEqualSlices(u8, &.{ (lizpack.spec.Format{ .bin_8 = {} }).encode(), 3, 'f', 'o', 'o' }, slice);
164164

165165
const format_as_string: lizpack.FormatOptions(@TypeOf(my_string)) = .str;
166166
const slice2 = try lizpack.encode(my_string, &out, .{ .format = format_as_string });
@@ -169,12 +169,12 @@ test "array and slice format customizations" {
169169
const format_as_array: lizpack.FormatOptions(@TypeOf(my_string)) = .array;
170170
const slice3 = try lizpack.encode(my_string, &out, .{ .format = format_as_array });
171171
try std.testing.expectEqualSlices(u8, &.{
172-
(lizpack.Spec.Format{ .fixarray = .{ .len = 3 } }).encode(),
173-
(lizpack.Spec.Format{ .uint_8 = {} }).encode(),
172+
(lizpack.spec.Format{ .fixarray = .{ .len = 3 } }).encode(),
173+
(lizpack.spec.Format{ .uint_8 = {} }).encode(),
174174
'f',
175-
(lizpack.Spec.Format{ .uint_8 = {} }).encode(),
175+
(lizpack.spec.Format{ .uint_8 = {} }).encode(),
176176
'o',
177-
(lizpack.Spec.Format{ .uint_8 = {} }).encode(),
177+
(lizpack.spec.Format{ .uint_8 = {} }).encode(),
178178
'o',
179179
}, slice3);
180180
}
@@ -188,12 +188,12 @@ test "union format customization" {
188188
pub const format_as_active_field: lizpack.FormatOptions(@This()) = .{ .layout = .active_field };
189189
};
190190

191-
const bytes_active_field: []const u8 = &.{(lizpack.Spec.Format{ .false = {} }).encode()};
191+
const bytes_active_field: []const u8 = &.{(lizpack.spec.Format{ .false = {} }).encode()};
192192
try std.testing.expectEqual(MyUnion{ .my_bool = false }, try lizpack.decode(MyUnion, bytes_active_field, .{ .format = MyUnion.format_as_active_field }));
193193

194194
const bytes_map: []const u8 = &.{
195-
(lizpack.Spec.Format{ .fixmap = .{ .n_elements = 1 } }).encode(),
196-
(lizpack.Spec.Format{ .fixstr = .{ .len = 5 } }).encode(),
195+
(lizpack.spec.Format{ .fixmap = .{ .n_elements = 1 } }).encode(),
196+
(lizpack.spec.Format{ .fixstr = .{ .len = 5 } }).encode(),
197197
'm',
198198
'y',
199199
'_',
@@ -219,15 +219,15 @@ test "maps" {
219219
const format: lizpack.FormatOptions(@TypeOf(roles)) = .{ .layout = .map_item_first_field_is_key };
220220

221221
const expected_bytes: []const u8 = &.{
222-
(lizpack.Spec.Format{ .fixmap = .{ .n_elements = 2 } }).encode(),
223-
(lizpack.Spec.Format{ .fixstr = .{ .len = 5 } }).encode(),
222+
(lizpack.spec.Format{ .fixmap = .{ .n_elements = 2 } }).encode(),
223+
(lizpack.spec.Format{ .fixstr = .{ .len = 5 } }).encode(),
224224
's',
225225
'a',
226226
'r',
227227
'a',
228228
'h',
229229
0,
230-
(lizpack.Spec.Format{ .fixstr = .{ .len = 3 } }).encode(),
230+
(lizpack.spec.Format{ .fixstr = .{ .len = 3 } }).encode(),
231231
'b',
232232
'o',
233233
'b',
@@ -246,3 +246,26 @@ test "encodeAlloc" {
246246
try std.testing.expectEqual(@as(usize, 12), slice.len);
247247
try std.testing.expectEqual(expected, lizpack.decode(@TypeOf(expected), slice, .{}));
248248
}
249+
250+
test "manual encoding" {
251+
const expected: []const u8 = &.{
252+
(lizpack.spec.Format{ .fixstr = .{ .len = 3 } }).encode(),
253+
'f',
254+
'o',
255+
'o',
256+
};
257+
const actual: lizpack.manual.MessagePackType = .{ .fixstr = "foo" };
258+
const encoded = try lizpack.manual.encodeAlloc(std.testing.allocator, actual);
259+
defer std.testing.allocator.free(encoded);
260+
try std.testing.expectEqualSlices(u8, expected, encoded);
261+
}
262+
263+
test "manual decoding" {
264+
const raw: []const u8 = &.{@bitCast(@as(i8, -15))};
265+
const decoded = try lizpack.manual.decodeAlloc(std.testing.allocator, raw);
266+
defer decoded.deinit();
267+
switch (decoded.value) {
268+
.negative_fixint => |payload| try std.testing.expectEqual(-15, payload),
269+
else => return error.Invalid,
270+
}
271+
}

0 commit comments

Comments
 (0)