deserialiser
This commit is contained in:
parent
f9b5f62e65
commit
f454526c5a
3 changed files with 827 additions and 3 deletions
52
src/root.zig
52
src/root.zig
|
|
@ -1,21 +1,67 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Raw = union(enum) {
|
||||
pub const deserialise = @import("deserialise.zig");
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const Object = union(enum) {
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
pub const Object = union(enum) {
|
||||
nil,
|
||||
bool: bool,
|
||||
integer: i64,
|
||||
float: f64,
|
||||
raw: Raw,
|
||||
array: []Object,
|
||||
map: []struct { key: Object, value: 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),
|
||||
.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 => {},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test {
|
||||
std.testing.refAllDecls(@This());
|
||||
}
|
||||
|
||||
test {
|
||||
const o: Object = .nil;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue