From e1f1084a39dfa3c3ccd14fcf302eed479046f534 Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Wed, 2 Apr 2025 02:44:26 +0200 Subject: [PATCH 1/3] serialiser --- src/deserialise.zig | 18 +-- src/root.zig | 72 ++++++++---- src/serialise.zig | 219 +++++++++++++++++++++++++++++++++++ tests/deserialise/binary.zig | 16 +-- tests/deserialise/int.zig | 2 +- tests/deserialise/root.zig | 67 ++++++++++- tests/deserialise/string.zig | 21 ++-- tests/deserialise/uint.zig | 2 +- tests/root.zig | 1 + tests/serialise/binary.zig | 82 +++++++++++++ tests/serialise/int.zig | 43 +++++++ tests/serialise/root.zig | 83 +++++++++++++ tests/serialise/string.zig | 84 ++++++++++++++ tests/serialise/uint.zig | 39 +++++++ 14 files changed, 698 insertions(+), 51 deletions(-) create mode 100644 src/serialise.zig create mode 100644 tests/serialise/binary.zig create mode 100644 tests/serialise/int.zig create mode 100644 tests/serialise/root.zig create mode 100644 tests/serialise/string.zig create mode 100644 tests/serialise/uint.zig diff --git a/src/deserialise.zig b/src/deserialise.zig index bf82602..cd3a694 100644 --- a/src/deserialise.zig +++ b/src/deserialise.zig @@ -36,7 +36,7 @@ fn deserialise_float(bytes: []const u8) DeserialiseError!ObjectLen { const f: f32 = @bitCast(std.mem.readInt(u32, payload[0..4], .big)); - return .{ .bytes_read = 5, .obj = .{ .float = f } }; + return .{ .bytes_read = 5, .obj = .{ .float32 = f } }; }, 0b1 => { // float 64 if (payload.len != 8) { @@ -45,7 +45,7 @@ fn deserialise_float(bytes: []const u8) DeserialiseError!ObjectLen { const f: f64 = @bitCast(std.mem.readInt(u64, payload[0..8], .big)); - return .{ .bytes_read = 9, .obj = .{ .float = f } }; + return .{ .bytes_read = 9, .obj = .{ .float64 = f } }; }, } } @@ -154,7 +154,7 @@ fn deserialise_int(bytes: []const u8) DeserialiseError!ObjectLen { } } -fn deserialise_raw(alloc: std.mem.Allocator, bytes: []const u8, len: usize, comptime kind: enum { str, binary }) DeserialiseError!Object { +fn deserialise_raw(alloc: std.mem.Allocator, bytes: []const u8, len: usize, comptime kind: enum { string, binary }) DeserialiseError!Object { std.debug.assert(bytes.len == len); if (!utils.validateUtf8(bytes)) { @@ -164,8 +164,8 @@ fn deserialise_raw(alloc: std.mem.Allocator, bytes: []const u8, len: usize, comp const s = try alloc.dupe(u8, bytes); const obj = switch (kind) { - .str => Object{ .raw = .{ .string = s } }, - .binary => Object{ .raw = .{ .binary = s } }, + .string => Object{ .string = s }, + .binary => Object{ .binary = s }, }; return obj; @@ -280,7 +280,7 @@ pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) Dese return error.BadLength; } - const obj = try deserialise_raw(alloc, payload[0..len], len, .str); + const obj = try deserialise_raw(alloc, payload[0..len], len, .string); return .{ .bytes_read = 1 + len, .obj = obj }; } @@ -468,7 +468,7 @@ pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) Dese const len: usize = payload[0]; - const obj = try deserialise_raw(alloc, payload[1..], len, .str); + const obj = try deserialise_raw(alloc, payload[1..], len, .string); return .{ .bytes_read = 1 + 1 + len, .obj = obj }; }, @@ -479,7 +479,7 @@ pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) Dese const len: usize = std.mem.readInt(u16, payload[0..2], .big); - const obj = try deserialise_raw(alloc, payload[2..], len, .str); + const obj = try deserialise_raw(alloc, payload[2..], len, .string); return .{ .bytes_read = 1 + 2 + len, .obj = obj }; }, @@ -490,7 +490,7 @@ pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) Dese const len: usize = std.mem.readInt(u32, payload[0..4], .big); - const obj = try deserialise_raw(alloc, payload[4..], len, .str); + const obj = try deserialise_raw(alloc, payload[4..], len, .string); return .{ .bytes_read = 1 + 4 + len, .obj = obj }; }, diff --git a/src/root.zig b/src/root.zig index 17ca194..c154e04 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,18 +1,19 @@ const std = @import("std"); -pub const deserialise = @import("deserialise.zig"); +pub const deserialise = @import("deserialise.zig").deserialise; +pub const serialise = @import("serialise.zig").serialise; -pub const Raw = union(enum) { - string: []u8, - binary: []u8, +// pub const Raw = union(enum) { +// string: []u8, +// binary: []u8, - pub fn deinit(self: Raw, alloc: std.mem.Allocator) void { - switch (self) { - .string => |s| alloc.free(s), - .binary => |b| alloc.free(b), - } - } -}; +// pub fn deinit(self: Raw, alloc: std.mem.Allocator) void { +// switch (self) { +// .string => |s| alloc.free(s), +// .binary => |b| alloc.free(b), +// } +// } +// }; pub const MapEntry = struct { key: Object, @@ -29,15 +30,18 @@ pub const Object = union(enum) { bool: bool, integer: i64, uinteger: u64, - float: f64, - raw: Raw, + float32: f32, + float64: f64, + string: []const u8, + binary: []const u8, array: []Object, map: []MapEntry, extension: struct { type: u8, bytes: []u8 }, pub fn deinit(self: Object, alloc: std.mem.Allocator) void { switch (self) { - .raw => |raw| raw.deinit(alloc), + .string => |s| alloc.free(s), + .binary => |b| alloc.free(b), .array => |array| { for (array) |x| { x.deinit(alloc); @@ -59,12 +63,40 @@ pub const Object = union(enum) { } }; +pub const Tag = enum(u8) { + nil = 0xc0, + false = 0xc2, + tru = 0xc3, + bin8 = 0xc4, + bin16 = 0xc5, + bin32 = 0xc6, + ext8 = 0xc7, + ext16 = 0xc8, + ext32 = 0xc9, + float32 = 0xca, + float64 = 0xcb, + uint8 = 0xcc, + uint16 = 0xcd, + uint32 = 0xce, + uint64 = 0xcf, + int8 = 0xd0, + int16 = 0xd1, + int32 = 0xd2, + int64 = 0xd3, + fixext1 = 0xd4, + fixext2 = 0xd5, + fixext4 = 0xd6, + fixext8 = 0xd7, + fixext16 = 0xd8, + str8 = 0xd9, + str16 = 0xda, + str32 = 0xdb, + array16 = 0xdc, + array32 = 0xdd, + map16 = 0xde, + map32 = 0xdf, +}; + test { std.testing.refAllDecls(@This()); } - -test { - const o: Object = .nil; - - _ = o; -} diff --git a/src/serialise.zig b/src/serialise.zig new file mode 100644 index 0000000..092eb8f --- /dev/null +++ b/src/serialise.zig @@ -0,0 +1,219 @@ +const std = @import("std"); + +const Object = @import("root.zig").Object; +const Tag = @import("root.zig").Tag; + +const SerialiseError = error{ + OutOfMemory, + StringTooLong, + BinaryTooLong, +}; + +fn intInRange(comptime T: type, i: i64) bool { + return std.math.minInt(T) <= i and i <= std.math.maxInt(T); +} + +fn serialise_int(alloc: std.mem.Allocator, i: i64) SerialiseError![]u8 { + if (i < 0 and i >= std.math.minInt(i6)) { + // neg fixint + const bytes = try alloc.alloc(u8, 1); + + std.mem.writeInt(i8, bytes[0..1], @intCast(i), .big); + + return bytes; + } + + if (intInRange(i8, i)) { // int8 + const bytes = try alloc.alloc(u8, 1 + 1); + + bytes[0] = 0xd0; + + std.mem.writeInt(i8, bytes[1..2], @intCast(i), .big); + + return bytes; + } + + if (intInRange(i16, i)) { // int16 + const bytes = try alloc.alloc(u8, 1 + 2); + + bytes[0] = 0xd1; + + std.mem.writeInt(i16, bytes[1..3], @intCast(i), .big); + + return bytes; + } + + if (intInRange(i32, i)) { // int32 + const bytes = try alloc.alloc(u8, 1 + 4); + + bytes[0] = 0xd2; + + std.mem.writeInt(i32, bytes[1..5], @intCast(i), .big); + + return bytes; + } + + // int64 + const bytes = try alloc.alloc(u8, 1 + 8); + + bytes[0] = 0xd3; + + std.mem.writeInt(i64, bytes[1..9], i, .big); + + return bytes; +} + +fn uintInRange(comptime T: type, u: u64) bool { + return u <= std.math.maxInt(T); +} + +fn serialise_uint(alloc: std.mem.Allocator, u: u64) SerialiseError![]u8 { + if (uintInRange(u7, u)) { // pos fixint + const bytes = try alloc.alloc(u8, 1); + + std.mem.writeInt(u8, bytes[0..1], @intCast(u), .big); + + return bytes; + } + + if (uintInRange(u8, u)) { // uint8 + const bytes = try alloc.alloc(u8, 1 + 1); + + bytes[0] = 0xcc; + + std.mem.writeInt(u8, bytes[1..2], @intCast(u), .big); + + return bytes; + } + + if (uintInRange(u16, u)) { // uint16 + const bytes = try alloc.alloc(u8, 1 + 2); + + bytes[0] = 0xcd; + + std.mem.writeInt(u16, bytes[1..3], @intCast(u), .big); + + return bytes; + } + + if (uintInRange(u32, u)) { // uint32 + const bytes = try alloc.alloc(u8, 1 + 4); + + bytes[0] = 0xce; + + std.mem.writeInt(u32, bytes[1..5], @intCast(u), .big); + + return bytes; + } + + // uint64 + const bytes = try alloc.alloc(u8, 1 + 8); + + bytes[0] = 0xcf; + + std.mem.writeInt(u64, bytes[1..9], u, .big); + + return bytes; +} + +fn serialise_raw(alloc: std.mem.Allocator, s: []const u8, comptime kind: enum { string, binary }) SerialiseError![]u8 { + // TODO: should we validate that s in UTF-8 here? + + if (kind == .string and s.len <= 31) { // fixstr + const bytes = try alloc.alloc(u8, 1 + s.len); + + bytes[0] = 0b101_00000 | @as(u8, @intCast(s.len)); + + @memcpy(bytes[1..], s); + + return bytes; + } + + if (s.len <= std.math.maxInt(u8)) { // str8 / bin8 + const bytes = try alloc.alloc(u8, 1 + 1 + s.len); + + bytes[0] = switch (kind) { + .string => 0xd9, + .binary => 0xc4, + }; + + bytes[1] = @as(u8, @intCast(s.len)); + + @memcpy(bytes[2..], s); + + return bytes; + } + + if (s.len <= std.math.maxInt(u16)) { // str16 / bin16 + const bytes = try alloc.alloc(u8, 1 + 2 + s.len); + + bytes[0] = switch (kind) { + .string => 0xda, + .binary => 0xc5, + }; + + std.mem.writeInt(u16, bytes[1..3], @intCast(s.len), .big); + + @memcpy(bytes[3..], s); + + return bytes; + } + + if (s.len <= std.math.maxInt(u32)) { // str16 / bin16 + const bytes = try alloc.alloc(u8, 1 + 4 + s.len); + + bytes[0] = switch (kind) { + .string => 0xdb, + .binary => 0xc6, + }; + + std.mem.writeInt(u32, bytes[1..5], @intCast(s.len), .big); + + @memcpy(bytes[5..], s); + + return bytes; + } + + return switch (kind) { + .string => error.StringTooLong, + .binary => error.BinaryTooLong, + }; +} + +pub fn serialise(alloc: std.mem.Allocator, obj: Object) SerialiseError![]u8 { + switch (obj) { + .nil => return try alloc.dupe(u8, &[_]u8{0xc0}), + .bool => |b| if (b) { + return try alloc.dupe(u8, &[_]u8{0xc3}); + } else { + return try alloc.dupe(u8, &[_]u8{0xc2}); + }, + .integer => |i| return try serialise_int(alloc, i), + .uinteger => |u| return try serialise_uint(alloc, u), + .float32 => |f| { + const bytes = try alloc.alloc(u8, 1 + 4); + + bytes[0] = 0xca; + + std.mem.writeInt(u32, bytes[1..5], @bitCast(f), .big); + + return bytes; + }, + .float64 => |f| { + const bytes = try alloc.alloc(u8, 1 + 8); + + bytes[0] = 0xcb; + + std.mem.writeInt(u64, bytes[1..9], @bitCast(f), .big); + + return bytes; + }, + .string => |s| return try serialise_raw(alloc, s, .string), + .binary => |b| return try serialise_raw(alloc, b, .binary), + else => unreachable, + } + + // const bytes = try alloc.alloc(u8, 0); + + // return bytes; +} diff --git a/tests/deserialise/binary.zig b/tests/deserialise/binary.zig index 9d3eb7d..6c04618 100644 --- a/tests/deserialise/binary.zig +++ b/tests/deserialise/binary.zig @@ -2,7 +2,7 @@ const std = @import("std"); const msgpack = @import("msgpack"); -const deserialise = msgpack.deserialise.deserialise; +const deserialise = msgpack.deserialise; fn test_binary(bytes: []const u8, expected: []const u8) !void { const alloc = std.testing.allocator; @@ -10,22 +10,22 @@ fn test_binary(bytes: []const u8, expected: []const u8) !void { const obj = try deserialise(alloc, bytes); defer obj.deinit(alloc); - try std.testing.expectEqualStrings(expected, obj.raw.binary); + try std.testing.expectEqualStrings(expected, obj.binary); } -test "binary 1" { +test "fixbin" { const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' }; try test_binary(&bytes, "ABC"); } -test "binary 2" { +test "empty bin" { const bytes = [_]u8{ 0xc4, 0x00 }; try test_binary(&bytes, ""); } -test "binary 3" { +test "bin8" { var bytes: [2 + 255]u8 = undefined; bytes[0] = 0xc4; @@ -38,7 +38,7 @@ test "binary 3" { try test_binary(&bytes, "A" ** 255); } -test "binary 4" { +test "bin16 1" { var bytes: [3 + 256]u8 = undefined; bytes[0] = 0xc5; @@ -52,7 +52,7 @@ test "binary 4" { try test_binary(&bytes, "A" ** 256); } -test "binary 5" { +test "bin16 2" { var bytes: [3 + 65535]u8 = undefined; bytes[0] = 0xc5; @@ -66,7 +66,7 @@ test "binary 5" { try test_binary(&bytes, "A" ** 65535); } -test "binary 6" { +test "bin32" { var bytes: [5 + 65536]u8 = undefined; bytes[0] = 0xc6; diff --git a/tests/deserialise/int.zig b/tests/deserialise/int.zig index b7a7069..1f34bf5 100644 --- a/tests/deserialise/int.zig +++ b/tests/deserialise/int.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub const msgpack = @import("msgpack"); -const deserialise = msgpack.deserialise.deserialise; +const deserialise = msgpack.deserialise; fn test_int(bytes: []const u8, expected: i64) !void { const alloc = std.testing.allocator; diff --git a/tests/deserialise/root.zig b/tests/deserialise/root.zig index c24d68a..9d5e456 100644 --- a/tests/deserialise/root.zig +++ b/tests/deserialise/root.zig @@ -1,13 +1,13 @@ const std = @import("std"); +pub const msgpack = @import("msgpack"); + pub const binary = @import("binary.zig"); pub const string = @import("string.zig"); pub const int = @import("int.zig"); pub const uint = @import("uint.zig"); -pub const msgpack = @import("msgpack"); - -const deserialise = msgpack.deserialise.deserialise; +const deserialise = msgpack.deserialise; test { @import("std").testing.refAllDecls(@This()); @@ -39,3 +39,64 @@ test "bool" { try std.testing.expectEqualDeep(msgpack.Object{ .bool = true }, obj); } } + +test "f32" { + const alloc = std.testing.allocator; + + { + const obj = try deserialise(alloc, &[_]u8{ 0xca, 0x7f, 0x7f, 0xff, 0xff }); + defer obj.deinit(alloc); + + // try std.testing.expectEqualDeep(msgpack.Object{ .float = 3.4028234e38 }, obj); + try std.testing.expectEqual(3.4028234e38, obj.float32); + } +} + +test "f64" { + const alloc = std.testing.allocator; + + { + const obj = try deserialise(alloc, &[_]u8{ 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); + defer obj.deinit(alloc); + + try std.testing.expectEqualDeep(msgpack.Object{ .float64 = 0.0 }, obj); + } + + { + const obj = try deserialise(alloc, &[_]u8{ 0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }); + defer obj.deinit(alloc); + + try std.testing.expectEqualDeep(msgpack.Object{ .float64 = 42.0 }, obj); + } +} + +test "array" { + const alloc = std.testing.allocator; + + { + const obj = try deserialise(alloc, &[_]u8{ 0x92, 0xa2, 0x6c, 0x65, 0xa4, 0x73, 0x68, 0x69, 0x74 }); + defer obj.deinit(alloc); + + try std.testing.expectEqual(2, obj.array.len); + + try std.testing.expectEqualStrings("le", obj.array[0].string); + try std.testing.expectEqualStrings("shit", obj.array[1].string); + } +} + +test "map" { + const alloc = std.testing.allocator; + + { + const obj = try deserialise(alloc, &[_]u8{ 0x82, 0x00, 0xa2, 0x6c, 0x65, 0x01, 0xa4, 0x73, 0x68, 0x69, 0x74 }); + defer obj.deinit(alloc); + + try std.testing.expectEqual(2, obj.map.len); + + try std.testing.expectEqual(0, obj.map[0].key.uinteger); + try std.testing.expectEqualStrings("le", obj.map[0].value.string); + + try std.testing.expectEqual(1, obj.map[1].key.uinteger); + try std.testing.expectEqualStrings("shit", obj.map[1].value.string); + } +} diff --git a/tests/deserialise/string.zig b/tests/deserialise/string.zig index bbc87bc..fc6e660 100644 --- a/tests/deserialise/string.zig +++ b/tests/deserialise/string.zig @@ -2,7 +2,7 @@ const std = @import("std"); const msgpack = @import("msgpack"); -const deserialise = msgpack.deserialise.deserialise; +const deserialise = msgpack.deserialise; fn test_string(bytes: []const u8, expected: []const u8) !void { const alloc = std.testing.allocator; @@ -10,26 +10,26 @@ fn test_string(bytes: []const u8, expected: []const u8) !void { const obj = try deserialise(alloc, bytes); defer obj.deinit(alloc); - try std.testing.expectEqualStrings(expected, obj.raw.string); + try std.testing.expectEqualStrings(expected, obj.string); } -test "string 1" { +test "fixstr 1" { const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' }; try test_string(&bytes, "ABC"); } -test "string 2" { +test "str8 1" { const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' }; try test_string(&bytes, "ABC"); } -test "string 3" { +test "empty string" { const bytes = [_]u8{ 0xd9, 0x00 }; try test_string(&bytes, ""); } -test "string 4" { +test "str8 2" { var bytes: [2 + 255]u8 = undefined; bytes[0] = 0xd9; @@ -41,7 +41,7 @@ test "string 4" { try test_string(&bytes, "A" ** 255); } -test "string 5" { +test "str16 1" { var bytes: [3 + 256]u8 = undefined; bytes[0] = 0xda; @@ -54,7 +54,7 @@ test "string 5" { try test_string(&bytes, "A" ** 256); } -test "string 6" { +test "str16 2" { var bytes: [3 + 65535]u8 = undefined; bytes[0] = 0xda; @@ -67,7 +67,7 @@ test "string 6" { try test_string(&bytes, "A" ** 65535); } -test "string 7" { +test "str32 1" { var bytes: [5 + 65536]u8 = undefined; bytes[0] = 0xdb; @@ -82,3 +82,6 @@ test "string 7" { try test_string(&bytes, "A" ** 65536); } +test "fixstr 2" { + try test_string(&[_]u8{ 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 }, "le message"); +} diff --git a/tests/deserialise/uint.zig b/tests/deserialise/uint.zig index 7f3cb7a..7eb3c9a 100644 --- a/tests/deserialise/uint.zig +++ b/tests/deserialise/uint.zig @@ -2,7 +2,7 @@ const std = @import("std"); pub const msgpack = @import("msgpack"); -const deserialise = msgpack.deserialise.deserialise; +const deserialise = msgpack.deserialise; fn test_uint(bytes: []const u8, expected: u64) !void { const alloc = std.testing.allocator; diff --git a/tests/root.zig b/tests/root.zig index 6dcea11..473541b 100644 --- a/tests/root.zig +++ b/tests/root.zig @@ -1,4 +1,5 @@ pub const deserialise = @import("deserialise/root.zig"); +pub const serialise = @import("serialise/root.zig"); test { @import("std").testing.refAllDecls(@This()); diff --git a/tests/serialise/binary.zig b/tests/serialise/binary.zig new file mode 100644 index 0000000..d5dfb99 --- /dev/null +++ b/tests/serialise/binary.zig @@ -0,0 +1,82 @@ +test "fixbin" { + const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' }; + + try test_binary(&bytes, "ABC"); +} + +test "empty bin" { + const bytes = [_]u8{ 0xc4, 0x00 }; + + try test_binary(&bytes, ""); +} + +test "bin8" { + var bytes: [2 + 255]u8 = undefined; + + bytes[0] = 0xc4; + bytes[1] = 0xff; + + for (bytes[2..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 255); +} +const std = @import("std"); + +const msgpack = @import("msgpack"); + +const serialise = msgpack.serialise; + +fn test_binary(expected: []const u8, s: []const u8) !void { + const alloc = std.testing.allocator; + + const bytes = try serialise(alloc, .{ .binary = s }); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, expected, bytes); +} + +test "bin16 1" { + var bytes: [3 + 256]u8 = undefined; + + bytes[0] = 0xc5; + bytes[1] = 0x01; + bytes[2] = 0x00; + + for (bytes[3..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 256); +} + +test "bin16 2" { + var bytes: [3 + 65535]u8 = undefined; + + bytes[0] = 0xc5; + bytes[1] = 0xff; + bytes[2] = 0xff; + + for (bytes[3..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 65535); +} + +test "bin32" { + var bytes: [5 + 65536]u8 = undefined; + + bytes[0] = 0xc6; + bytes[1] = 0x00; + bytes[2] = 0x01; + bytes[3] = 0x00; + bytes[4] = 0x00; + + for (bytes[5..]) |*c| { + c.* = 'A'; + } + + try test_binary(&bytes, "A" ** 65536); +} diff --git a/tests/serialise/int.zig b/tests/serialise/int.zig new file mode 100644 index 0000000..e12f5e6 --- /dev/null +++ b/tests/serialise/int.zig @@ -0,0 +1,43 @@ +const std = @import("std"); + +const msgpack = @import("msgpack"); + +const Object = msgpack.Object; +const serialise = msgpack.serialise; + +fn test_int(expected: []const u8, i: i64) !void { + const alloc = std.testing.allocator; + + const bytes = try serialise(alloc, .{ .integer = i }); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, expected, bytes); +} + +test "neg fixint" { + try test_int(&[_]u8{0xFF}, -1); +} + +test "int i8" { + try test_int(&[_]u8{ 0xd0, 0x80 }, std.math.minInt(i8)); + + try test_int(&[_]u8{ 0xd0, 0x7F }, std.math.maxInt(i8)); +} + +test "int i16" { + try test_int(&[_]u8{ 0xd1, 0x80, 0x00 }, std.math.minInt(i16)); + + try test_int(&[_]u8{ 0xd1, 0x7f, 0xff }, std.math.maxInt(i16)); +} + +test "int i32" { + try test_int(&[_]u8{ 0xd2, 0x80, 0x00, 0x00, 0x00 }, std.math.minInt(i32)); + + try test_int(&[_]u8{ 0xd2, 0x7f, 0xff, 0xff, 0xff }, std.math.maxInt(i32)); +} + +test "int i64" { + try test_int(&[_]u8{ 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, std.math.minInt(i64)); + + try test_int(&[_]u8{ 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(i64)); +} diff --git a/tests/serialise/root.zig b/tests/serialise/root.zig new file mode 100644 index 0000000..14d1713 --- /dev/null +++ b/tests/serialise/root.zig @@ -0,0 +1,83 @@ +const std = @import("std"); + +pub const msgpack = @import("msgpack"); + +pub const binary = @import("binary.zig"); +pub const string = @import("string.zig"); +pub const int = @import("int.zig"); +pub const uint = @import("uint.zig"); + +test { + @import("std").testing.refAllDecls(@This()); +} + +const Object = msgpack.Object; +const serialise = msgpack.serialise; + +test "nil" { + const alloc = std.testing.allocator; + + const obj: Object = .nil; + defer obj.deinit(alloc); + + const bytes = try serialise(alloc, obj); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, &[_]u8{0xc0}, bytes); +} + +test "bool" { + const alloc = std.testing.allocator; + + { + const obj = Object{ .bool = false }; + defer obj.deinit(alloc); + + const bytes = try serialise(alloc, obj); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, &[_]u8{0xc2}, bytes); + } + + { + const obj = Object{ .bool = true }; + defer obj.deinit(alloc); + + const bytes = try serialise(alloc, obj); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, &[_]u8{0xc3}, bytes); + } +} + +test "f32" { + const alloc = std.testing.allocator; + + { + const bytes = try serialise(alloc, .{ .float32 = 3.4028234e38 }); + defer alloc.free(bytes); + + // try std.testing.expectEqual(3.4028234e38, obj.float32); + try std.testing.expectEqualSlices(u8, &[_]u8{ 0xca, 0x7f, 0x7f, 0xff, 0xff }, bytes); + } +} + +test "f64" { + const alloc = std.testing.allocator; + + { + const bytes = try serialise(alloc, .{ .float64 = 0.0 }); + defer alloc.free(bytes); + + // try std.testing.expectEqual(3.4028234e38, obj.float32); + try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, bytes); + } + + { + const bytes = try serialise(alloc, .{ .float64 = 42.0 }); + defer alloc.free(bytes); + + // try std.testing.expectEqual(3.4028234e38, obj.float32); + try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, bytes); + } +} diff --git a/tests/serialise/string.zig b/tests/serialise/string.zig new file mode 100644 index 0000000..30c7a8a --- /dev/null +++ b/tests/serialise/string.zig @@ -0,0 +1,84 @@ +const std = @import("std"); + +const msgpack = @import("msgpack"); + +const serialise = msgpack.serialise; + +fn test_string(expected: []const u8, s: []const u8) !void { + const alloc = std.testing.allocator; + + const bytes = try serialise(alloc, .{ .string = s }); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, expected, bytes); +} + +test "fixstr" { + const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' }; + + try test_string(&bytes, "ABC"); +} + +test "empty str" { + const bytes = [_]u8{0b101_00000}; + + try test_string(&bytes, ""); +} + +test "string 4" { + var bytes: [2 + 255]u8 = undefined; + + bytes[0] = 0xd9; + bytes[1] = 0xff; + + for (bytes[2..]) |*c| { + c.* = 'A'; + } + + try test_string(&bytes, "A" ** 255); +} + +// test "string 5" { +// var bytes: [3 + 256]u8 = undefined; + +// bytes[0] = 0xda; +// bytes[1] = 0x01; +// bytes[2] = 0x00; + +// for (bytes[3..]) |*c| { +// c.* = 'A'; +// } + +// try test_string(&bytes, "A" ** 256); +// } +// test "string 6" { +// var bytes: [3 + 65535]u8 = undefined; + +// bytes[0] = 0xda; +// bytes[1] = 0xff; +// bytes[2] = 0xff; + +// for (bytes[3..]) |*c| { +// c.* = 'A'; +// } + +// try test_string(&bytes, "A" ** 65535); +// } +// test "string 7" { +// var bytes: [5 + 65536]u8 = undefined; + +// bytes[0] = 0xdb; +// bytes[1] = 0x00; +// bytes[2] = 0x01; +// bytes[3] = 0x00; +// bytes[4] = 0x00; + +// for (bytes[5..]) |*c| { +// c.* = 'A'; +// } + +// try test_string(&bytes, "A" ** 65536); +// } +// test "string 8" { +// try test_string(&[_]u8{ 0xaa, 0x6c, 0x65, 0x20, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65 }, "le message"); +// } diff --git a/tests/serialise/uint.zig b/tests/serialise/uint.zig new file mode 100644 index 0000000..9fc66af --- /dev/null +++ b/tests/serialise/uint.zig @@ -0,0 +1,39 @@ +const std = @import("std"); + +const msgpack = @import("msgpack"); + +const Object = msgpack.Object; +const serialise = msgpack.serialise; + +fn test_uint(expected: []const u8, u: u64) !void { + const alloc = std.testing.allocator; + + const bytes = try serialise(alloc, .{ .uinteger = u }); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, expected, bytes); +} + +test "pos fixint" { + try test_uint(&[_]u8{0x00}, 0); + + try test_uint(&[_]u8{0x07}, 0x07); + + try test_uint(&[_]u8{0x7F}, 0x7F); +} + +test "int u8" { + try test_uint(&[_]u8{ 0xcc, 0xff }, std.math.maxInt(u8)); +} + +test "int u16" { + try test_uint(&[_]u8{ 0xcd, 0xff, 0xff }, std.math.maxInt(u16)); +} + +test "int u32" { + try test_uint(&[_]u8{ 0xce, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(u32)); +} + +test "int u64" { + try test_uint(&[_]u8{ 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(u64)); +} From 3fce037239b3d6f01ce121dfc31ae37ce7af4b4b Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Wed, 2 Apr 2025 17:54:07 +0200 Subject: [PATCH 2/3] switch serialiser to ArrayList for now possibly later switch to a Writer or precount the number of bytes that will be written --- src/root.zig | 4 +- src/serialise.zig | 207 ++++++++++++++-------------------------------- 2 files changed, 66 insertions(+), 145 deletions(-) diff --git a/src/root.zig b/src/root.zig index c154e04..bb94b4e 100644 --- a/src/root.zig +++ b/src/root.zig @@ -34,8 +34,8 @@ pub const Object = union(enum) { float64: f64, string: []const u8, binary: []const u8, - array: []Object, - map: []MapEntry, + array: []const Object, + map: []const MapEntry, extension: struct { type: u8, bytes: []u8 }, pub fn deinit(self: Object, alloc: std.mem.Allocator) void { diff --git a/src/serialise.zig b/src/serialise.zig index 092eb8f..d990b1a 100644 --- a/src/serialise.zig +++ b/src/serialise.zig @@ -13,165 +13,82 @@ fn intInRange(comptime T: type, i: i64) bool { return std.math.minInt(T) <= i and i <= std.math.maxInt(T); } -fn serialise_int(alloc: std.mem.Allocator, i: i64) SerialiseError![]u8 { - if (i < 0 and i >= std.math.minInt(i6)) { - // neg fixint - const bytes = try alloc.alloc(u8, 1); +fn serialise_int(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), i: i64) SerialiseError!void { + if (i < 0 and i >= std.math.minInt(i6)) { // neg fixint + std.mem.writeInt(i8, try buf.addManyAsArray(alloc, 1), @intCast(i), .big); - std.mem.writeInt(i8, bytes[0..1], @intCast(i), .big); - - return bytes; + return; } - if (intInRange(i8, i)) { // int8 - const bytes = try alloc.alloc(u8, 1 + 1); + inline for (0.., &[_]type{ i8, i16, i32, i64 }) |n, T| { + if (intInRange(T, i)) { + try buf.ensureUnusedCapacity(alloc, 1 + @sizeOf(T)); - bytes[0] = 0xd0; + try buf.append(alloc, 0xd0 + n); - std.mem.writeInt(i8, bytes[1..2], @intCast(i), .big); + std.mem.writeInt(T, try buf.addManyAsArray(alloc, @sizeOf(T)), @intCast(i), .big); - return bytes; + return; + } } - if (intInRange(i16, i)) { // int16 - const bytes = try alloc.alloc(u8, 1 + 2); - - bytes[0] = 0xd1; - - std.mem.writeInt(i16, bytes[1..3], @intCast(i), .big); - - return bytes; - } - - if (intInRange(i32, i)) { // int32 - const bytes = try alloc.alloc(u8, 1 + 4); - - bytes[0] = 0xd2; - - std.mem.writeInt(i32, bytes[1..5], @intCast(i), .big); - - return bytes; - } - - // int64 - const bytes = try alloc.alloc(u8, 1 + 8); - - bytes[0] = 0xd3; - - std.mem.writeInt(i64, bytes[1..9], i, .big); - - return bytes; + unreachable; } fn uintInRange(comptime T: type, u: u64) bool { return u <= std.math.maxInt(T); } -fn serialise_uint(alloc: std.mem.Allocator, u: u64) SerialiseError![]u8 { +fn serialise_uint(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), u: u64) SerialiseError!void { if (uintInRange(u7, u)) { // pos fixint - const bytes = try alloc.alloc(u8, 1); + std.mem.writeInt(u8, try buf.addManyAsArray(alloc, 1), @intCast(u), .big); - std.mem.writeInt(u8, bytes[0..1], @intCast(u), .big); - - return bytes; + return; } - if (uintInRange(u8, u)) { // uint8 - const bytes = try alloc.alloc(u8, 1 + 1); + inline for (0.., &[_]type{ u8, u16, u32, u64 }) |n, T| { + if (uintInRange(T, u)) { + try buf.ensureUnusedCapacity(alloc, 1 + @sizeOf(T)); - bytes[0] = 0xcc; + try buf.append(alloc, 0xcc + n); - std.mem.writeInt(u8, bytes[1..2], @intCast(u), .big); + std.mem.writeInt(T, try buf.addManyAsArray(alloc, @sizeOf(T)), @intCast(u), .big); - return bytes; + return; + } } - if (uintInRange(u16, u)) { // uint16 - const bytes = try alloc.alloc(u8, 1 + 2); - - bytes[0] = 0xcd; - - std.mem.writeInt(u16, bytes[1..3], @intCast(u), .big); - - return bytes; - } - - if (uintInRange(u32, u)) { // uint32 - const bytes = try alloc.alloc(u8, 1 + 4); - - bytes[0] = 0xce; - - std.mem.writeInt(u32, bytes[1..5], @intCast(u), .big); - - return bytes; - } - - // uint64 - const bytes = try alloc.alloc(u8, 1 + 8); - - bytes[0] = 0xcf; - - std.mem.writeInt(u64, bytes[1..9], u, .big); - - return bytes; + unreachable; } -fn serialise_raw(alloc: std.mem.Allocator, s: []const u8, comptime kind: enum { string, binary }) SerialiseError![]u8 { - // TODO: should we validate that s in UTF-8 here? +fn serialise_raw(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), s: []const u8, comptime kind: enum { string, binary }) SerialiseError!void { + // TODO: should we validate that `s` is UTF-8 here? if (kind == .string and s.len <= 31) { // fixstr - const bytes = try alloc.alloc(u8, 1 + s.len); + try buf.ensureUnusedCapacity(alloc, 1 + s.len); - bytes[0] = 0b101_00000 | @as(u8, @intCast(s.len)); + try buf.append(alloc, 0b101_00000 | @as(u8, @intCast(s.len))); - @memcpy(bytes[1..], s); + try buf.appendSlice(alloc, s); - return bytes; + return; } - if (s.len <= std.math.maxInt(u8)) { // str8 / bin8 - const bytes = try alloc.alloc(u8, 1 + 1 + s.len); + inline for (0.., &[_]type{ u8, u16, u32 }) |i, T| { + if (s.len <= std.math.maxInt(T)) { + try buf.ensureUnusedCapacity(alloc, 1 + @sizeOf(T) + s.len); - bytes[0] = switch (kind) { - .string => 0xd9, - .binary => 0xc4, - }; + switch (kind) { + .string => try buf.append(alloc, 0xd9 + i), + .binary => try buf.append(alloc, 0xc4 + i), + } - bytes[1] = @as(u8, @intCast(s.len)); + std.mem.writeInt(T, try buf.addManyAsArray(alloc, @sizeOf(T)), @intCast(s.len), .big); - @memcpy(bytes[2..], s); + try buf.appendSlice(alloc, s); - return bytes; - } - - if (s.len <= std.math.maxInt(u16)) { // str16 / bin16 - const bytes = try alloc.alloc(u8, 1 + 2 + s.len); - - bytes[0] = switch (kind) { - .string => 0xda, - .binary => 0xc5, - }; - - std.mem.writeInt(u16, bytes[1..3], @intCast(s.len), .big); - - @memcpy(bytes[3..], s); - - return bytes; - } - - if (s.len <= std.math.maxInt(u32)) { // str16 / bin16 - const bytes = try alloc.alloc(u8, 1 + 4 + s.len); - - bytes[0] = switch (kind) { - .string => 0xdb, - .binary => 0xc6, - }; - - std.mem.writeInt(u32, bytes[1..5], @intCast(s.len), .big); - - @memcpy(bytes[5..], s); - - return bytes; + return; + } } return switch (kind) { @@ -180,40 +97,44 @@ fn serialise_raw(alloc: std.mem.Allocator, s: []const u8, comptime kind: enum { }; } +fn serialise_array(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), array: []const Object) SerialiseError!void { + _ = alloc; + _ = buf; + _ = array; +} + pub fn serialise(alloc: std.mem.Allocator, obj: Object) SerialiseError![]u8 { + var buf = std.ArrayListUnmanaged(u8){}; + defer buf.deinit(alloc); + switch (obj) { - .nil => return try alloc.dupe(u8, &[_]u8{0xc0}), + .nil => try buf.append(alloc, 0xc0), .bool => |b| if (b) { - return try alloc.dupe(u8, &[_]u8{0xc3}); + try buf.append(alloc, 0xc3); } else { - return try alloc.dupe(u8, &[_]u8{0xc2}); + try buf.append(alloc, 0xc2); }, - .integer => |i| return try serialise_int(alloc, i), - .uinteger => |u| return try serialise_uint(alloc, u), + .integer => |i| try serialise_int(alloc, &buf, i), + .uinteger => |u| try serialise_uint(alloc, &buf, u), .float32 => |f| { - const bytes = try alloc.alloc(u8, 1 + 4); + try buf.ensureUnusedCapacity(alloc, 1 + 4); - bytes[0] = 0xca; + try buf.append(alloc, 0xca); - std.mem.writeInt(u32, bytes[1..5], @bitCast(f), .big); - - return bytes; + std.mem.writeInt(u32, try buf.addManyAsArray(alloc, 4), @bitCast(f), .big); }, .float64 => |f| { - const bytes = try alloc.alloc(u8, 1 + 8); + try buf.ensureUnusedCapacity(alloc, 1 + 8); - bytes[0] = 0xcb; + try buf.append(alloc, 0xcb); - std.mem.writeInt(u64, bytes[1..9], @bitCast(f), .big); - - return bytes; + std.mem.writeInt(u64, try buf.addManyAsArray(alloc, 8), @bitCast(f), .big); }, - .string => |s| return try serialise_raw(alloc, s, .string), - .binary => |b| return try serialise_raw(alloc, b, .binary), + .string => |s| try serialise_raw(alloc, &buf, s, .string), + .binary => |b| try serialise_raw(alloc, &buf, b, .binary), + .array => |array| try serialise_array(alloc, &buf, array), else => unreachable, } - // const bytes = try alloc.alloc(u8, 0); - - // return bytes; + return try buf.toOwnedSlice(alloc); } From b2d93a466e3171a7eca2186756b2317f6e776244 Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Wed, 2 Apr 2025 18:49:28 +0200 Subject: [PATCH 3/3] more deserialiser work --- src/serialise.zig | 105 ++++++++++++++++++++++++++++++++++----- tests/serialise/root.zig | 35 +++++++++++++ 2 files changed, 127 insertions(+), 13 deletions(-) diff --git a/src/serialise.zig b/src/serialise.zig index d990b1a..1ba0d3f 100644 --- a/src/serialise.zig +++ b/src/serialise.zig @@ -1,12 +1,16 @@ const std = @import("std"); const Object = @import("root.zig").Object; -const Tag = @import("root.zig").Tag; + +const MapEntry = @import("root.zig").MapEntry; const SerialiseError = error{ OutOfMemory, StringTooLong, BinaryTooLong, + ArrayTooLong, + MapTooLong, + ExtTooLong, }; fn intInRange(comptime T: type, i: i64) bool { @@ -98,15 +102,82 @@ fn serialise_raw(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), s: } fn serialise_array(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), array: []const Object) SerialiseError!void { - _ = alloc; - _ = buf; - _ = array; + if (array.len <= 15) { + try buf.append(alloc, 0b1001_0000 | @as(u8, @intCast(array.len))); + } else if (array.len <= std.math.maxInt(u16)) { + try buf.append(alloc, 0xdc); + + std.mem.writeInt(u16, try buf.addManyAsArray(alloc, @sizeOf(u16)), @intCast(array.len), .big); + } else if (array.len <= std.math.maxInt(u32)) { + try buf.append(alloc, 0xdd); + + std.mem.writeInt(u32, try buf.addManyAsArray(alloc, @sizeOf(u32)), @intCast(array.len), .big); + } else { + return error.ArrayTooLong; + } + + for (array) |obj| { + try serialise_into_buf(alloc, buf, obj); + } } -pub fn serialise(alloc: std.mem.Allocator, obj: Object) SerialiseError![]u8 { - var buf = std.ArrayListUnmanaged(u8){}; - defer buf.deinit(alloc); +fn serialise_map(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), map: []const MapEntry) SerialiseError!void { + if (map.len <= 15) { + try buf.append(alloc, 0b1000_0000 | @as(u8, @intCast(map.len))); + } else if (map.len <= std.math.maxInt(u16)) { + try buf.append(alloc, 0xde); + std.mem.writeInt(u16, try buf.addManyAsArray(alloc, @sizeOf(u16)), @intCast(map.len), .big); + } else if (map.len <= std.math.maxInt(u32)) { + try buf.append(alloc, 0xdf); + + std.mem.writeInt(u32, try buf.addManyAsArray(alloc, @sizeOf(u32)), @intCast(map.len), .big); + } else { + return error.MapTooLong; + } + + for (map) |entry| { + try serialise_into_buf(alloc, buf, entry.key); + try serialise_into_buf(alloc, buf, entry.value); + } +} + +fn serialise_ext(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), type_: u8, bytes: []u8) SerialiseError!void { + // fixext + for (0.., [_]usize{ 1, 2, 4, 8, 16 }) |n, len| { + if (bytes.len == len) { + try buf.ensureUnusedCapacity(alloc, 1 + 1 + len); + + try buf.append(alloc, 0xd4 + @as(u8, @intCast(n))); + try buf.append(alloc, type_); + + try buf.appendSlice(alloc, bytes); + + return; + } + } + + // ext + inline for (0.., [_]type{ u8, u16, u32 }) |n, T| { + if (bytes.len <= std.math.maxInt(T)) { + try buf.ensureUnusedCapacity(alloc, 1 + @sizeOf(T) + bytes.len); + + try buf.append(alloc, 0xc7 + n); + + std.mem.writeInt(T, try buf.addManyAsArray(alloc, @sizeOf(T)), @intCast(bytes.len), .big); + + try buf.append(alloc, type_); + + try buf.appendSlice(alloc, bytes); + + return; + } + } + + return error.ExtTooLong; +} + +fn serialise_into_buf(alloc: std.mem.Allocator, buf: *std.ArrayListUnmanaged(u8), obj: Object) SerialiseError!void { switch (obj) { .nil => try buf.append(alloc, 0xc0), .bool => |b| if (b) { @@ -114,8 +185,8 @@ pub fn serialise(alloc: std.mem.Allocator, obj: Object) SerialiseError![]u8 { } else { try buf.append(alloc, 0xc2); }, - .integer => |i| try serialise_int(alloc, &buf, i), - .uinteger => |u| try serialise_uint(alloc, &buf, u), + .integer => |i| try serialise_int(alloc, buf, i), + .uinteger => |u| try serialise_uint(alloc, buf, u), .float32 => |f| { try buf.ensureUnusedCapacity(alloc, 1 + 4); @@ -130,11 +201,19 @@ pub fn serialise(alloc: std.mem.Allocator, obj: Object) SerialiseError![]u8 { std.mem.writeInt(u64, try buf.addManyAsArray(alloc, 8), @bitCast(f), .big); }, - .string => |s| try serialise_raw(alloc, &buf, s, .string), - .binary => |b| try serialise_raw(alloc, &buf, b, .binary), - .array => |array| try serialise_array(alloc, &buf, array), - else => unreachable, + .string => |s| try serialise_raw(alloc, buf, s, .string), + .binary => |b| try serialise_raw(alloc, buf, b, .binary), + .array => |array| try serialise_array(alloc, buf, array), + .map => |map| try serialise_map(alloc, buf, map), + .extension => |ext| try serialise_ext(alloc, buf, ext.type, ext.bytes), } +} + +pub fn serialise(alloc: std.mem.Allocator, obj: Object) SerialiseError![]u8 { + var buf = std.ArrayListUnmanaged(u8){}; + defer buf.deinit(alloc); + + try serialise_into_buf(alloc, &buf, obj); return try buf.toOwnedSlice(alloc); } diff --git a/tests/serialise/root.zig b/tests/serialise/root.zig index 14d1713..bc6a2b6 100644 --- a/tests/serialise/root.zig +++ b/tests/serialise/root.zig @@ -81,3 +81,38 @@ test "f64" { try std.testing.expectEqualSlices(u8, &[_]u8{ 0xcb, 0x40, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, bytes); } } + +test "array" { + const alloc = std.testing.allocator; + + { + // const obj = try deserialise(alloc, &[_]u8{ 0x92, 0xa2, 0x6c, 0x65, 0xa4, 0x73, 0x68, 0x69, 0x74 }); + // defer obj.deinit(alloc); + + // try std.testing.expectEqual(2, obj.array.len); + + // try std.testing.expectEqualStrings("le", obj.array[0].string); + // try std.testing.expectEqualStrings("shit", obj.array[1].string); + + const bytes = try serialise(alloc, .{ .array = &[_]Object{ .{ .string = "le" }, .{ .string = "shit" } } }); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, &[_]u8{ 0x92, 0xa2, 0x6c, 0x65, 0xa4, 0x73, 0x68, 0x69, 0x74 }, bytes); + } +} + +test "map" { + const alloc = std.testing.allocator; + + const MapEntry = msgpack.MapEntry; + + { + const bytes = try serialise(alloc, .{ .map = &[_]MapEntry{ + .{ .key = .{ .uinteger = 0 }, .value = .{ .string = "le" } }, + .{ .key = .{ .uinteger = 1 }, .value = .{ .string = "shit" } }, + } }); + defer alloc.free(bytes); + + try std.testing.expectEqualSlices(u8, &[_]u8{ 0x82, 0x00, 0xa2, 0x6c, 0x65, 0x01, 0xa4, 0x73, 0x68, 0x69, 0x74 }, bytes); + } +}