msgpack-zig/src/root.zig

69 lines
1.4 KiB
Zig
Raw Normal View History

2025-03-30 15:15:38 +02:00
const std = @import("std");
2025-03-30 15:38:38 +02:00
pub const deserialise = @import("deserialise.zig");
pub const Raw = union(enum) {
2025-03-30 15:38:22 +02:00
string: []u8,
binary: []u8,
2025-03-30 15:38:38 +02:00
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,
value: Object,
pub fn deinit(self: MapEntry, alloc: std.mem.Allocator) void {
self.key.deinit(alloc);
self.value.deinit(alloc);
}
2025-03-30 15:38:22 +02:00
};
2025-03-30 15:38:38 +02:00
pub const Object = union(enum) {
2025-03-30 15:38:22 +02:00
nil,
bool: bool,
integer: i64,
float: f64,
raw: Raw,
array: []Object,
2025-03-30 15:38:38 +02:00
map: []MapEntry,
2025-03-30 15:38:22 +02:00
extension: struct { type: u8, bytes: []u8 },
2025-03-30 15:38:38 +02:00
pub fn deinit(self: Object, alloc: std.mem.Allocator) void {
switch (self) {
.raw => |raw| raw.deinit(alloc),
.array => |array| {
for (array) |x| {
x.deinit(alloc);
}
alloc.free(array);
},
.map => |map| {
for (map) |elem| {
elem.key.deinit(alloc);
elem.value.deinit(alloc);
}
alloc.free(map);
},
.extension => |ext| alloc.free(ext.bytes),
else => {},
}
}
2025-03-30 15:38:22 +02:00
};
2025-03-30 15:38:38 +02:00
test {
std.testing.refAllDecls(@This());
}
2025-03-30 15:38:22 +02:00
test {
const o: Object = .nil;
2025-03-30 15:15:38 +02:00
2025-03-30 15:38:22 +02:00
_ = o;
2025-03-30 15:15:38 +02:00
}